// /AIWK_COMMON/core/yw-diagnostic.js
// AIWK EXT4 v044
(function () {
    "use strict";

    const AIWK = window.AIWK || (window.AIWK = {});

    function iso() { return new Date().toISOString(); }
    function safeJson(v) { try { return JSON.parse(JSON.stringify(v)); } catch (e) { return String(v); } }
    function maskToken(v) {
        const s = String(v || "").trim();
        if (!s) { return { has_token: 0, token_masked: "", token_len: 0 }; }
        if (s.length <= 12) { return { has_token: 1, token_masked: "***", token_len: s.length }; }
        return { has_token: 1, token_masked: s.slice(0, 6) + "..." + s.slice(-4), token_len: s.length };
    }
    function storageSnapshot() {
        const out = {};
        try {
            Object.keys(localStorage || {}).forEach(function (k) {
                if (/aiwk/i.test(k)) {
                    const v = String(localStorage.getItem(k) || "");
                    if (/token|auth/i.test(k)) {
                        out[k] = "[masked len=" + v.length + "]";
                    } else {
                        out[k] = v.length > 500 ? v.slice(0, 500) + "...(cut)" : v;
                    }
                }
            });
        } catch (e) { out.error = e.message || String(e); }
        return out;
    }
    function profileSnapshot(extra) {
        let profile = {};
        try { profile = AIWK.store && AIWK.store.get ? (AIWK.store.get("profile") || {}) : {}; } catch (e) { profile = {}; }
        profile = Object.assign({}, profile, extra && extra.profile ? extra.profile : {});
        const tokenInfo = maskToken(profile.authToken || profile.token || (extra && extra.token));
        delete profile.authToken;
        delete profile.token;
        delete profile.authPayload;
        return Object.assign({}, profile, tokenInfo);
    }
    function winSnapshot() {
        try {
            if (AIWK.win && typeof AIWK.win.exportState === "function") {
                return AIWK.win.exportState();
            }
        } catch (e) { return { ok: 0, error: e.message || String(e) }; }
        return { ok: 0, reason: "AIWK.win.exportState 없음" };
    }
    function commSnapshot() {
        try { return AIWK.comm && typeof AIWK.comm.status === "function" ? AIWK.comm.status() : { ok: 0, reason: "AIWK.comm.status 없음" }; }
        catch (e) { return { ok: 0, error: e.message || String(e) }; }
    }
    function buildReport(extra) {
        extra = extra || {};
        return {
            kind: "AIWK_CHATGPT_DIAGNOSTIC",
            aiwk_version: AIWK.version || "v044",
            build_version: AIWK.buildVersion || "0.0.98-stage4.5",
            source: extra.source || (location.hostname.indexOf("chatgpt") >= 0 ? "chatgpt_tab" : "aiwk_page"),
            created_at: iso(),
            url: location.href,
            title: document.title,
            profile: profileSnapshot(extra),
            comm: commSnapshot(),
            windows: winSnapshot(),
            web: safeJson(extra.web || {}),
            ext: safeJson(extra.ext || {}),
            tests: safeJson(extra.tests || []),
            notes: safeJson(extra.notes || []),
            local_aiwk_keys: storageSnapshot()
        };
    }
    function toChatGptText(report, userText) {
        const head = String(userText || "").trim();
        const body = JSON.stringify(report || buildReport(), null, 2);
        return (head ? head + "\n\n" : "") + "AIWK_DIAG_JSON_START\n" + body + "\nAIWK_DIAG_JSON_END";
    }

    AIWK.diagnostic = Object.assign({}, AIWK.diagnostic || {}, {
        maskToken: maskToken,
        buildReport: buildReport,
        toChatGptText: toChatGptText
    });
}());
