<?php

namespace App\Http\Controllers;

use App\Models\User;
use App\Models\UserRole;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\Rule;

class StaffController extends Controller
{
    /**
     * Display staff list.
     */
    public function index()
    {
        $staff = User::with('role')->get();

        return view('staff.list', compact('staff'));
    }


    /**
     * Show add staff form.
     */
    public function create()
    {
        $roles = UserRole::all();

        return view('staff.add', compact('roles'));
    }


    /**
     * Show individual staff details.
     */
    public function show($id)
    {
        $staff = User::with('role')->findOrFail($id);

        return view('staff.view', compact('staff'));
    }


    /**
     * Show edit staff form.
     */
    public function edit($id)
    {
        $staff = User::findOrFail($id);

        $roles = UserRole::all();

        return view('staff.edit', compact('staff', 'roles'));
    }


    /**
     * Delete staff member.
     */
    public function destroy($id)
    {
        $staff = User::findOrFail($id);

        $staff->delete();

        return redirect()
            ->route('staff.list')
            ->with('success', 'Staff member deleted successfully.');
    }


    /**
     * Update staff member.
     */
    public function update(Request $request, $id)
    {
        $staff = User::findOrFail($id);


        $validated = $request->validate([

            'user_name' => [
                'required',
                'string',
                'max:255',
            ],

            'user_email' => [
                'required',
                'email',
                'max:255',
                Rule::unique('users', 'user_email')
                    ->ignore($staff->user_id, 'user_id'),
            ],

            'role_id' => [
                'required',
                'exists:user_role,role_id',
            ],

            'is_active' => [
                'required',
                'boolean',
            ],

            'password' => [
                'nullable',
                'string',
                'min:6',
                'confirmed',
            ],

        ]);


        /*
        |--------------------------------------------------------------------------
        | Update Password Only If A New Password Was Entered
        |--------------------------------------------------------------------------
        */

        if (!empty($validated['password'])) {

            $validated['password'] = Hash::make(
                $validated['password']
            );

        } else {

            unset($validated['password']);

        }


        $staff->update($validated);


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


    /**
     * Store new staff member.
     */
    public function store(Request $request)
    {
        $validated = $request->validate([

            'user_name' => [
                'required',
                'string',
                'max:255',
            ],

            'user_email' => [
                'required',
                'email',
                'max:255',
                'unique:users,user_email',
            ],

            'password' => [
                'required',
                'string',
                'min:6',
                'confirmed',
            ],

            'role_id' => [
                'required',
                'exists:user_role,role_id',
            ],

            'is_active' => [
                'required',
                'boolean',
            ],

        ]);


        $validated['password'] = Hash::make(
            $validated['password']
        );


        $validated['created_at'] = now();


        User::create($validated);


        return redirect('/staff/list')
            ->with('success', 'Staff added successfully.');
    }
}