<?php

namespace App\Http\Requests;

use App\Concerns\EmployeeDocumentValidationRules;
use App\Models\TemporaryEmployee;
use App\Models\TemporaryEmployeeContract;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class UpdateTemporaryEmployeeContractRequest extends FormRequest
{
    use EmployeeDocumentValidationRules;

    public function authorize(): bool
    {
        return (bool) $this->user()?->can('temporary-contracts.update');
    }

    /**
     * @return array<string, array<int, mixed>>
     */
    public function rules(): array
    {
        /** @var TemporaryEmployee $employee */
        $employee = $this->route('temporaryEmployee');

        /** @var TemporaryEmployeeContract $contract */
        $contract = $this->route('contract');

        return [
            'temporary_employee_id' => [
                'required',
                'integer',
                Rule::in([$employee->id]),
            ],
            'from_date' => ['required', 'date'],
            'to_date' => ['required', 'date', 'after_or_equal:from_date'],
            'daily_amount' => ['required', 'numeric', 'min:0'],
            'contract' => $this->employeeDocumentRules(required: $contract->contract_path === null),
        ];
    }
}
