<?php

namespace App\Modules\HR\Leave\Requests;

use App\Modules\HR\Leave\Models\LeaveType;
use Illuminate\Foundation\Http\FormRequest;

class StoreLeaveTypeRequest extends FormRequest
{
    public function authorize(): bool
    {
        return $this->user()?->can('create', LeaveType::class) ?? false;
    }

    /**
     * @return array<string, mixed>
     */
    public function rules(): array
    {
        return $this->baseRules();
    }

    protected function prepareForValidation(): void
    {
        $this->merge([
            'is_paid' => $this->boolean('is_paid'),
            'requires_approval' => $this->boolean('requires_approval', true),
            'allow_half_day' => $this->boolean('allow_half_day'),
            'allow_negative_balance' => $this->boolean('allow_negative_balance'),
            'requires_attachment' => $this->boolean('requires_attachment'),
            'allow_carry_forward' => $this->boolean('allow_carry_forward'),
            'allow_hourly' => $this->boolean('allow_hourly'),
            'is_active' => $this->boolean('is_active', true),
        ]);
    }

    /**
     * @return array<string, mixed>
     */
    protected function baseRules(): array
    {
        return [
            'name' => ['required', 'string', 'max:120'],
            'is_paid' => ['required', 'boolean'],
            'annual_limit' => ['nullable', 'integer', 'min:0', 'max:365'],
            'requires_approval' => ['required', 'boolean'],
            'allow_half_day' => ['required', 'boolean'],
            'allow_negative_balance' => ['required', 'boolean'],
            'requires_attachment' => ['required', 'boolean'],
            'allow_carry_forward' => ['required', 'boolean'],
            'carry_forward_max_days' => ['nullable', 'numeric', 'min:0', 'max:365'],
            'allow_hourly' => ['required', 'boolean'],
            'max_hourly_hours' => ['nullable', 'numeric', 'min:0.25', 'max:24'],
            'is_active' => ['sometimes', 'boolean'],
        ];
    }
}
