"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.campaignEmailsAdminRouter = exports.campaignEmailsPublicRouter = void 0;
const express_1 = require("express");
const campaign_emails_service_1 = require("./campaign-emails.service");
const campaign_emails_schemas_1 = require("./campaign-emails.schemas");
const validate_1 = require("../../../middleware/validate");
const auth_middleware_1 = require("../../../middleware/auth.middleware");
const rbac_middleware_1 = require("../../../middleware/rbac.middleware");
const rate_limit_1 = require("../../../middleware/rate-limit");
const roles_1 = require("../../../utils/roles");
// --- Public Routes (no auth) ---
const publicRouter = (0, express_1.Router)();
exports.campaignEmailsPublicRouter = publicRouter;
// POST /api/campaigns/:slug/send-email
publicRouter.post('/:slug/send-email', rate_limit_1.emailRateLimit, (0, validate_1.validate)(campaign_emails_schemas_1.sendCampaignEmailSchema), async (req, res, next) => {
    try {
        const slug = req.params.slug;
        const senderIp = req.ip || req.socket.remoteAddress;
        const result = await campaign_emails_service_1.campaignEmailsService.sendEmail(slug, req.body, senderIp);
        res.status(201).json(result);
    }
    catch (err) {
        next(err);
    }
});
// POST /api/campaigns/:slug/track-mailto
publicRouter.post('/:slug/track-mailto', rate_limit_1.emailRateLimit, (0, validate_1.validate)(campaign_emails_schemas_1.trackMailtoSchema), async (req, res, next) => {
    try {
        const slug = req.params.slug;
        const senderIp = req.ip || req.socket.remoteAddress;
        const result = await campaign_emails_service_1.campaignEmailsService.trackMailto(slug, req.body, senderIp);
        res.status(201).json(result);
    }
    catch (err) {
        next(err);
    }
});
// --- Admin Routes (auth required) ---
const adminRouter = (0, express_1.Router)();
exports.campaignEmailsAdminRouter = adminRouter;
adminRouter.use(auth_middleware_1.authenticate);
adminRouter.use((0, rbac_middleware_1.requireRole)(...roles_1.INFLUENCE_ROLES));
// GET /api/campaigns/:id/emails
adminRouter.get('/:id/emails', (0, validate_1.validate)(campaign_emails_schemas_1.listCampaignEmailsSchema, 'query'), async (req, res, next) => {
    try {
        const id = req.params.id;
        const result = await campaign_emails_service_1.campaignEmailsService.listByCampaign(id, req.query);
        res.json(result);
    }
    catch (err) {
        next(err);
    }
});
// GET /api/campaigns/:id/email-stats
adminRouter.get('/:id/email-stats', async (req, res, next) => {
    try {
        const id = req.params.id;
        const stats = await campaign_emails_service_1.campaignEmailsService.getStats(id);
        res.json(stats);
    }
    catch (err) {
        next(err);
    }
});
//# sourceMappingURL=campaign-emails.routes.js.map