import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import Database from 'better-sqlite3';
import { runMigrations } from '../../db/migrations.js';
import { LeadService } from '../lead-service.js';
describe('LeadService', () => {
    let db;
    let service;
    beforeEach(() => {
        db = new Database(':memory:');
        runMigrations(db);
        service = new LeadService(db);
    });
    afterEach(() => { db.close(); });
    describe('createLead', () => {
        it('should create a lead', () => {
            const lead = service.createLead({ email: 'a@b.com', name: 'Alice', source: 'manual' });
            expect(lead.id).toBeDefined();
            expect(lead.email).toBe('a@b.com');
            expect(lead.status).toBe('active');
        });
        it('should upsert on duplicate email', () => {
            service.createLead({ email: 'a@b.com', name: 'Alice', source: 'manual' });
            const lead = service.createLead({ email: 'a@b.com', name: 'Alice Updated', source: 'csv' });
            expect(lead.name).toBe('Alice Updated');
            const all = service.queryLeads({});
            expect(all.data).toHaveLength(1);
        });
    });
    describe('tags', () => {
        it('should add and query tags', () => {
            const lead = service.createLead({ email: 'a@b.com', source: 'manual' });
            service.addTags(lead.id, ['saas', 'hot']);
            const tags = service.getTagsForLead(lead.id);
            expect(tags).toEqual(['hot', 'saas']);
        });
        it('should remove tags', () => {
            const lead = service.createLead({ email: 'a@b.com', source: 'manual' });
            service.addTags(lead.id, ['saas', 'hot']);
            service.removeTags(lead.id, ['hot']);
            expect(service.getTagsForLead(lead.id)).toEqual(['saas']);
        });
    });
    describe('lists', () => {
        it('should add to list and query', () => {
            const lead = service.createLead({ email: 'a@b.com', source: 'manual' });
            service.addToList(lead.id, 'prospects');
            const lists = service.getListsForLead(lead.id);
            expect(lists).toEqual(['prospects']);
        });
    });
    describe('queryLeads', () => {
        it('should filter by tag', () => {
            const a = service.createLead({ email: 'a@b.com', source: 'manual' });
            const b = service.createLead({ email: 'b@b.com', source: 'manual' });
            service.addTags(a.id, ['saas']);
            const result = service.queryLeads({ tag: 'saas' });
            expect(result.data).toHaveLength(1);
            expect(result.data[0].email).toBe('a@b.com');
        });
        it('should filter by list', () => {
            const a = service.createLead({ email: 'a@b.com', source: 'manual' });
            service.addToList(a.id, 'vip');
            service.createLead({ email: 'b@b.com', source: 'manual' });
            const result = service.queryLeads({ list: 'vip' });
            expect(result.data).toHaveLength(1);
        });
        it('should paginate', () => {
            for (let i = 0; i < 5; i++)
                service.createLead({ email: `u${i}@b.com`, source: 'manual' });
            const page1 = service.queryLeads({ page: 1, limit: 2 });
            expect(page1.data).toHaveLength(2);
            expect(page1.total).toBe(5);
        });
    });
    describe('unsubscribe', () => {
        it('should mark lead as unsubscribed', () => {
            service.createLead({ email: 'a@b.com', source: 'manual' });
            service.unsubscribe('a@b.com');
            const result = service.queryLeads({ status: 'unsubscribed' });
            expect(result.data).toHaveLength(1);
        });
    });
    describe('getListsWithCounts', () => {
        it('should return lists with lead counts', () => {
            const a = service.createLead({ email: 'a@b.com', source: 'manual' });
            const b = service.createLead({ email: 'b@b.com', source: 'manual' });
            service.addToList(a.id, 'vip');
            service.addToList(b.id, 'vip');
            service.addToList(a.id, 'trial');
            const lists = service.getListsWithCounts();
            expect(lists).toEqual([{ list_name: 'trial', count: 1 }, { list_name: 'vip', count: 2 }]);
        });
    });
    describe('getTagsWithCounts', () => {
        it('should return tags with lead counts', () => {
            const a = service.createLead({ email: 'a@b.com', source: 'manual' });
            service.addTags(a.id, ['saas', 'hot']);
            const tags = service.getTagsWithCounts();
            expect(tags.find((t) => t.tag === 'saas')?.count).toBe(1);
        });
    });
    describe('batch import', () => {
        it('should import leads in batch', () => {
            const results = service.importBatch([
                { email: 'a@b.com', name: 'Alice' },
                { email: 'b@b.com', name: 'Bob' },
            ], 'test-squad', 'squad');
            expect(results.imported).toBe(2);
            expect(service.queryLeads({}).total).toBe(2);
        });
    });
});
//# sourceMappingURL=lead-service.test.js.map