import { LeadService } from './lead-service.js';
export async function leadRoutes(app, opts) {
    const service = new LeadService(opts.db);
    app.post('/api/email/leads', { preHandler: [app.requireAuth] }, async (request, reply) => {
        const lead = service.createLead(request.body);
        return reply.send({ lead });
    });
    app.get('/api/email/leads', { preHandler: [app.requireAuth] }, async (request, reply) => {
        const { tag, list, status, search, page, limit } = request.query;
        const result = service.queryLeads({ tag, list, status, search, page: page ? parseInt(page) : undefined, limit: limit ? parseInt(limit) : undefined });
        return reply.send(result);
    });
    app.post('/api/email/leads/batch', { preHandler: [app.requireAuth] }, async (request, reply) => {
        const result = service.importBatch(request.body.leads, request.body.source_detail, request.body.source);
        return reply.send(result);
    });
    app.patch('/api/email/leads/:id', { preHandler: [app.requireAuth] }, async (request, reply) => {
        const lead = service.updateLead(request.params.id, request.body);
        if (!lead)
            return reply.status(404).send({ error: 'Lead not found' });
        return reply.send({ lead });
    });
    app.delete('/api/email/leads/:id', { preHandler: [app.requireAuth] }, async (request, reply) => {
        service.deleteLead(request.params.id);
        return reply.send({ ok: true });
    });
    app.post('/api/email/leads/:id/tags', { preHandler: [app.requireAuth] }, async (request, reply) => {
        service.addTags(request.params.id, request.body.tags);
        const tags = service.getTagsForLead(request.params.id);
        return reply.send({ tags });
    });
    app.delete('/api/email/leads/:id/tags', { preHandler: [app.requireAuth] }, async (request, reply) => {
        service.removeTags(request.params.id, request.body.tags);
        const tags = service.getTagsForLead(request.params.id);
        return reply.send({ tags });
    });
    app.post('/api/email/leads/:id/lists', { preHandler: [app.requireAuth] }, async (request, reply) => {
        for (const list of request.body.lists)
            service.addToList(request.params.id, list);
        const lists = service.getListsForLead(request.params.id);
        return reply.send({ lists });
    });
    app.delete('/api/email/leads/:id/lists', { preHandler: [app.requireAuth] }, async (request, reply) => {
        for (const list of request.body.lists)
            service.removeFromList(request.params.id, list);
        const lists = service.getListsForLead(request.params.id);
        return reply.send({ lists });
    });
    app.get('/api/email/leads/lists', { preHandler: [app.requireAuth] }, async (_request, reply) => {
        return reply.send({ lists: service.getListsWithCounts() });
    });
    app.get('/api/email/leads/tags', { preHandler: [app.requireAuth] }, async (_request, reply) => {
        return reply.send({ tags: service.getTagsWithCounts() });
    });
}
//# sourceMappingURL=lead-routes.js.map