window.nexdirectoryModerationQueuePage = function(seed) {
    const safeSeed = seed && typeof seed === 'object' ? seed : {};

    const pendingEvents = Array.isArray(safeSeed.pendingEvents)
        ? safeSeed.pendingEvents.map(function(item) {
            return Object.assign({ selected: false }, item || {});
        })
        : [];

    const claims = Array.isArray(safeSeed.claims) ? safeSeed.claims : [];
    const reports = Array.isArray(safeSeed.reports) ? safeSeed.reports : [];

    return {
        activeTab: 'pending',
        pendingEvents: pendingEvents,
        claims: claims,
        reports: reports,
        actionUrl: String(safeSeed.actionUrl || ''),
        bulkNonce: String(safeSeed.bulkNonce || ''),

        bulkAction: '',
        bulkNote: '',
        selectAll: false,

        toast: { show: false, message: '', type: 'success', timeout: null },
        actionModal: { open: false, type: '', source: '', item: null, note: '' },

        get stats() {
            return {
                pending: this.pendingEvents.length,
                claims: this.claims.length,
                reports: this.reports.length,
                total: this.pendingEvents.length + this.claims.length + this.reports.length
            };
        },

        get selectedPendingIds() {
            return this.pendingEvents.filter(function(item) {
                return !!item.selected;
            }).map(function(item) {
                return Number(item.id || 0);
            }).filter(function(id) {
                return id > 0;
            });
        },

        showToast(msg, type) {
            this.toast.message = String(msg || '');
            this.toast.type = String(type || 'success');
            this.toast.show = true;
            if (this.toast.timeout) {
                clearTimeout(this.toast.timeout);
            }
            this.toast.timeout = setTimeout(() => {
                this.toast.show = false;
            }, 3000);
        },

        syncSelectAll() {
            if (this.pendingEvents.length === 0) {
                this.selectAll = false;
                return;
            }
            this.selectAll = this.pendingEvents.every(function(item) {
                return !!item.selected;
            });
        },

        toggleSelectAll() {
            const shouldSelect = !!this.selectAll;
            this.pendingEvents.forEach(function(item) {
                item.selected = shouldSelect;
            });
        },

        applyBulkAction() {
            if (this.selectedPendingIds.length === 0) {
                this.showToast('Please select at least one item.', 'error');
                return;
            }
            if (!this.bulkAction) {
                this.showToast('Please choose a bulk action.', 'error');
                return;
            }
            if (!this.$refs.bulkForm) {
                return;
            }
            this.$refs.bulkForm.submit();
        },

        submitPendingDecision(item, decision, note) {
            if (!item || !item.id || !item.nonce || !this.actionUrl) {
                return;
            }
            this.submitHiddenForm([
                { name: 'action', value: 'nexdirectory_moderate_event' },
                { name: 'post_id', value: item.id },
                { name: 'event_id', value: item.id },
                { name: 'decision', value: decision },
                { name: 'moderator_note', value: String(note || '') },
                { name: 'nexdirectory_moderate_nonce', value: item.nonce }
            ]);
        },

        submitClaimDecision(item, decision, note) {
            if (!item || !item.id || !item.nonce || !this.actionUrl) {
                return;
            }
            this.submitHiddenForm([
                { name: 'action', value: 'nexdirectory_moderate_claim' },
                { name: 'comment_id', value: item.id },
                { name: 'decision', value: decision },
                { name: 'claim_reject_reason', value: String(note || '') },
                { name: 'nexdirectory_moderate_claim_nonce', value: item.nonce }
            ]);
        },

        submitReportAction(item, intent, note, subject) {
            if (!item || !item.id || !item.nonce || !this.actionUrl) {
                return;
            }
            this.submitHiddenForm([
                { name: 'action', value: 'nexdirectory_moderate_report' },
                { name: 'comment_id', value: item.id },
                { name: 'report_id', value: item.id },
                { name: 'event_id', value: item.eventId || 0 },
                { name: 'intent', value: String(intent || '') },
                { name: 'admin_message', value: String(note || '') },
                { name: 'admin_subject', value: String(subject || '') },
                { name: 'nexdirectory_moderate_report_nonce', value: item.nonce }
            ]);
        },

        submitHiddenForm(fields) {
            const form = document.createElement('form');
            form.method = 'post';
            form.action = this.actionUrl;
            fields.forEach(function(field) {
                const input = document.createElement('input');
                input.type = 'hidden';
                input.name = String(field.name || '');
                input.value = String(field.value || '');
                form.appendChild(input);
            });
            document.body.appendChild(form);
            form.submit();
        },

        quickApprovePending(item) {
            this.submitPendingDecision(item, 'approve', '');
        },

        quickApproveClaim(item) {
            this.submitClaimDecision(item, 'approve', '');
        },

        quickToggleReportedEvent(item) {
            this.submitReportAction(item, 'toggle_event_status', '', '');
        },

        quickResolveReport(item) {
            this.submitReportAction(item, 'resolve_report', '', '');
        },

        openActionModal(type, source, item) {
            this.actionModal.type = String(type || '');
            this.actionModal.source = String(source || '');
            this.actionModal.item = item || null;
            this.actionModal.note = '';
            this.actionModal.open = true;
            document.body.style.overflow = 'hidden';
        },

        closeActionModal() {
            this.actionModal.open = false;
            this.actionModal.type = '';
            this.actionModal.source = '';
            this.actionModal.item = null;
            this.actionModal.note = '';
            document.body.style.overflow = '';
        },

        submitAction() {
            if (!this.actionModal.item) {
                return;
            }

            if (this.actionModal.source === 'claim' && this.actionModal.type === 'reject' && String(this.actionModal.note || '').trim() === '') {
                this.showToast('Please provide a rejection reason.', 'error');
                return;
            }

            if (this.actionModal.source === 'pending') {
                this.submitPendingDecision(this.actionModal.item, this.actionModal.type, this.actionModal.note);
                return;
            }

            if (this.actionModal.source === 'claim') {
                this.submitClaimDecision(this.actionModal.item, this.actionModal.type, this.actionModal.note);
                return;
            }

            if (this.actionModal.source === 'report') {
                if (String(this.actionModal.note || '').trim() === '') {
                    this.showToast('Please provide a message first.', 'error');
                    return;
                }
                this.submitReportAction(this.actionModal.item, this.actionModal.type, this.actionModal.note, '');
            }
        }
    };
};
