<?php

declare(strict_types=1);

namespace app\common\library\scrm\spider;

use app\common\library\scrm\AbstractScrmSpider;
use app\common\library\scrm\UnifiedScrmData;

/**
 * SS云控(Customer) 蜘蛛
 */
class SsCustomerSpider extends AbstractScrmSpider
{
    private const API_LIST = '/sys/share/report/get-customer-analysis-dimension-list';

    private const API_DETAILS = '/sys/share/report/get-customer-analysis-statistics';

    private const DEFAULT_PER_PAGE_COUNT = 20;

    private string $pageUrl;

    private string $account;

    private string $password;

    private UnifiedScrmData $unifiedData;

    public function __construct(
        string $pageUrl,
        string $account = '',
        string $password = '',
        string $nodeHost = 'http://127.0.0.1:3001'
    ) {
        parent::__construct($nodeHost);
        $this->pageUrl = $pageUrl;
        $this->account = $account;
        $this->password = $password;
        $this->unifiedData = new UnifiedScrmData();
    }

    protected function getSpiderConfig(): array
    {
        return [
            'pageUrl'        => $this->pageUrl,
            'listApi'        => self::API_LIST,
            'detailApi'      => self::API_DETAILS,
            'listMethod'     => 'POST',
            'paginationMode' => self::MODE_UI,
            'authActions'    => [
                ['type' => 'wait', 'ms' => 2000],
                ['type' => 'type', 'selector' => 'input[type="password"]', 'value' => $this->password],
                ['type' => 'press', 'key' => 'Enter'],
                ['type' => 'wait', 'ms' => 3000],
                [
                    'type'     => 'vue_click',
                    'selector' => 'button[class*="reports-customers__dimension"]',
                    'text'     => 'social media accounts',
                ],
                ['type' => 'wait', 'ms' => 3000],
            ],
        ];
    }

    protected function extractListTotalPages($listFirstPageData, $countData = null)
    {
        return null;
    }

    protected function buildListPageParams(int $page): array
    {
        return ['page' => $page, 'pageSize' => self::DEFAULT_PER_PAGE_COUNT];
    }

    protected function getUiPaginationConfig(): array
    {
        return [
            'nextBtnSelector' => '.arco-pagination-item-next',
            'waitMs'          => 2000,
        ];
    }

    protected function parseToUnifiedData($detailData, array $allListPagesData): UnifiedScrmData
    {
        $unifiedData = $this->unifiedData;
        if ($detailData) {
            $unifiedData->todayNewCount = (int) ($detailData['data']['distinct_contacts_total'] ?? 0);
        }
        foreach ($allListPagesData as $pageRaw) {
            $records = $pageRaw['data']['list'] ?? [];
            foreach ($records as $item) {
                if (empty($item['channel_tag'])) {
                    continue;
                }
                $number = (string) $item['channel_tag'];
                $unifiedData->addNumber($number, true, (int) ($item['distinct_contacts_total'] ?? 0));
            }
        }
        $unifiedData->total = count($unifiedData->numbers);
        return $unifiedData;
    }
}
