<?php

namespace App\Http\Controllers\Admin\HR;

use App\Enums\LeaveRequestStatus;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\HR\LeaveRequestRequest;
use App\Http\Requests\PaginatedRequest;
use App\Models\LeaveRequest;
use App\Services\Admin\HR\LeaveRequestService;
use Illuminate\Http\RedirectResponse;
use Inertia\Inertia;
use Inertia\Response;

class LeaveRequestController extends Controller
{
    public function __construct(
        private LeaveRequestService $service
    ) {}

    public function index(PaginatedRequest $request): Response
    {
        return Inertia::render('admin/hr/leave-request/index', [
            'leaveRequests' => $this->service->paginated(
                ...$request->validatedWithDefaults(),
                filters: $request->only(['status']),
            ),
            'filters' => $request->only(['status']),
            'filterOptions' => [
                'statusOptions' => LeaveRequestStatus::toSelect(),
            ],
            'currentUserId' => $request->user()->id,
        ]);
    }

    public function store(LeaveRequestRequest $request): RedirectResponse
    {
        return $this->handleAction(
            fn () => $this->service->store($request->validated()),
            'Permohonan cuti berhasil ditambahkan.',
            'admin.hr.leave-requests.index'
        );
    }

    public function update(LeaveRequestRequest $request, LeaveRequest $leaveRequest): RedirectResponse
    {
        return $this->handleAction(
            fn () => $this->service->update($leaveRequest, $request->validated()),
            'Permohonan cuti berhasil diperbarui.',
            'admin.hr.leave-requests.index'
        );
    }

    public function destroy(LeaveRequest $leaveRequest): RedirectResponse
    {
        return $this->handleAction(
            fn () => $this->service->destroy($leaveRequest),
            'Permohonan cuti berhasil dihapus.',
            'admin.hr.leave-requests.index'
        );
    }

    public function approve(LeaveRequest $leaveRequest): RedirectResponse
    {
        return $this->handleAction(
            fn () => $this->service->approve($leaveRequest),
            'Permohonan cuti berhasil disetujui.',
            'admin.hr.leave-requests.index'
        );
    }

    public function reject(LeaveRequest $leaveRequest): RedirectResponse
    {
        return $this->handleAction(
            fn () => $this->service->reject($leaveRequest),
            'Permohonan cuti berhasil ditolak.',
            'admin.hr.leave-requests.index'
        );
    }
}
