<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

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

    public function rules(): array
    {
        return [
            'to_recipients' => ['required', 'array', 'min:1'],
            'to_recipients.*.email' => ['required', 'email', 'max:191'],
            'to_recipients.*.name' => ['nullable', 'string', 'max:191'],
            'cc_recipients' => ['nullable', 'array'],
            'cc_recipients.*.email' => ['required', 'email', 'max:191'],
            'bcc_recipients' => ['nullable', 'array'],
            'bcc_recipients.*.email' => ['required', 'email', 'max:191'],
            'subject' => ['required', 'string', 'max:191'],
            'html_body' => ['required', 'string'],
            'text_body' => ['nullable', 'string'],
            'template_id' => ['nullable', 'integer', 'exists:email_templates,id'],
            'scheduled_at' => ['nullable', 'date', 'after:now'],
            'priority' => ['nullable', 'in:low,normal,high'],
        ];
    }

    public function messages(): array
    {
        return [
            'to_recipients.required' => 'Please select at least one recipient.',
            'to_recipients.*.email.email' => 'Please enter a valid email address.',
            'subject.required' => 'Please enter a subject.',
            'html_body.required' => 'Please enter your message.',
            'scheduled_at.after' => 'Scheduled time must be in the future.',
        ];
    }
}
