<?php

namespace App\Http\Requests\Admin;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class DriverRequest extends FormRequest
{
    public function authorize(): bool
    {
        return true;
    }

    public function rules(): array
    {
        $rules = [
            'first_name' => ['required', 'string', 'max:255'],
            'last_name' => ['required', 'string', 'max:255'],
            'call_sign' => ['nullable', 'string', 'max:50'],
            'email' => ['nullable', 'email', 'max:255'],
            'phone' => ['nullable', 'string', 'max:50'],
            'national_insurance_number' => ['nullable', 'string', 'max:50'],
            'date_of_birth' => ['nullable', 'date'],
            'joined_at' => ['nullable', 'date'],
            'left_at' => ['nullable', 'date'],
            'address' => ['nullable', 'string', 'max:255'],
            'status' => ['required', Rule::in(['active', 'inactive'])],

            // Optional documents uploaded alongside creation (see the Add
            // Driver wizard's second step) — a row with no file is just an
            // unused template row the JS added and gets ignored by the
            // controller, not a validation failure.
            'documents' => ['nullable', 'array'],
            'documents.*.type' => ['nullable', Rule::in(['licence', 'phv_badge', 'passport', 'biometric_residence_permit'])],
            'documents.*.document_number' => ['nullable', 'string', 'max:255'],
            'documents.*.expiry_date' => ['nullable', 'date'],
            'documents.*.file' => ['nullable', 'file', 'mimes:jpg,jpeg,png,pdf', 'max:10240'],
        ];

        if (! $this->user()->company_id) {
            $rules['company_id'] = ['required', 'exists:companies,id'];
        }

        return $rules;
    }
}
