<?php

namespace App\Http\Requests\Api;

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

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

    /** @return array<string, mixed> */
    public function rules(): array
    {
        return [
            'name' => ['required', 'string', 'max:255'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users,email'],
            'password' => ['required', 'string', 'min:8', 'confirmed'],
            'phone' => ['nullable', 'string', 'max:20'],
            'role' => ['nullable', 'string', Rule::in([\App\Models\User::ROLE_CLIENT, \App\Models\User::ROLE_LIVREUR])],
        ];
    }

    protected function prepareForValidation(): void
    {
        if (! $this->has('role')) {
            $this->merge(['role' => \App\Models\User::ROLE_CLIENT]);
        }
    }
}
