<?php

namespace App\Http\Controllers\Admin;

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

class TestimonyController extends Controller
{
    public function addTestimony(): View
    {
        return view('admin.add-testimony', [
        ]);
    }

    public function storeTestimony(Request $request): RedirectResponse
    {
        $testimony = new Testimony();

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

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


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

        $testimony->save();

        return redirect()->route('list-testimonies')->with('success', 'Testimony added successfully');

    }


    public function listTestimonies(): View
    {
        $testimonies = Testimony::all();
        return view('admin.list-testimonies', [
            'testimonies' => $testimonies,
        ]);
    }


    public function editTestimony($id): View
    {

        $testimony = Testimony::findOrFail($id);


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


    public function updateTestimony(Request $request): RedirectResponse
    {

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

//        dd($request);
        $id = $request->id;

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

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

        return redirect()->route('list-testimonies')->with('success', 'Testimony updated successfully');

    }


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

    }

}
