/**
 * CF 过盾后等待业务页就绪（可选，由 antiBot.postCfReady* 驱动，未配置则跳过）
 */

/**
 * 采集当前页面诊断信息（auth 失败时便于排查）
 * @param {import('puppeteer').Page} page
 */
async function collectPageDiagnostics(page) {
    const url = page.url();
    const snapshot = await page.evaluate(() => ({
        url: window.location.href,
        title: document.title || '',
        bodyText: (document.body && document.body.innerText ? document.body.innerText : '').slice(0, 500),
        elInputInner: document.querySelectorAll('.el-input__inner').length,
    })).catch(() => null);

    return snapshot || { url, title: '', bodyText: '', elInputInner: 0 };
}

/**
 * CF 成功后等待最终业务 URL / DOM 出现（Whatshub 等 SPA 短链跳转场景）
 *
 * @param {import('puppeteer').Page} page
 * @param {object} antiBot normalizeAntiBot 返回值
 * @param {(page: import('puppeteer').Page) => Promise<string>} [waitForUrlSettled]
 * @param {number} [navigationTimeoutMs]
 */
async function awaitPostCfBusinessReady(page, antiBot, waitForUrlSettled, navigationTimeoutMs = 60000) {
    if (!antiBot || !antiBot.enabled) {
        return;
    }

    const urlContains = (antiBot.postCfReadyUrlContains || '').trim();
    const selector = (antiBot.postCfReadySelector || '').trim();
    if (urlContains === '' && selector === '') {
        return;
    }

    const timeoutMs = Math.max(5000, antiBot.postCfReadyTimeoutMs || 30000);
    console.log(
        `[CF] 等待业务页就绪 current=${page.url()} urlContains="${urlContains}" `
        + `selector="${selector}" timeout=${timeoutMs}ms`
    );

    try {
        if (urlContains !== '') {
            await page.waitForFunction(
                (needle) => window.location.href.includes(needle),
                { timeout: timeoutMs, polling: 200 },
                urlContains
            );
        }
        if (selector !== '') {
            await page.waitForSelector(selector, { timeout: timeoutMs, visible: true });
        }
    } catch (err) {
        const retryMs = Math.min(15000, timeoutMs);
        const needsRetry = urlContains !== '' && !page.url().includes(urlContains);
        if (needsRetry) {
            console.warn(`[CF] 业务页等待失败 url=${page.url()}，最后尝试 reload (${retryMs}ms)`);
            await page.reload({ waitUntil: 'domcontentloaded', timeout: navigationTimeoutMs }).catch(() => {});
            if (typeof waitForUrlSettled === 'function') {
                await waitForUrlSettled(page).catch(() => {});
            }
            if (urlContains !== '') {
                await page.waitForFunction(
                    (needle) => window.location.href.includes(needle),
                    { timeout: retryMs, polling: 200 },
                    urlContains
                );
            }
            if (selector !== '') {
                await page.waitForSelector(selector, { timeout: retryMs, visible: true });
            }
        } else {
            err.pageDiagnostics = await collectPageDiagnostics(page);
            throw err;
        }
    }

    const settleMs = antiBot.postCfSettleMs || 0;
    if (settleMs > 0) {
        await new Promise((resolve) => setTimeout(resolve, settleMs));
    }

    console.log(`[CF] 业务页就绪 url=${page.url()}`);
}

/**
 * 执行 authActions，失败时附带 page 诊断
 * @param {import('puppeteer').Page} page
 * @param {object[]|undefined} authActions
 * @param {Function} executeAuthActionsFn
 */
async function executeAuthActionsWithDiagnostics(page, authActions, executeAuthActionsFn) {
    try {
        await executeAuthActionsFn(page, authActions);
    } catch (err) {
        err.pageDiagnostics = await collectPageDiagnostics(page);
        throw err;
    }
}

module.exports = {
    collectPageDiagnostics,
    awaitPostCfBusinessReady,
    executeAuthActionsWithDiagnostics,
};
