<?php

namespace App\Http\Requests\Auth;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Rules\Password;

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

    public function rules(): array
    {
        return [
            'display_name' => ['required', 'string', 'max:255'],
            'username' => [
                'required',
                'string',
                'min:3',
                'max:30',
                'alpha_dash',
                Rule::unique('users', 'username'),
            ],
            'email' => ['required', 'string', 'lowercase', 'email', 'max:255', Rule::unique('users', 'email')],
            'password' => ['required', 'confirmed', Password::defaults()],
            'country_code' => ['required', 'string', 'size:2', 'alpha'],
            'phone_country_code' => ['nullable', 'required_with:phone', 'string', 'max:5'],
            'phone' => ['nullable', 'string', 'max:32'],
            'preferred_locale' => ['required', 'string', Rule::in(config('esports.supported_locales', ['en', 'ar']))],
            'timezone' => ['required', 'string', 'timezone:all'],
            'terms' => ['accepted'],
        ];
    }

    protected function prepareForValidation(): void
    {
        $this->merge([
            'country_code' => strtoupper((string) $this->input('country_code')),
            'preferred_locale' => $this->input('preferred_locale', config('esports.default_locale', 'en')),
            'timezone' => $this->input('timezone', config('app.timezone', 'UTC')),
        ]);
    }
}
