<?php

namespace Kylin\User\Requests;

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

/**
 * 创建用户请求验证
 *
 * 验证创建用户时的输入参数。
 *
 * 合规要求：
 *  - 依据《未成年人保护法》第七十二条，14岁以下用户需监护人同意
 *  - 依据等保 2.0 (GB/T 22239)，密码复杂度要求
 */
class StoreUserRequest extends FormRequest
{
    public function authorize(): bool
    {
        return true;
    }

    public function rules(): array
    {
        return [
            'name' => ['required', 'string', 'max:50'],
            'email' => ['required', 'email', 'max:100', 'unique:users,email'],
            'phone' => ['nullable', 'string', 'max:20', 'unique:users,phone'],
            'password' => ['required', 'string', 'min:8', 'confirmed'],
            'roles' => ['nullable', 'array'],
            'roles.*' => ['string', 'exists:kylin_roles,name'],
            // 未成年人保护：14岁以下需监护人同意
            'birthday' => ['nullable', 'date', 'before:today'],
            'guardian_consent' => [
                'boolean',
                Rule::requiredIf(function () {
                    $birthday = $this->input('birthday');
                    if (! $birthday) {
                        return false;
                    }

                    $age = \Carbon\Carbon::parse($birthday)->age;
                    return $age < 14;
                }),
            ],
            'guardian_name' => [
                'nullable',
                'string',
                'max:50',
                Rule::requiredIf(function () {
                    return $this->input('guardian_consent');
                }),
            ],
            'guardian_phone' => [
                'nullable',
                'string',
                'max:20',
                Rule::requiredIf(function () {
                    return $this->input('guardian_consent');
                }),
            ],
        ];
    }

    /**
     * 验证后处理
     *
     * 监护人同意标记必须为 true 才通过。
     */
    public function withValidator($validator): void
    {
        $validator->after(function ($validator) {
            $birthday = $this->input('birthday');

            if (! $birthday) {
                return;
            }

            $age = \Carbon\Carbon::parse($birthday)->age;

            if ($age < 14 && ! $this->boolean('guardian_consent')) {
                $validator->errors()->add(
                    'guardian_consent',
                    '根据《未成年人保护法》，未满14周岁用户注册需监护人同意。'
                );
            }
        });
    }

    public function messages(): array
    {
        return [
            'name.required' => '用户名不能为空',
            'email.required' => '邮箱不能为空',
            'email.unique' => '邮箱已存在',
            'phone.unique' => '手机号已存在',
            'password.required' => '密码不能为空',
            'password.min' => '密码至少8位',
            'password.confirmed' => '两次密码不一致',
            'birthday.before' => '生日日期无效',
            'guardian_consent.required' => '未满14周岁用户注册需监护人同意',
            'guardian_name.required' => '监护人姓名不能为空',
            'guardian_phone.required' => '监护人手机号不能为空',
        ];
    }
}