<?php

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

use Illuminate\Foundation\Http\FormRequest;

class AuthRegisterRequest extends FormRequest
{
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array|string>
     */
    public function rules(): array
    {
        return [
            /**
             * @example 61929248
             */
            'phone_number' => ['required', 'integer', 'between:61000000,71999999', 'unique:users,phone_number'],

            /**
             * @example Nurmuhammet Allanov
             */
            'name' => ['required', 'string', 'max:255'],

            /**
             * @example 75 3rd Ave, New York, NY 10003, USA
             */
            '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 ? implode(' ', array_slice($name, 1)) : '',
        ]);
    }
}
