<?php

declare(strict_types=1);

namespace app\common\service;

use app\admin\model\split\Number;
use app\admin\model\split\Ticket;
use app\common\library\scrm\UnifiedScrmData;
use think\Db;

/**
 * 工单同步结果写入号码表
 */
class SplitTicketNumberSyncService
{
    /**
     * 将蜘蛛返回的号码列表同步到号码管理
     */
    public function syncFromUnifiedData(Ticket $ticket, UnifiedScrmData $data): void
    {
        $adminId = (int) $ticket['admin_id'];
        $linkId = (int) $ticket['split_link_id'];
        $ticketName = (string) $ticket['ticket_name'];
        if ($linkId <= 0 || $ticketName === '') {
            return;
        }

        $randomShuffle = SplitNumberWeighService::isRandomShuffleEnabled($linkId);

        // 使用独立 number 字段存储，避免纯数字号码作为数组 key 被 PHP 自动转为 int
        $syncedNumbers = [];
        foreach ($data->numbers as $row) {
            $number = self::normalizeNumber($row['number'] ?? '');
            if ($number === '') {
                continue;
            }
            $syncedNumbers[] = [
                'number' => $number,
                'row'    => $row,
            ];
        }

        $existingList = Number::where('admin_id', $adminId)
            ->where('split_link_id', $linkId)
            ->where('ticket_name', $ticketName)
            ->select();

        $existingMap = [];
        foreach ($existingList as $item) {
            $existingMap[(string) $item['number']] = $item;
        }

        $syncedNumberSet = [];
        $pendingInserts = [];
        foreach ($syncedNumbers as $entry) {
            $number = $entry['number'];
            $row = $entry['row'];
            $syncedNumberSet[$number] = true;
            $platformStatus = ($row['status'] ?? '') === 'online' ? 'online' : 'offline';
            $newFollowers = (int) ($row['newFollowersToday'] ?? 0);

            if (isset($existingMap[$number])) {
                $this->updateExistingNumber($existingMap[$number], $platformStatus, $newFollowers);
                continue;
            }

            $pendingInserts[] = [
                'number'          => $number,
                'platform_status' => $platformStatus,
                'new_followers'   => $newFollowers,
            ];
        }

        if ($pendingInserts !== []) {
            // 随机打乱：打乱待插入批次顺序，按随机顺序逐条 insert 以获得乱序自增 id
            if ($randomShuffle && count($pendingInserts) > 1) {
                shuffle($pendingInserts);
            }
            foreach ($pendingInserts as $item) {
                $this->insertNumber(
                    $ticket,
                    $item['number'],
                    $item['platform_status'],
                    $item['new_followers']
                );
            }
        }

        foreach ($existingMap as $number => $item) {
            if (isset($syncedNumberSet[$number])) {
                continue;
            }
            if ((int) $item['manual_manage'] === 1) {
                continue;
            }
            Number::where('id', (int) $item['id'])->update([
                'status'     => 'hidden',
                'updatetime' => time(),
            ]);
        }
    }

    /**
     * @param Number $row
     */
    private function updateExistingNumber($row, string $platformStatus, int $newFollowers): void
    {
        $update = [
            'platform_status' => $platformStatus,
            'updatetime'      => time(),
        ];

        if ((int) $row['manual_manage'] === 1) {
            Number::where('id', (int) $row['id'])->update($update);
            return;
        }

        // 进线人数由同步写入，最终开关由 applyNumberRules 统一判定（单号上限/下号比率等）
        $update['inbound_count'] = max(0, $newFollowers);
        Number::where('id', (int) $row['id'])->update($update);
    }

    private function insertNumber(
        Ticket $ticket,
        string $number,
        string $platformStatus,
        int $newFollowers
    ): void {
        $now = time();
        $data = [
            'admin_id'           => (int) $ticket['admin_id'],
            'split_link_id'      => (int) $ticket['split_link_id'],
            'ticket_name'        => (string) $ticket['ticket_name'],
            'number'             => $number,
            'number_type'        => (string) $ticket['number_type'],
            'number_type_custom' => (string) ($ticket['number_type_custom'] ?? ''),
            'visit_count'        => 0,
            'inbound_count'      => max(0, $newFollowers),
            'manual_manage'      => 0,
            'platform_status'    => $platformStatus,
            'status'             => 'hidden',
            'createtime'         => $now,
            'updatetime'         => $now,
        ];

        try {
            Db::name('split_number')->insert($data);
        } catch (\Throwable $e) {
            $exists = Number::where('split_link_id', (int) $ticket['split_link_id'])
                ->where('number', $number)
                ->find();
            if ($exists) {
                $this->updateExistingNumber($exists, $platformStatus, $newFollowers);
            }
        }
    }

    /**
     * 统一号码为字符串（云控 API 可能返回 int）
     */
    private static function normalizeNumber($value): string
    {
        if ($value === null || $value === '') {
            return '';
        }
        return trim((string) $value);
    }

    /**
     * 汇总工单进线人数（仅开启状态的号码）
     */
    public function sumInboundForTicket(Ticket $ticket): int
    {
        $sum = Number::where('admin_id', (int) $ticket['admin_id'])
            ->where('split_link_id', (int) $ticket['split_link_id'])
            ->where('ticket_name', (string) $ticket['ticket_name'])
            ->where('status', 'normal')
            ->sum('inbound_count');
        return (int) $sum;
    }
}
