<?php

namespace App\Http\Controllers\Admin\Hr;

use App\Enums\EmploymentStatus;
use App\Enums\Gender;
use App\Http\Controllers\Concerns\FlashesEntityMessage;
use App\Http\Controllers\Concerns\ParsesDataTableQuery;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\Hr\EmployeeRequest;
use App\Http\Requests\Admin\ToggleStatusRequest;
use App\Models\User;
use App\Services\Hr\EmployeeService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;

class EmployeeController extends Controller
{
    use FlashesEntityMessage, ParsesDataTableQuery;

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

    public function index(Request $request): Response
    {
        $tableQuery = $this->parseDataTableQuery($request);
        $role = $request->string('role')->toString();
        $gender = $request->string('gender')->toString();
        $employmentStatus = $request->string('employment_status')->toString();
        $isActive = $request->string('is_active')->toString();

        return Inertia::render('admin/hr/employees/Index', [
            'employees' => $this->employeeService->paginateForIndex(
                $tableQuery,
                $role,
                $gender,
                $employmentStatus,
                $isActive
            ),
            'filters' => $this->dataTableFilters($tableQuery, [
                'role' => $role,
                'gender' => $gender,
                'employment_status' => $employmentStatus,
                'is_active' => $isActive,
            ]),
            'roles' => $this->employeeService->assignableRoleOptions(),
            'genders' => Gender::selectOptions(),
            'employmentStatuses' => EmploymentStatus::selectOptions(),
        ]);
    }

    public function create(): Response
    {
        return Inertia::render('admin/hr/employees/Create', [
            'genders' => Gender::selectOptions(),
            'employmentStatuses' => EmploymentStatus::selectOptions(),
            'roles' => $this->employeeService->assignableRoleOptions(),
        ]);
    }

    public function store(EmployeeRequest $request): RedirectResponse
    {
        $this->employeeService->create($request->validated());

        $this->flashCreated('Pegawai');

        return redirect()->route('admin.hr.employees.index');
    }

    public function edit(User $user): Response
    {
        $editData = $this->employeeService->findForEdit($user);

        return Inertia::render('admin/hr/employees/Edit', [
            'genders' => Gender::selectOptions(),
            'employmentStatuses' => EmploymentStatus::selectOptions(),
            'roles' => $this->employeeService->assignableRoleOptions(),
            'employee' => $editData['employee'],
            'profilePhoto' => $editData['profilePhoto'],
        ]);
    }

    public function update(EmployeeRequest $request, User $user): RedirectResponse
    {
        $this->employeeService->update($user, $request->validated());

        $this->flashUpdated('Pegawai');

        return redirect()->route('admin.hr.employees.index');
    }

    public function destroy(User $user): RedirectResponse
    {
        $this->employeeService->delete($user);

        $this->flashDeleted('Pegawai');

        return redirect()->route('admin.hr.employees.index');
    }

    public function toggleStatus(ToggleStatusRequest $request, User $user): RedirectResponse
    {
        $this->employeeService->toggleStatus($user, $request->validated());

        $this->flashStatusUpdated('Pegawai');

        return redirect()->route('admin.hr.employees.index');
    }

    public function resetPassword(User $user): RedirectResponse
    {
        $this->employeeService->resetPassword($user);

        $this->flashSuccess('Kata sandi pegawai berhasil direset.');

        return redirect()->route('admin.hr.employees.index');
    }
}
