<?php

namespace App\Http\Controllers\Admin;

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

class ManagementFaqControllers extends Controller
{
    public function index()
    {
        $faqs = Faq::latest()->get();
        return view('admin.management-faq.index', compact('faqs'));
    }

    public function create()
    {
        return view('admin.management-faq.create');
    }

    public function store(Request $request)
    {
        $request->validate([
            'question' => 'required',
            'answer' => 'required',
        ]);

        Faq::create($request->all());

        return redirect()->route('admin.management-faq.index')
            ->with('success', 'FAQ berhasil ditambahkan');
    }

    public function edit($id)
    {
        $faq = Faq::findOrFail($id);
        return view('admin.management-faq.edit', compact('faq'));
    }

    public function update(Request $request, $id)
    {
        $faq = Faq::findOrFail($id);

        $request->validate([
            'question' => 'required',
            'answer' => 'required',
        ]);

        $faq->update($request->all());

        return redirect()->route('admin.management-faq.index')
            ->with('success', 'FAQ berhasil diupdate');
    }

    public function destroy($id)
    {
        Faq::destroy($id);

        return redirect()->route('admin.management-faq.index')
            ->with('success', 'FAQ berhasil dihapus');
    }
}
