<?php

namespace App\Http\Requests\Api;

use App\Rules\LandlineNumber;
use Illuminate\Foundation\Http\FormRequest;

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

    public function rules(): array
    {
        return [
            'reference' => ['nullable', 'string', 'max:255'],

            'title' => [
                'nullable',
                'string',
                'max:50',
                'regex:/^[\pL\'\-\s]+$/u',
            ],

            'first_name' => [
                'nullable',
                'string',
                'min:2',
                'max:50',
                'regex:/^[\pL\'\-\s]+$/u',
            ],

            'middle_name' => [
                'nullable',
                'string',
                'min:2',
                'max:50',
                'regex:/^[\pL\'\-\s]+$/u',
            ],

            'last_name' => [
                'nullable',
                'string',
                'min:2',
                'max:50',
                'regex:/^[\pL\'\-\s]+$/u',
            ],

            'full_name' => [
                'nullable',
                'string',
                'min:2',
                'max:100',
                'regex:/^[\pL\'\-\s]+$/u',
            ],

            'preferred_name' => [
                'nullable',
                'string',
                'min:2',
                'max:50',
                'regex:/^[\pL\'\-\s]+$/u',
            ],

            'dob' => [
                'nullable',
                'date',
                'before:-18 years',
                'after:-120 years',
            ],

            'email' => [
                'required',
                'email:rfc,dns',
                'unique:clients,email_address',
            ],

            'phone_number' => [
                'nullable',
                'required_without:landline_number',
                'regex:/^(\+44\s?7\d{3}|\(?07\d{3}\)?)\s?\d{3}\s?\d{3}$/',
                'unique:clients,phone_number',
            ],

            'landline_number' => [
                'nullable',
                'required_without:phone_number',
                new LandlineNumber,
            ],

            /*
             * Care information
             */
            'what_type_of_care_were_you_in' => [
                'nullable',
                'string',
                'max:1000',
            ],

            'care_eligibility_responses' => [
                'nullable',
                'array',
            ],

            'care_eligibility_responses.*' => [
                'nullable',
                'boolean',
            ],

            /*
             * Additional information
             */
            'anything_you_wish_to_tell_us' => [
                'nullable',
                'string',
                'max:5000',
            ],

            /*
             * Callback preferences
             */
            'preferred_callback_time' => [
                'nullable',
                'date_format:H:i',
            ],

            'preferred_callback_daypart' => [
                'nullable',
                'string',
                'in:morning,afternoon,evening,anytime',
            ],

            /*
             * Consent
             */
            'privacy_consent' => [
                'nullable',
            ],

            'terms_consent' => [
                'nullable',
            ],

            /*
             * Submission metadata
             */
            'submitted_at' => [
                'nullable',
                'date',
            ],

            'collection_id' => [
                'nullable',
                'string',
                'max:255',
            ],

            'external_reference_id' => [
                'nullable',
                'string',
                'max:255',
            ],
        ];
    }
}
