<?php

namespace Kylin\User\Requests;

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

/**
 * 更新用户请求验证
 *
 * 验证更新用户时的输入参数。
 */
class UpdateUserRequest extends FormRequest
{
    public function authorize(): bool
    {
        return true;
    }

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

        return [
            'name' => ['sometimes', 'string', 'max:50'],
            'email' => ['sometimes', 'email', 'max:100', Rule::unique('users', 'email')->ignore($userId)],
            'phone' => ['nullable', 'string', 'max:20', Rule::unique('users', 'phone')->ignore($userId)],
            'password' => ['nullable', 'string', 'min:8', 'confirmed'],
            'status' => ['sometimes', 'integer', 'in:0,1,2'],
            'roles' => ['nullable', 'array'],
            'roles.*' => ['string', 'exists:kylin_roles,name'],
        ];
    }

    public function messages(): array
    {
        return [
            'name.max' => '用户名不能超过50个字符',
            'email.unique' => '邮箱已存在',
            'phone.unique' => '手机号已存在',
            'password.min' => '密码至少8位',
            'password.confirmed' => '两次密码不一致',
        ];
    }
}
