import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import Database from 'better-sqlite3';
import { runMigrations } from '../../db/migrations.js';
import { CampaignService } from '../campaign-service.js';
import { LeadService } from '../lead-service.js';
describe('CampaignService', () => {
    let db;
    let service;
    let leadService;
    beforeEach(() => {
        db = new Database(':memory:');
        runMigrations(db);
        service = new CampaignService(db);
        leadService = new LeadService(db);
    });
    afterEach(() => { db.close(); });
    it('should create a campaign in draft status', () => {
        const campaign = service.createCampaign({ name: 'Test', subject: 'Hi', body_html: '<p>Hi</p>', from_email: 'a@b.com' });
        expect(campaign.status).toBe('draft');
        expect(campaign.id).toBeDefined();
    });
    it('should enqueue campaign and populate email_queue', () => {
        const lead = leadService.createLead({ email: 'user@test.com', source: 'manual' });
        leadService.addTags(lead.id, ['saas']);
        const campaign = service.createCampaign({ name: 'Test', subject: 'Hi', body_html: '<p>Hi</p>', from_email: 'a@b.com' });
        service.enqueueCampaign(campaign.id, { tags: ['saas'] });
        const updated = service.getCampaign(campaign.id);
        expect(updated.status).toBe('queued');
        expect(updated.total_recipients).toBe(1);
        const queue = db.prepare('SELECT * FROM email_queue WHERE campaign_id = ?').all(campaign.id);
        expect(queue).toHaveLength(1);
    });
    it('should reject start on draft campaign', () => {
        const campaign = service.createCampaign({ name: 'Test', subject: 'Hi', body_html: '<p>Hi</p>', from_email: 'a@b.com' });
        expect(() => service.startCampaign(campaign.id)).toThrow(/queued/);
    });
    it('should start a queued campaign', () => {
        const lead = leadService.createLead({ email: 'user@test.com', source: 'manual' });
        const campaign = service.createCampaign({ name: 'Test', subject: 'Hi', body_html: '<p>Hi</p>', from_email: 'a@b.com' });
        service.enqueueCampaign(campaign.id, {});
        service.startCampaign(campaign.id);
        expect(service.getCampaign(campaign.id).status).toBe('sending');
    });
    it('should pause and resume', () => {
        const lead = leadService.createLead({ email: 'user@test.com', source: 'manual' });
        const campaign = service.createCampaign({ name: 'Test', subject: 'Hi', body_html: '<p>Hi</p>', from_email: 'a@b.com' });
        service.enqueueCampaign(campaign.id, {});
        service.startCampaign(campaign.id);
        service.pauseCampaign(campaign.id);
        expect(service.getCampaign(campaign.id).status).toBe('paused');
        service.resumeCampaign(campaign.id);
        expect(service.getCampaign(campaign.id).status).toBe('sending');
    });
    it('should cancel and delete pending queue entries', () => {
        const lead = leadService.createLead({ email: 'user@test.com', source: 'manual' });
        const campaign = service.createCampaign({ name: 'Test', subject: 'Hi', body_html: '<p>Hi</p>', from_email: 'a@b.com' });
        service.enqueueCampaign(campaign.id, {});
        service.cancelCampaign(campaign.id);
        expect(service.getCampaign(campaign.id).status).toBe('cancelled');
        const pending = db.prepare("SELECT * FROM email_queue WHERE campaign_id = ? AND status = 'pending'").all(campaign.id);
        expect(pending).toHaveLength(0);
    });
    it('should filter leads by list during enqueue', () => {
        const a = leadService.createLead({ email: 'a@b.com', source: 'manual' });
        const b = leadService.createLead({ email: 'b@b.com', source: 'manual' });
        leadService.addToList(a.id, 'vip');
        const campaign = service.createCampaign({ name: 'Test', subject: 'Hi', body_html: '<p>Hi</p>', from_email: 'a@b.com' });
        service.enqueueCampaign(campaign.id, { lists: ['vip'] });
        expect(service.getCampaign(campaign.id).total_recipients).toBe(1);
    });
    it('should exclude non-active leads during enqueue', () => {
        leadService.createLead({ email: 'a@b.com', source: 'manual' });
        leadService.unsubscribe('a@b.com');
        const campaign = service.createCampaign({ name: 'Test', subject: 'Hi', body_html: '<p>Hi</p>', from_email: 'a@b.com' });
        service.enqueueCampaign(campaign.id, {});
        expect(service.getCampaign(campaign.id).total_recipients).toBe(0);
    });
});
//# sourceMappingURL=campaign-service.test.js.map