<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Admin\Staff;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;

class StaffController extends Controller
{
    public function addStaff(): View
    {
        return view('admin.add-staff', [
        ]);
    }

    public function storeStaff(Request $request): RedirectResponse
    {
        $staff = new Staff();

        $request->validate([
            'name' => 'required',
            'occupation' => 'required',
            'path' => 'required',
            'order' => 'required',
        ]);

        // Determine the values of active and featured based on the checkbox status
        $active = $request->has('active') ? 1 : 0;


        $staff->name = $request->name;
        $staff->occupation = $request->occupation;
        $staff->active = $active;
        $staff->image = $request->path;
        $staff->order = $request->order;
        $staff->slug = strtolower(str_replace(' ', '-', $request->name));

        $staff->save();

        return redirect()->route('list-staffs')->with('success', 'Staff created successfully');

    }


    public function listStaffs(): View
    {
        $staffs = Staff::all();
        return view('admin.list-staffs', [
            'staffs' => $staffs,
        ]);
    }


    public function editStaff($id): View
    {

        $staff = Staff::findOrFail($id);


        return view('admin.edit-staff', [
            'staff' => $staff,
        ]);
    }


    public function updateStaff(Request $request): RedirectResponse
    {

        $request->validate([
            'name' => 'required',
            'occupation' => 'required',
            'path' => 'required',
            'order' => 'required',
        ]);

        $id = $request->id;

        // Determine the values of active and featured based on the checkbox status
        $active = $request->has('active') ? 1 : 0;

        Staff::findOrFail($id)->update([
            'name' => $request->name,
            'occupation' => $request->occupation,
            'active' => $active,
            'image' => $request->path,
            'order' => $request->order,
            'slug' => strtolower(str_replace(' ', '-', $request->name)),
        ]);

        return redirect()->route('list-staffs')->with('success', 'Staff updated successfully');

    }


    public function deleteStaff($id): RedirectResponse
    {
        Staff::findOrFail($id)->delete();
        return redirect()->route('list-staffs')->with('success', 'Staff deleted successfully');

    }

}
