<?php

namespace App\Http\Controllers\Admin;

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

class EmployeeController extends Controller
{
    use \Illuminate\Foundation\Auth\Access\AuthorizesRequests;

    public function __construct(public \App\Services\EmployeeService $service) {}

    public function index(Request $request)
    {
        // Permission check can be handled by middleware or policy, or here manually if preferred.
        // Assuming route middleware or simple check in view/nav.
        // Or if strictly using AuthorizesRequests:
        // $this->authorize('viewAny', User::class); // Reusing User policy?

        $employees = $this->service->getAllEmployees(
            $request->only(['search', 'department', 'status']),
            15
        );
        $departments = $this->service->getDepartments();

        return inertia('Admin/Employees/Index', [
            'employees' => $employees,
            'departments' => $departments,
            'filters' => $request->only(['search', 'department', 'status']),
        ]);
    }

    public function create()
    {
        return inertia('Admin/Employees/Create', [
            // Pass any needed data like manager list if select is used
            'managers' => \App\Models\User::where('type', \App\Enums\UserType::EMPLOYEE)->select('id', 'name')->get(),
            'departments' => \App\Models\Department::where('status', true)->select('id', 'name')->get(),
        ]);
    }

    public function store(\App\Http\Requests\Admin\StoreEmployeeRequest $request)
    {
        $this->service->createEmployee($request->validated());

        return redirect()->route('admin.employees.index')->with('success', 'Employee created successfully.');
    }

    public function show(\App\Models\User $employee)
    {
        $employee->load('employeeProfile.manager', 'employeeProfile.departmentRel');

        return inertia('Admin/Employees/Show', [
            'employee' => $employee,
        ]);
    }

    public function edit(\App\Models\User $employee)
    {
        $employee->load('employeeProfile');

        return inertia('Admin/Employees/Edit', [
            'employee' => $employee,
            'managers' => \App\Models\User::where('type', \App\Enums\UserType::EMPLOYEE)->where('id', '!=', $employee->id)->select('id', 'name')->get(),
            'departments' => \App\Models\Department::where('status', true)->select('id', 'name')->get(),
        ]);
    }

    public function update(\App\Http\Requests\Admin\UpdateEmployeeRequest $request, \App\Models\User $employee)
    {
        $this->service->updateEmployee($employee, $request->validated());

        return redirect()->route('admin.employees.index')->with('success', 'Employee updated successfully.');
    }

    public function destroy(\App\Models\User $employee)
    {
        // Soft delete logic usually
        $employee->delete();

        return redirect()->back()->with('success', 'Employee deleted successfully.');
    }
}
