"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.listmonkRouter = void 0;
const express_1 = require("express");
const auth_middleware_1 = require("../../middleware/auth.middleware");
const rbac_middleware_1 = require("../../middleware/rbac.middleware");
const listmonk_client_1 = require("../../services/listmonk.client");
const listmonk_sync_service_1 = require("../../services/listmonk-sync.service");
const listmonk_event_sync_service_1 = require("../../services/listmonk-event-sync.service");
const env_1 = require("../../config/env");
const roles_1 = require("../../utils/roles");
const router = (0, express_1.Router)();
exports.listmonkRouter = router;
router.use(auth_middleware_1.authenticate);
router.use((0, rbac_middleware_1.requireRole)(...roles_1.BROADCAST_ROLES));
// GET /api/listmonk — sync status
router.get('/', async (_req, res, next) => {
    try {
        const status = listmonk_sync_service_1.listmonkSyncService.getStatus();
        // Live connection check
        const connected = await listmonk_client_1.listmonkClient.checkHealth();
        res.json({ ...status, connected });
    }
    catch (err) {
        next(err);
    }
});
// GET /api/listmonk/stats — list subscriber counts
router.get('/stats', async (_req, res, next) => {
    try {
        const stats = await listmonk_sync_service_1.listmonkSyncService.getStats();
        res.json(stats);
    }
    catch (err) {
        next(err);
    }
});
// POST /api/listmonk/test-connection
router.post('/test-connection', async (_req, res, next) => {
    try {
        const healthy = await listmonk_client_1.listmonkClient.checkHealth();
        res.json({
            success: healthy,
            message: healthy ? 'Listmonk is reachable' : 'Listmonk is not reachable',
        });
    }
    catch (err) {
        next(err);
    }
});
// POST /api/listmonk/sync/participants
router.post('/sync/participants', async (_req, res, next) => {
    try {
        const results = await listmonk_sync_service_1.listmonkSyncService.syncCampaignParticipants();
        listmonk_sync_service_1.listmonkSyncService.setLastSyncAt(new Date());
        res.json({
            success: true,
            message: `Synced ${results.success}/${results.total} campaign participants`,
            results,
        });
    }
    catch (err) {
        listmonk_sync_service_1.listmonkSyncService.setLastError(err instanceof Error ? err.message : String(err));
        next(err);
    }
});
// POST /api/listmonk/sync/locations
router.post('/sync/locations', async (_req, res, next) => {
    try {
        const results = await listmonk_sync_service_1.listmonkSyncService.syncLocations();
        listmonk_sync_service_1.listmonkSyncService.setLastSyncAt(new Date());
        res.json({
            success: true,
            message: `Synced ${results.success}/${results.total} locations`,
            results,
        });
    }
    catch (err) {
        listmonk_sync_service_1.listmonkSyncService.setLastError(err instanceof Error ? err.message : String(err));
        next(err);
    }
});
// POST /api/listmonk/sync/users
router.post('/sync/users', async (_req, res, next) => {
    try {
        const results = await listmonk_sync_service_1.listmonkSyncService.syncUsers();
        listmonk_sync_service_1.listmonkSyncService.setLastSyncAt(new Date());
        res.json({
            success: true,
            message: `Synced ${results.success}/${results.total} users`,
            results,
        });
    }
    catch (err) {
        listmonk_sync_service_1.listmonkSyncService.setLastError(err instanceof Error ? err.message : String(err));
        next(err);
    }
});
// POST /api/listmonk/sync/all
router.post('/sync/all', async (_req, res, next) => {
    try {
        const results = await listmonk_sync_service_1.listmonkSyncService.syncAll();
        res.json({
            success: true,
            message: 'All syncs completed',
            results,
        });
    }
    catch (err) {
        next(err);
    }
});
// POST /api/listmonk/reinitialize
router.post('/reinitialize', async (_req, res, next) => {
    try {
        await listmonk_sync_service_1.listmonkSyncService.reinitialize();
        res.json({ success: true, message: 'Lists reinitialized' });
    }
    catch (err) {
        next(err);
    }
});
// GET /api/listmonk/event-sync-stats — event-driven sync stats
router.get('/event-sync-stats', (_req, res) => {
    res.json(listmonk_event_sync_service_1.listmonkEventSyncService.getStats());
});
// GET /api/listmonk/proxy-url — get proxy port + token for iframe embedding
router.get('/proxy-url', (req, res, _next) => {
    const token = req.headers.authorization?.slice(7) || '';
    res.json({ port: env_1.env.LISTMONK_PROXY_PORT, token });
});
//# sourceMappingURL=listmonk.routes.js.map