<?php

namespace App\Modules\HR\Attendance\Requests;

use App\Modules\HR\Attendance\Models\AttendanceRecord;
use Illuminate\Foundation\Http\FormRequest;

class UpdateAttendanceCheckoutRequest extends FormRequest
{
    public function authorize(): bool
    {
        $record = $this->route('record');

        if (! $record instanceof AttendanceRecord) {
            return false;
        }
        $user = $this->user();
        if ($user === null) {
            return false;
        }
        if ($record->check_out_at === null) {
            return $user->can('registerCheckout', $record);
        }

        return $user->can('updateCheckout', $record);
    }

    /**
     * @return array<string, mixed>
     */
    public function rules(): array
    {
        return [
            'check_out_at' => ['required', 'date'],
        ];
    }
}
