<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class UserRegistrationRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
     */
    public function rules(): array
    {
        $provider = $this->provider;
        $rules = [
            'name' => 'required|string|max:255|regex:/^[A-Za-z][A-Za-z0-9\s._-]*$/',
        ];

        if (! $provider) {
            $rules['email'] = 'required|email|unique:users,email';
            $rules['phone_no'] = 'required|string|min:8|max:13|regex:/^[0-9+\-\s\(\)]+$/|unique:users,phone_no';
            $rules['password'] = 'required|string|min:6';
            $rules['agree'] = 'required|accepted';
        } else {
            $rules['provider'] = 'required';
            $rules['provider_id'] = 'required';
        }

        return $rules;
    }

    /**
     * Custom validation messages
     */
    public function messages(): array
    {
        return [
            // Name
            'name.required' => 'Name is required.',
            'name.string' => 'Name must be a valid string.',
            'name.max' => 'Name may not be greater than 255 characters.',
            'name.regex' => 'Name must start with a letter and can contain letters, numbers, spaces, dots, underscores, and hyphens only.',

            // Email
            'email.required' => 'Email address is required.',
            'email.email' => 'Please enter a valid email address.',
            'email.unique' => 'This email address is already registered.',

            // Phone
            'phone_no.required' => 'Phone number is required.',
            'phone_no.string' => 'Phone number must be a valid string.',
            'phone_no.regex' => 'Please enter a valid phone number with country code.',
            'phone_no.unique' => 'This phone number is already registered.',

            // Password
            'password.required' => 'Password is required.',
            'password.string' => 'Password must be a valid string.',
            'password.min' => 'Password must be at least 6 characters long.',

            // Social Login
            'provider.required' => 'Provider is required for social login.',
            'provider_id.required' => 'Provider ID is required for social login.',

            // Agreement
            'agree.required' => 'You must agree to the Terms & Conditions and Privacy Policy.',
            'agree.accepted' => 'You must accept the Terms & Conditions and Privacy Policy.',
        ];
    }
}
