"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.campaignsRouter = void 0;
const express_1 = require("express");
const campaigns_service_1 = require("./campaigns.service");
const campaigns_schemas_1 = require("./campaigns.schemas");
const validate_1 = require("../../../middleware/validate");
const auth_middleware_1 = require("../../../middleware/auth.middleware");
const rbac_middleware_1 = require("../../../middleware/rbac.middleware");
const roles_1 = require("../../../utils/roles");
const router = (0, express_1.Router)();
exports.campaignsRouter = router;
// All campaign admin routes require authentication + admin role
router.use(auth_middleware_1.authenticate);
router.use((0, rbac_middleware_1.requireRole)(...roles_1.INFLUENCE_ROLES));
// GET /api/campaigns — list campaigns with pagination/filters
router.get('/', (0, validate_1.validate)(campaigns_schemas_1.listCampaignsSchema, 'query'), async (req, res, next) => {
    try {
        const result = await campaigns_service_1.campaignsService.findAll(req.query, req.user);
        res.json(result);
    }
    catch (err) {
        next(err);
    }
});
// GET /api/campaigns/:id — get single campaign
router.get('/:id', async (req, res, next) => {
    try {
        const id = req.params.id;
        const campaign = await campaigns_service_1.campaignsService.findById(id);
        res.json(campaign);
    }
    catch (err) {
        next(err);
    }
});
// POST /api/campaigns — create campaign
router.post('/', (0, validate_1.validate)(campaigns_schemas_1.createCampaignSchema), async (req, res, next) => {
    try {
        const campaign = await campaigns_service_1.campaignsService.create(req.body, req.user);
        res.status(201).json(campaign);
    }
    catch (err) {
        next(err);
    }
});
// PUT /api/campaigns/:id — update campaign
router.put('/:id', (0, validate_1.validate)(campaigns_schemas_1.updateCampaignSchema), async (req, res, next) => {
    try {
        const id = req.params.id;
        const campaign = await campaigns_service_1.campaignsService.update(id, req.body);
        res.json(campaign);
    }
    catch (err) {
        next(err);
    }
});
// DELETE /api/campaigns/:id — delete campaign
router.delete('/:id', async (req, res, next) => {
    try {
        const id = req.params.id;
        await campaigns_service_1.campaignsService.delete(id);
        res.status(204).send();
    }
    catch (err) {
        next(err);
    }
});
//# sourceMappingURL=campaigns.routes.js.map