<?php

namespace App\Http\Controllers\Api\V1\Auth\Register;

use Illuminate\Foundation\Http\FormRequest;

class AuthRegisterRequest extends FormRequest
{
    /**
     * Prepare the data for validation.
     */
    protected function prepareForValidation(): void
    {
        $this->merge([
            'phone_number' => normalizeTurkmenPhoneNumber($this->phone_number),
        ]);
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array|string>
     */
    public function rules(): array
    {
        return [
            'phone_number' => ['required', 'integer', 'between:61000000,71999999', 'unique:users,phone_number'],
            'name' => ['required', 'string', 'max:255'],
            'address' => ['required', 'string', 'max:255'],
        ];
    }

    /**
     * Handle a passed validation attempt.
     */
    protected function passedValidation(): void
    {
        $name = explode(' ', $this->name);

        $this->merge([
            'first_name' => $name[0],
            'last_name' => count($name) > 1 ? $name[0] : '',
        ]);
    }
}
