<?php

namespace App\Http\Controllers\Admin;

use App\Services\Admin\faqServices;
use App\Models\Faq;
use Illuminate\Support\Facades\Log;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class FaqController extends Controller
{    
    /**
    * Created By : sayali parab
    * Created at : 24 May 2024
    * Use : To get faq  page.
    */

    protected $faqServices;
    public function __construct(faqServices $faqServices)
    {
        $this->faqServices = $faqServices;
    }
     /**
    * Created By : sayali parab
    * Created at : 24 May 2024
    * Use : To get faq  page.
    */
    public function index()
    {
        $data = $this->faqServices->viewfaq();
        // return $data;
        return view('Admin.pages.manage_cms.manage_faq.manage_faq')->with($data);
    }
    /**
    * Created By : sayali parab
    * Created at : 24 May 2024
    * Use : To change the status of faq  page.
    */
    public function change_faqStatus(Request $request)
    {
        $status = Faq::find($request->program_id);
        $status->is_active = $request->status;
        $status->save();
        return response()->json(['success' => 'Status change successfully.']);
    }

   /**
    * Created By : sayali parab
    * Created at : 24 May 2024
    * Use : To delete faq  page.
    */
    public function delete_faq($id)
    {
        $faq = Faq::find($id);
        if (!$faq) {
            return response()->json(['success' => false, 'status' => 404, 'message' => 'FAQ not found']);
        }

        $faq->delete();
        return response()->json(['success' => true, 'status' => 200]);
    }
    /**
    * Created By : sayali parab
    * Created at : 24 May 2024
    * Use : To update faq  page.
    */
    public function update(Request $request)
    {
        $faq = new Faq();
        $faq = Faq::find($request->faq_id);
        $faq->question = $request->question;
        $faq->answers = $request->answer;
        $faq->faq_category_id = $request->faq_categ;
        $faq->save();
        return response()->json(['success' => true, 'status' => 200]);
    }
    /**
    * Created By : sayali parab
    * Created at : 24 May 2024
    * Use : To store faq  page.
    */
    public function store(Request $request)
    {
        try {
            $faq = new Faq();
            $faq->question = $request->question;
            $faq->answers = $request->answer;
            $faq->faq_category_id = $request->faq_categ;
            $faq->save();

            return response()->json(['success' => true, 'status' => 200]);
        } catch (\Exception $e) {
            // Log the exception or handle it as needed
            return response()->json(['success' => false, 'error' => $e->getMessage(), 'status' => 500]);
        }
    }
}
