// 签到奖励规则后台管理页事件代理，替代 Blade 内联编辑、删除确认和启停函数。

let adminSignInRulesControlsBound = false;
let signInRulesCache = null;

const SIGN_IN_RULE_FIELDS = [
    "streak_days",
    "gold_reward",
    "exp_reward",
    "charm_reward",
    "identity_badge_code",
    "identity_badge_name",
    "identity_badge_icon",
    "identity_badge_color",
    "identity_duration_days",
    "sort_order",
];

/**
 * 读取后台 layout 注入的 CSRF token。
 *
 * @returns {string}
 */
function getCsrfToken() {
    return document.querySelector('meta[name="csrf-token"]')?.getAttribute("content") || "";
}

/**
 * 读取 Blade 注入的签到规则快照。
 *
 * @returns {Record<string, Record<string, unknown>>}
 */
function getSignInRules() {
    if (signInRulesCache !== null) {
        return signInRulesCache;
    }

    const dataNode = document.getElementById("admin-sign-in-rules-data");
    if (!dataNode?.textContent) {
        signInRulesCache = {};
        return signInRulesCache;
    }

    try {
        signInRulesCache = JSON.parse(dataNode.textContent);
    } catch (error) {
        // 数据块异常时不影响新增和删除表单，编辑弹窗会自然不可打开。
        signInRulesCache = {};
    }

    return signInRulesCache;
}

/**
 * 打开签到规则编辑弹窗并填充表单。
 *
 * @param {string} ruleId 规则 ID
 * @returns {void}
 */
function openSignInRuleModal(ruleId) {
    const rule = getSignInRules()[ruleId];
    const form = document.getElementById("edit-rule-form");
    const modal = document.getElementById("edit-rule-modal");

    if (!rule || !(form instanceof HTMLFormElement) || !modal) {
        return;
    }

    form.action = String(rule.update_url || "");
    SIGN_IN_RULE_FIELDS.forEach((field) => {
        const input = document.getElementById(`edit-${field}`);
        if (input instanceof HTMLInputElement) {
            input.value = String(rule[field] ?? "");
        }
    });

    const enabledInput = document.getElementById("edit-is-enabled");
    if (enabledInput instanceof HTMLInputElement) {
        enabledInput.checked = Boolean(rule.is_enabled);
    }

    modal.classList.remove("hidden");
}

/**
 * 关闭签到规则编辑弹窗。
 *
 * @returns {void}
 */
function closeSignInRuleModal() {
    document.getElementById("edit-rule-modal")?.classList.add("hidden");
}

/**
 * 切换签到规则启停状态，低风险沿用旧逻辑：成功后刷新页面。
 *
 * @param {HTMLButtonElement} button 启停按钮
 * @returns {Promise<void>}
 */
async function toggleSignInRule(button) {
    const toggleUrl = button.getAttribute("data-sign-in-rule-toggle-url") || "";

    if (!toggleUrl || button.disabled) {
        return;
    }

    button.disabled = true;

    try {
        const response = await fetch(toggleUrl, {
            method: "POST",
            headers: {
                "X-CSRF-TOKEN": getCsrfToken(),
                "Accept": "application/json",
            },
        });
        const data = await response.json();

        if (!response.ok || !data?.ok) {
            window.alert(data?.message || "切换失败");
            return;
        }

        window.location.reload();
    } finally {
        button.disabled = false;
    }
}

/**
 * 绑定签到奖励规则管理页操作按钮。
 *
 * @returns {void}
 */
export function bindAdminSignInRulesControls() {
    if (adminSignInRulesControlsBound || typeof document === "undefined") {
        return;
    }

    adminSignInRulesControlsBound = true;
    document.addEventListener("click", (event) => {
        if (!(event.target instanceof Element)) {
            return;
        }

        const editButton = event.target.closest("[data-sign-in-rule-edit-id]");
        if (editButton) {
            event.preventDefault();
            openSignInRuleModal(editButton.getAttribute("data-sign-in-rule-edit-id") || "");
            return;
        }

        const closeButton = event.target.closest("[data-sign-in-rule-close]");
        if (closeButton) {
            event.preventDefault();
            closeSignInRuleModal();
            return;
        }

        const toggleButton = event.target.closest("[data-sign-in-rule-toggle-url]");
        if (toggleButton instanceof HTMLButtonElement) {
            event.preventDefault();
            void toggleSignInRule(toggleButton);
        }
    });

    document.addEventListener("submit", (event) => {
        if (!(event.target instanceof HTMLFormElement)) {
            return;
        }

        const confirmMessage = event.target.getAttribute("data-sign-in-rule-delete-confirm");
        if (confirmMessage && !window.confirm(confirmMessage)) {
            event.preventDefault();
        }
    });
}
