<?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'),
            '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'],
            'is_active' => ['sometimes', 'boolean'],
        ];
    }
}
