<?php

declare(strict_types=1);

namespace app\common\service;

use app\common\library\scrm\ScrmSpiderInterface;
use app\common\library\scrm\spider\A2cSpider;
use app\common\library\scrm\spider\ChatknowSpider;
use app\common\library\scrm\spider\HaiwangSpider;
use app\common\library\scrm\spider\HuojianSpider;
use app\common\library\scrm\spider\SsCustomerSpider;
use app\common\library\scrm\spider\WhatshubSpider;
use app\common\library\scrm\spider\XingheSpider;

/**
 * 工单类型 -> 云控蜘蛛工厂
 *
 * 新增云控类型：在 spider/ 下新增类并在此注册 ticket_type => Class
 */
class SplitScrmSpiderFactory
{
    /** @var array<string, class-string<ScrmSpiderInterface>> */
    private const MAP = [
        'a2c'         => A2cSpider::class,
        'haiwang'     => HaiwangSpider::class,
        'huojian'     => HuojianSpider::class,
        'xinghe'      => XingheSpider::class,
        'ss_customer' => SsCustomerSpider::class,
        'chatknow'    => ChatknowSpider::class,
        'whatshub'    => WhatshubSpider::class,
        // ceo_scrm 等未实现类型：新增 spider 类后在此注册
    ];

    /**
     * @return class-string<ScrmSpiderInterface>|null
     */
    public static function resolveClass(string $ticketType): ?string
    {
        $ticketType = trim($ticketType);
        return self::MAP[$ticketType] ?? null;
    }

    /**
     * 是否已实现蜘蛛
     */
    public static function isSupported(string $ticketType): bool
    {
        return self::resolveClass($ticketType) !== null;
    }

    /**
     * 已注册且已实现蜘蛛的类型列表
     *
     * @return list<string>
     */
    public static function listSupportedTypes(): array
    {
        return array_keys(self::MAP);
    }

    /**
     * @return ScrmSpiderInterface|null
     */
    public static function create(
        string $ticketType,
        string $pageUrl,
        string $account = '',
        string $password = '',
        string $nodeHost = ''
    ): ?ScrmSpiderInterface {
        $class = self::resolveClass($ticketType);
        if ($class === null) {
            return null;
        }
        $host = $nodeHost !== '' ? $nodeHost : SplitSyncConfigService::getNodeHost();
        return new $class($pageUrl, $account, $password, $host);
    }
}
