<?php

namespace App\Http\Requests\Registration;

use Illuminate\Foundation\Http\FormRequest;

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

    public function rules(): array
    {
        return [
            'first_name'          => ['required', 'string', 'max:191'],
            'last_name'           => ['required', 'string', 'max:191'],
            'email'               => ['required', 'string', 'email', 'max:191', 'unique:users,email'],
            'phone'               => ['nullable', 'string', 'max:50'],
            'address'             => ['nullable', 'string', 'max:500'],
            'city'                => ['nullable', 'string', 'max:100'],
            'country'             => ['nullable', 'string', 'max:100'],
            'organization'        => ['nullable', 'string', 'max:191'],
            'marketing_focus'     => ['required', 'array', 'min:1'],
            'marketing_focus.*'   => ['string', 'max:191'],
            'target_audience'     => ['required', 'string', 'max:2000'],
            'campaign_experience' => ['nullable', 'string', 'max:3000'],
            'years_of_experience' => ['nullable', 'integer', 'min:0', 'max:60'],
            'bio'                 => ['required', 'string', 'max:3000'],
            // Optional social media links
            'facebook'            => ['nullable', 'url', 'max:500'],
            'twitter'             => ['nullable', 'url', 'max:500'],
            'linkedin'            => ['nullable', 'url', 'max:500'],
            'instagram'           => ['nullable', 'url', 'max:500'],
            'password'            => ['required', 'string', 'min:8', 'confirmed'],
            'terms_accepted'      => ['required', 'accepted'],
        ];
    }

    public function messages(): array
    {
        return [
            'first_name.required'        => 'Please enter your first name.',
            'last_name.required'         => 'Please enter your last name.',
            'email.required'             => 'Please enter your email address.',
            'email.email'                => 'Please enter a valid email address.',
            'email.unique'               => 'This email is already registered. <a href="' . route('login') . '" class="underline">Login instead?</a>',
            'marketing_focus.required'   => 'Please select at least one marketing focus area.',
            'marketing_focus.min'        => 'Please select at least one marketing focus area.',
            'target_audience.required'   => 'Please describe your target audience.',
            'bio.required'               => 'Please provide a professional bio.',
            'years_of_experience.integer'=> 'Years of experience must be a number.',
            'years_of_experience.min'    => 'Years of experience cannot be negative.',
            'facebook.url'               => 'Please enter a valid Facebook URL.',
            'twitter.url'                => 'Please enter a valid Twitter/X URL.',
            'linkedin.url'               => 'Please enter a valid LinkedIn URL.',
            'instagram.url'              => 'Please enter a valid Instagram URL.',
            'password.required'          => 'Please create a password.',
            'password.min'               => 'Password must be at least 8 characters.',
            'password.confirmed'         => 'Password confirmation does not match.',
            'terms_accepted.required'    => 'You must accept the terms and conditions.',
            'terms_accepted.accepted'    => 'You must accept the terms and conditions.',
        ];
    }

    protected function prepareForValidation(): void
    {
        $this->merge([
            'first_name' => strip_tags(trim($this->first_name ?? '')),
            'last_name'  => strip_tags(trim($this->last_name ?? '')),
            'email'      => strtolower(trim($this->email ?? '')),
        ]);
    }
}
