<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Benefit;
use App\Models\GeneralSetting;
use Brian2694\Toastr\Facades\Toastr;
use Illuminate\Http\Request;

class BenefitController extends Controller
{
    public function index()
    {
        $benefits       = Benefit::orderBy('sort_order')->orderBy('id', 'DESC')->get();
        $generalsetting = GeneralSetting::where('status', 1)->first();

        return view('backEnd.benefit.index', compact('benefits', 'generalsetting'));
    }

    public function updateSectionTitle(Request $request)
    {
        $request->validate([
            'benefit_section_title' => 'required|string|max:255',
        ]);

        GeneralSetting::where('status', 1)->update([
            'benefit_section_title' => $request->benefit_section_title,
        ]);

        Toastr::success('Success', 'Section title updated successfully');

        return redirect()->back();
    }

    public function create()
    {
        return view('backEnd.benefit.create');
    }

    public function store(Request $request)
    {
        $request->validate([
            'title'  => 'required|string|max:255',
            'status' => 'nullable|boolean',
        ]);

        Benefit::create([
            'title'  => $request->title,
            'status' => $request->boolean('status', true),
        ]);

        Toastr::success('Success', 'Benefit created successfully');

        return redirect()->route('admin.benefit.index');
    }

    public function edit($id)
    {
        $benefit = Benefit::findOrFail($id);

        return view('backEnd.benefit.edit', compact('benefit'));
    }

    public function update(Request $request, $id)
    {
        $benefit = Benefit::findOrFail($id);

        $request->validate([
            'title'  => 'required|string|max:255',
            'status' => 'nullable|boolean',
        ]);

        $benefit->title  = $request->title;
        $benefit->status = $request->boolean('status', true);
        $benefit->save();

        Toastr::success('Success', 'Benefit updated successfully');

        return redirect()->route('admin.benefit.index');
    }

    public function destroy(Request $request)
    {
        $benefit = Benefit::findOrFail($request->hidden_id);
        $benefit->delete();

        Toastr::success('Success', 'Benefit deleted successfully');

        return redirect()->back();
    }
}
