document.addEventListener('DOMContentLoaded', () => {
    const cfg = window.scos_review;
    if (!cfg) return;

    const notice = document.getElementById(`${cfg.prefix}review-notice`);
    if (!notice) return;

    const message = notice.querySelector(`.${cfg.prefix}review-message`);
    const actions = notice.querySelector(`.${cfg.prefix}review-actions`);

    if (!message || !actions) return;

    const postAction = async (reviewAction) => {
        const body = new URLSearchParams({
            action: `${cfg.prefix}review_action`,
            nonce: cfg.nonce,
            review_action: reviewAction,
        });

        const response = await fetch(cfg.ajaxUrl, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
            },
            body: body.toString(),
        });

        return response.json();
    };

    const renderPositive = () => {
        message.textContent = cfg.texts.positiveMessage;
        actions.innerHTML = `
            <a href="${cfg.reviewUrl}" class="button button-primary" data-action="reviewed" target="_blank" rel="noopener noreferrer">⭐ ${cfg.texts.leaveReview}</a>
            <a href="#" class="button button-secondary" data-action="later">${cfg.texts.maybeLater}</a>
            <a href="#" class="button button-secondary" data-action="dismiss">${cfg.texts.dismiss}</a>
        `;
    };

    const renderNegative = () => {
        message.textContent = cfg.texts.negativeMessage;
        actions.innerHTML = `
            <a href="${cfg.supportUrl}" class="button button-primary" data-action="feedback" target="_blank" rel="noopener noreferrer">${cfg.texts.sendFeedback}</a>
            <a href="#" class="button button-secondary" data-action="dismiss">${cfg.texts.dismiss}</a>
        `;
    };

    actions.addEventListener('click', async (event) => {
        const button = event.target.closest('[data-action]');
        if (!button) return;

        const action = button.dataset.action;

        if (action === 'positive') {
            event.preventDefault();
            try {
                await postAction('positive');
            } catch (e) {}
            renderPositive();
            return;
        }

        if (action === 'negative') {
            event.preventDefault();
            try {
                await postAction('negative');
            } catch (e) {}
            renderNegative();
            return;
        }

        if (action === 'dismiss') {
            event.preventDefault();
            try {
                await postAction('dismiss');
            } catch (e) {}
            notice.remove();
            return;
        }

        if (action === 'later') {
            event.preventDefault();
            try {
                await postAction('later');
            } catch (e) {}
            notice.remove();
            return;
        }

        if (action === 'feedback') {
            try {
                await postAction('dismiss');
            } catch (e) {}
            notice.remove();
            return;
        }

        if (action === 'reviewed') {
            try {
                await postAction('reviewed');
            } catch (e) {}
            notice.remove();
        }
    });
});