<?php

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

use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Foundation\Http\FormRequest;

class AuthRegisterRequest extends FormRequest
{
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, 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] : '',
        ]);
    }
}
