import { CampaignService } from './campaign-service.js';
export async function campaignRoutes(app, opts) {
    const service = new CampaignService(opts.db);
    app.post('/api/email/campaigns', { preHandler: [app.requireAuth] }, async (request, reply) => {
        const campaign = service.createCampaign(request.body);
        return reply.send({ campaign });
    });
    app.get('/api/email/campaigns', { preHandler: [app.requireAuth] }, async (request, reply) => {
        const { page, limit, status } = request.query;
        const result = service.listCampaigns({ page: page ? parseInt(page) : undefined, limit: limit ? parseInt(limit) : undefined, status });
        return reply.send(result);
    });
    app.get('/api/email/campaigns/:id', { preHandler: [app.requireAuth] }, async (request, reply) => {
        const campaign = service.getCampaign(request.params.id);
        if (!campaign)
            return reply.status(404).send({ error: 'Campaign not found' });
        return reply.send({ campaign });
    });
    app.post('/api/email/campaigns/:id/enqueue', { preHandler: [app.requireAuth] }, async (request, reply) => {
        try {
            service.enqueueCampaign(request.params.id, request.body.filters);
            const campaign = service.getCampaign(request.params.id);
            return reply.send({ campaign });
        }
        catch (err) {
            return reply.status(400).send({ error: err.message });
        }
    });
    app.post('/api/email/campaigns/:id/start', { preHandler: [app.requireAuth] }, async (request, reply) => {
        try {
            service.startCampaign(request.params.id);
            return reply.send({ campaign: service.getCampaign(request.params.id) });
        }
        catch (err) {
            return reply.status(400).send({ error: err.message });
        }
    });
    app.post('/api/email/campaigns/:id/pause', { preHandler: [app.requireAuth] }, async (request, reply) => {
        try {
            service.pauseCampaign(request.params.id);
            return reply.send({ campaign: service.getCampaign(request.params.id) });
        }
        catch (err) {
            return reply.status(400).send({ error: err.message });
        }
    });
    app.post('/api/email/campaigns/:id/cancel', { preHandler: [app.requireAuth] }, async (request, reply) => {
        try {
            service.cancelCampaign(request.params.id);
            return reply.send({ campaign: service.getCampaign(request.params.id) });
        }
        catch (err) {
            return reply.status(400).send({ error: err.message });
        }
    });
    app.get('/api/email/campaigns/:id/queue', { preHandler: [app.requireAuth] }, async (request, reply) => {
        const page = request.query.page ? parseInt(request.query.page) : 1;
        const limit = request.query.limit ? parseInt(request.query.limit) : 50;
        const offset = (page - 1) * limit;
        const total = opts.db.prepare('SELECT COUNT(*) as count FROM email_queue WHERE campaign_id = ?').get(request.params.id).count;
        const data = opts.db.prepare('SELECT * FROM email_queue WHERE campaign_id = ? ORDER BY created_at LIMIT ? OFFSET ?').all(request.params.id, limit, offset);
        return reply.send({ data, total });
    });
}
//# sourceMappingURL=campaign-routes.js.map