<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class IncomingEmailRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
     */
    public function rules(): array
    {
        return [
            'hash' => ['required', 'string', 'max:64'],
            'metadata.recipientEmail' => ['required', 'email', 'max:255'],
            'metadata.recipientName' => ['nullable', 'string', 'max:255'],
            'metadata.senderEmail' => ['required', 'email', 'max:255'],
            'metadata.senderName' => ['nullable', 'string', 'max:255'],
            'metadata.domain' => ['required', 'string', 'max:255'],
            'metadata.subject' => ['nullable', 'string', 'max:500'],
            'metadata.received_at' => ['required', 'date'],
            'metadata.attachments' => ['nullable', 'array'],
            'metadata.attachments.*.filename' => ['required_with:metadata.attachments', 'string'],
            'metadata.attachments.*.mimeType' => ['required_with:metadata.attachments', 'string'],
            'metadata.attachments.*.size' => ['required_with:metadata.attachments', 'integer'],
            'metadata.attachmentSize' => ['nullable', 'integer', 'min:0'],
            'bodyText' => ['nullable', 'string'],
            'bodyHtml' => ['nullable', 'string'],
        ];
    }

    /**
     * Custom error messages.
     *
     * @return array<string, string>
     */
    public function messages(): array
    {
        return [
            'hash.required' => 'The email hash identifier is required.',
            'metadata.recipientEmail.required' => 'A recipient email address is required.',
            'metadata.senderEmail.required' => 'A sender email address is required.',
            'metadata.domain.required' => 'The recipient domain is required.',
            'metadata.received_at.required' => 'The email received timestamp is required.',
        ];
    }
}
