<?php

declare(strict_types=1);

namespace app\admin\validate\split;

use app\admin\model\split\Link;
use app\admin\model\split\Number as NumberModel;
use think\Validate;

/**
 * 分流号码验证器（部署源文件，由 deploy_split_number.php 复制到 application）
 */
class Number extends Validate
{
    protected $rule = [
        'number_type'        => 'require|checkNumberType',
        'number_type_custom' => 'max:50|checkNumberTypeCustom',
        'split_link_id'      => 'require|integer|gt:0|checkSplitLink',
        'ticket_name'        => 'require|max:100',
        'numbers'            => 'require|checkNumbers',
        'number'             => 'require|max:50',
        'status'             => 'require|checkStatus',
    ];

    protected $message = [
        'number_type.require'   => '请选择号码类型',
        'split_link_id.require' => '请选择分流链接',
        'ticket_name.require'   => '请填写工单名称',
        'ticket_name.max'       => '工单名称不能超过100个字符',
        'numbers.require'       => '请填写号码',
        'number.require'        => '请填写号码',
        'number.max'            => '号码不能超过50个字符',
        'status.require'        => '请选择状态',
    ];

    protected $scene = [
        'add'  => ['number_type', 'number_type_custom', 'split_link_id', 'ticket_name', 'numbers', 'status'],
        'edit' => ['number_type', 'number_type_custom', 'split_link_id', 'ticket_name', 'number', 'status'],
    ];

    /**
     * @param mixed $value
     * @return bool|string
     */
    protected function checkNumberType($value)
    {
        $key = (string) $value;
        $list = (new NumberModel())->getNumberTypeList();
        return isset($list[$key]) ? true : '号码类型无效';
    }

    /**
     * @param mixed $value
     * @param mixed $rule
     * @param array $data
     * @return bool|string
     */
    protected function checkNumberTypeCustom($value, $rule, array $data = [])
    {
        if (($data['number_type'] ?? '') !== 'custom') {
            return true;
        }
        if (trim((string) $value) === '') {
            return '请填写自定义号码类型';
        }
        return true;
    }

    /**
     * @param mixed $value
     * @param mixed $rule
     * @param array $data
     * @return bool|string
     */
    protected function checkSplitLink($value, $rule, array $data = [])
    {
        $linkId = (int) $value;
        $link = Link::get($linkId);
        if (!$link) {
            return '所选分流链接不存在';
        }
        if (($link['status'] ?? '') !== 'normal') {
            return '所选分流链接已停用';
        }
        return true;
    }

    /**
     * @param mixed $value
     * @return bool|string
     */
    protected function checkNumbers($value)
    {
        $list = NumberModel::parseNumbersText((string) $value);
        return $list !== [] ? true : '请至少填写一个有效号码（每行一个）';
    }

    /**
     * @param mixed $value
     * @return bool|string
     */
    protected function checkStatus($value)
    {
        $key = (string) $value;
        $list = (new NumberModel())->getStatusList();
        return isset($list[$key]) ? true : '状态无效';
    }
}
