<?php

namespace App\Http\Requests\Registration;

use Illuminate\Foundation\Http\FormRequest;

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

    public function rules(): array
    {
        return [
            'name'                      => ['required', 'string', 'max:191'],
            'email'                     => ['required', 'string', 'email', 'max:191', 'unique:users,email'],
            'phone'                     => ['nullable', 'string', 'max:50'],
            'address'                   => ['nullable', 'string', 'max:500'],
            'country'                   => ['nullable', 'string', 'max:100'],
            'city'                      => ['nullable', 'string', 'max:100'],
            'occupation'                => ['nullable', 'string', 'max:191'],
            'education_level'           => ['nullable', 'string', 'max:100'],
            'bio'                       => ['nullable', 'string', 'max:2000'],
            'areas_of_interest'         => ['required', 'array', 'min:1'],
            'areas_of_interest.*'       => ['string', 'max:191'],
            'skills'                    => ['nullable', 'array'],
            'skills.*'                  => ['string', 'max:191'],
            'learning_goals'            => ['required', 'string', 'max:2000'],
            'preferred_mentor_qualities'=> ['nullable', 'string', 'max:2000'],
            'time_commitment'           => ['nullable', 'string', 'max:100'],
            'motivation'                => ['required', 'string', 'max:2000'],
            // category_id references mentorship_categories table
            'category_id'               => ['nullable', 'integer', 'exists:mentorship_categories,id'],
            'password'                  => ['required', 'string', 'min:8', 'confirmed'],
            'terms_accepted'            => ['required', 'accepted'],
        ];
    }

    public function messages(): array
    {
        return [
            'name.required'              => 'Please enter your full 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>',
            'areas_of_interest.required' => 'Please select at least one area of interest.',
            'areas_of_interest.min'      => 'Please select at least one area of interest.',
            'learning_goals.required'    => 'Please describe your learning goals.',
            'motivation.required'        => 'Please describe your motivation for joining the mentorship programme.',
            'category_id.exists'         => 'The selected mentorship category is invalid.',
            '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([
            'name'  => strip_tags(trim($this->name ?? '')),
            'email' => strtolower(trim($this->email ?? '')),
        ]);
    }
}
