<?php

namespace App\Http\Requests\Identity;

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

class StoreUserRequest extends FormRequest
{
    public function rules(): array
    {
        return [
            'name' => ['required', 'string', 'max:255'],
            'email' => ['required', 'email', 'max:255', Rule::unique('users', 'email')],
            'phone' => ['nullable', 'string', 'max:32'],
            'branch_id' => ['nullable', 'uuid', Rule::exists('branches', 'uuid')->whereNull('deleted_at')],
            'department_id' => ['nullable', 'uuid', Rule::exists('departments', 'uuid')->whereNull('deleted_at')],
            'team_id' => ['nullable', 'uuid', Rule::exists('teams', 'uuid')->whereNull('deleted_at')],
            'locale' => ['nullable', 'string', 'max:10'],
            // At least one role: an account with none can sign in and do nothing.
            'roles' => ['required', 'array', 'min:1'],
            'roles.*' => ['string', Rule::exists('roles', 'name')],
        ];
    }

    public function messages(): array
    {
        return [
            'roles.required' => 'Give the user at least one role.',
        ];
    }
}
