<?php

namespace App\Modules\HR\PermissionRequests\Requests;

use App\Modules\HR\PermissionRequests\Support\PermissionRequestType;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

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

    /**
     * @return array<string, mixed>
     */
    public function rules(): array
    {
        $tenantId = $this->user()?->tenant_id;

        return [
            'employee_id' => [
                'required',
                'integer',
                Rule::exists('employees', 'id')->where(fn ($q) => $q->where('tenant_id', $tenantId)),
            ],
            'type' => ['required', 'string', Rule::in(PermissionRequestType::all())],
            'request_date' => ['required', 'date'],
            'start_at' => ['nullable', 'date'],
            'end_at' => ['nullable', 'date', 'after_or_equal:start_at'],
            'reason' => ['nullable', 'string', 'max:5000'],
        ];
    }
}
