<?php

namespace App\Http\Requests\User;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rules\Password;

class UpdateUserRequest extends FormRequest
{
    public function authorize(): bool
    {
        return $this->user()->hasAdminAccess();
    }

    public function rules(): array
    {
        $userId = $this->route('id');

        $allowedRoles = $this->user()->isSuperAdmin()
            ? 'in:super_admin,admin,user'
            : 'in:user';

        return [
            'name'     => ['sometimes', 'string', 'max:100'],
            'email'    => ['sometimes', 'email', 'max:150', "unique:users,email,{$userId}"],
            'password' => ['sometimes', Password::min(6)],
            'role'     => ['sometimes', $allowedRoles],
            'phone'    => ['nullable', 'string', 'max:20'],
        ];
    }
}
