<?php

namespace App\Http\Requests\Registration;

use Illuminate\Foundation\Http\FormRequest;

class StoreTrainerRegistrationRequest 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'               => ['required', 'string', 'max:50'],
            'address'             => ['nullable', 'string', 'max:500'],
            'city'                => ['nullable', 'string', 'max:100'],
            'country'             => ['nullable', 'string', 'max:100'],
            'professional_title'  => ['required', 'string', 'max:191'],
            'specialization'      => ['required', 'string', 'max:191'],
            'years_of_experience' => ['required', 'integer', 'min:0', 'max:60'],
            'certifications'      => ['nullable', 'string', 'max:2000'],
            'bio'                 => ['required', 'string', 'max:3000'],
            'expertise_areas'     => ['required', 'array', 'min:1'],
            'expertise_areas.*'   => ['string', 'max:191'],
            'availability'        => ['required', 'string', 'max:100'],
            // CV file: pdf, doc, docx, max 5MB
            'cv'                  => ['nullable', 'file', 'mimes:pdf,doc,docx', 'max:5120'],
            '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>',
            'phone.required'               => 'Please enter your phone number.',
            'professional_title.required'  => 'Please enter your professional title.',
            'specialization.required'      => 'Please enter your area of specialization.',
            'years_of_experience.required' => 'Please enter your years of experience.',
            'years_of_experience.integer'  => 'Years of experience must be a number.',
            'years_of_experience.min'      => 'Years of experience cannot be negative.',
            'bio.required'                 => 'Please provide a professional bio.',
            'expertise_areas.required'     => 'Please select at least one area of expertise.',
            'expertise_areas.min'          => 'Please select at least one area of expertise.',
            'availability.required'        => 'Please indicate your availability.',
            'cv.file'                      => 'Please upload a valid file.',
            'cv.mimes'                     => 'CV must be a PDF, DOC, or DOCX file.',
            'cv.max'                       => 'CV file size must not exceed 5MB.',
            '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 ?? '')),
        ]);
    }
}
