<?php

namespace App\Http\Requests\Auth;

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

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

    protected function prepareForValidation(): void
    {
        $mobile = preg_replace('/\D+/', '', (string) $this->input('mobilenumber'));

        $this->merge([
            'mobilenumber' => $mobile,
            'email' => $mobile !== '' ? $mobile . '@justexamleague.com' : null,
            'schoolcode' => trim((string) $this->input('schoolcode', 'jel_215617')),
        ]);
    }

    public function rules(): array
    {
        return [
            'username' => ['required', 'string', 'max:100'],
            'mobilenumber' => ['required', 'regex:/^[6-9][0-9]{9}$/', Rule::unique('users', 'mobileno')],
            'email' => ['required', 'email', Rule::unique('users', 'email')],
            'password' => ['required', 'string', 'min:6', 'max:50', 'same:confirmpassword'],
            'confirmpassword' => ['required', 'string', 'min:6', 'max:50'],
            'schoolcode' => ['nullable', 'string', 'max:100'],
        ];
    }
}
