<?php

namespace App\Http\Requests\Documents;

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

class UploadDocumentRequest extends FormRequest
{
    public function rules(): array
    {
        return [
            'document_type_id' => ['required', 'uuid', Rule::exists('document_types', 'uuid')->whereNull('deleted_at')],
            // Size and MIME are enforced against the document type in storage;
            // this is the outer guard rail.
            'file' => ['required', 'file', 'max:'.config('mams.documents.max_file_size_kb')],
            'document_number' => ['nullable', 'string', 'max:100'],
            'issue_date' => ['nullable', 'date'],
            'expiry_date' => ['nullable', 'date', 'after:issue_date'],
            'issuing_country' => ['nullable', 'string', 'size:2'],
            'issuer' => ['nullable', 'string', 'max:255'],
        ];
    }

    /** @return array<string, mixed> */
    public function metadata(): array
    {
        return [
            'document_number' => $this->input('document_number'),
            'issue_date' => $this->input('issue_date'),
            'expiry_date' => $this->input('expiry_date'),
            'issuing_country' => $this->filled('issuing_country')
                ? strtoupper($this->string('issuing_country')->toString())
                : null,
            'issuer' => $this->input('issuer'),
        ];
    }
}
