<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Role;
use App\Models\User;
use App\Services\EmployeeService;
use App\Traits\LogsActivity;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;

class EmployeeController extends Controller
{
    use LogsActivity;

    public function __construct(private readonly EmployeeService $employeeService)
    {
    }

    public function index(Request $request): View
    {
        return view('admin.employees.index', [
            'employees' => $this->employeeService->listEmployees(12, $request->only(['department', 'status', 'q'])),
        ]);
    }

    public function create(): View
    {
        return view('admin.employees.create', ['roles' => Role::query()->where('slug', '!=', 'super_admin')->get()]);
    }

    public function store(Request $request): RedirectResponse
    {
        $data = $request->validate([
            'name' => 'required|string|max:255',
            'email' => 'required|email|unique:users,email',
            'mobile' => 'nullable|string|max:20',
            'password' => 'required|string|min:8',
            'status' => 'required|string',
            'department' => 'nullable|string|max:100',
            'skills' => 'nullable|string|max:255',
            'experience_years' => 'nullable|integer|min:0',
            'salary' => 'nullable|numeric|min:0',
            'joining_date' => 'nullable|date',
            'role_ids' => 'nullable|array',
            'role_ids.*' => 'exists:roles,id',
        ]);

        $employee = $this->employeeService->createEmployee($data);
        $this->logActivity('create', 'employee', 'Employee created', ['employee_id' => $employee->id]);

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

    public function edit(User $employee): View
    {
        return view('admin.employees.edit', ['employee' => $employee->load(['employeeProfile', 'roles']), 'roles' => Role::query()->where('slug', '!=', 'super_admin')->get()]);
    }

    public function update(Request $request, User $employee): RedirectResponse
    {
        $data = $request->validate([
            'name' => 'required|string|max:255',
            'email' => 'required|email|unique:users,email,'.$employee->id,
            'mobile' => 'nullable|string|max:20',
            'password' => 'nullable|string|min:8',
            'status' => 'required|string',
            'department' => 'nullable|string|max:100',
            'skills' => 'nullable|string|max:255',
            'experience_years' => 'nullable|integer|min:0',
            'salary' => 'nullable|numeric|min:0',
            'joining_date' => 'nullable|date',
            'role_ids' => 'nullable|array',
            'role_ids.*' => 'exists:roles,id',
        ]);

        $this->employeeService->updateEmployee($employee, $data);
        $this->logActivity('update', 'employee', 'Employee updated', ['employee_id' => $employee->id]);

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

    public function destroy(User $employee): RedirectResponse
    {
        $this->employeeService->deleteEmployee($employee);
        $this->logActivity('delete', 'employee', 'Employee soft deleted', ['employee_id' => $employee->id]);

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