<?php

declare(strict_types=1);

namespace App\Request;

use App\Constants\HttpEnumCode;
use Hyperf\Validation\Request\FormRequest;

class UserRequest extends FormRequest
{
    protected array $scenes = [
        'putUserAvatar' => [ // 修改用户头像
            'avatar',
        ],
        'putUserName' => [ // 修改用户名
            'user_name',
        ],
        'addUserAddress' => [ // 添加用户收获地址
            'province_id',
            'city_id',
            'county_id',
            'address',
            'consignee_name',
            'consignee_tel',
            'consignee_zip_code',
            'is_default',
            'tag',
        ],
        'putUserAddress' => [ // 修改用户收获地址
            'province_id',
            'city_id',
            'county_id',
            'address',
            'consignee_name',
            'consignee_tel',
            'consignee_zip_code',
            'is_default',
            'tag',
        ],
    ];

    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     */
    public function rules(): array
    {
        return [
            'avatar' => 'required|url',
            'user_name' => 'required',

            'province_id' => 'required_with:city_id,county_id',
            'city_id' => 'required_with:county_id',
            'county_id' => 'required',
            'address' => 'required',
            'consignee_name' => 'required',
            'consignee_tel' => 'required',
            'is_default' => ['required','numeric','min:0','max:1'],
            'tag' => ['sometimes','required','numeric','min:1','max:4'],
        ];
    }

    /**
     * 获取已定义验证规则的错误消息.
     */
    public function messages(): array
    {
        return [
            'avatar.required' => HttpEnumCode::getMessage(HttpEnumCode::CLIENT_HTTP_ERROR),
            'avatar.url' => HttpEnumCode::getMessage(HttpEnumCode::CLIENT_HTTP_ERROR),
            'user_name.required' => HttpEnumCode::getMessage(HttpEnumCode::CLIENT_HTTP_ERROR),

            'province_id.required_with' => "请选择省份",
            'city_id.required_with' => "请选择城市",
            'county_id.required' => "请选择区县",
            'address.required' => "请输入详细地址",
            'consignee_name.required' => "请输入姓名",
            'consignee_tel.required' => "请输入电话",
            'is_default.required' => HttpEnumCode::getMessage(HttpEnumCode::CLIENT_HTTP_ERROR),
            'is_default.numeric' => HttpEnumCode::getMessage(HttpEnumCode::CLIENT_HTTP_ERROR),
            'is_default.min' => HttpEnumCode::getMessage(HttpEnumCode::CLIENT_HTTP_ERROR),
            'is_default.max' => HttpEnumCode::getMessage(HttpEnumCode::CLIENT_HTTP_ERROR),
            'tag.required' => HttpEnumCode::getMessage(HttpEnumCode::CLIENT_HTTP_ERROR),
            'tag.numeric' => HttpEnumCode::getMessage(HttpEnumCode::CLIENT_HTTP_ERROR),
            'tag.min' => HttpEnumCode::getMessage(HttpEnumCode::CLIENT_HTTP_ERROR),
            'tag.max' => HttpEnumCode::getMessage(HttpEnumCode::CLIENT_HTTP_ERROR),
        ];
    }
}
