/**
 * Tests for EngagementTracker
 *
 * Story 3.4: Verify engagement tracking behavior
 */

import { describe, it, expect, beforeEach } from 'vitest';
import { EngagementTracker } from './engagement-tracker';
import { db } from '@/lib/db';
import type { DraftRecord } from '@/lib/db';

describe('EngagementTracker', () => {
  // Helper to create a test draft
  const createDraft = async (status: DraftRecord['status'] = 'draft'): Promise<number> => {
    const id = await db.drafts.add({
      sessionId: 'test-session',
      title: 'Test Draft',
      content: '# Test Content',
      tags: ['test'],
      createdAt: Date.now(),
      status,
    });
    return id;
  };

  beforeEach(async () => {
    // Clear database before each test
    await db.delete();
    await db.open();
  });

  describe('getCompletedSessionCount', () => {
    it('should return 0 when no drafts exist', async () => {
      const count = await EngagementTracker.getCompletedSessionCount();

      expect(count).toBe(0);
    });

    it('should return 0 when only draft status drafts exist', async () => {
      await createDraft('draft');
      await createDraft('draft');

      const count = await EngagementTracker.getCompletedSessionCount();

      expect(count).toBe(0);
    });

    it('should return count of completed drafts', async () => {
      await createDraft('completed');
      await createDraft('completed');
      await createDraft('draft');

      const count = await EngagementTracker.getCompletedSessionCount();

      expect(count).toBe(2);
    });

    it('should return count of regenerated drafts', async () => {
      await createDraft('regenerated');
      await createDraft('completed');

      const count = await EngagementTracker.getCompletedSessionCount();

      expect(count).toBe(1);
    });

    it('should count all completed drafts', async () => {
      await createDraft('completed');
      await createDraft('completed');
      await createDraft('completed');
      await createDraft('draft');

      const count = await EngagementTracker.getCompletedSessionCount();

      expect(count).toBe(3);
    });
  });

  describe('hasEngaged', () => {
    it('should return false when no drafts exist', async () => {
      const hasEngaged = await EngagementTracker.hasEngaged();

      expect(hasEngaged).toBe(false);
    });

    it('should return false when only draft status drafts exist', async () => {
      await createDraft('draft');
      await createDraft('draft');

      const hasEngaged = await EngagementTracker.hasEngaged();

      expect(hasEngaged).toBe(false);
    });

    it('should return true when at least 1 completed draft exists', async () => {
      await createDraft('completed');

      const hasEngaged = await EngagementTracker.hasEngaged();

      expect(hasEngaged).toBe(true);
    });

    it('should return true when multiple completed drafts exist', async () => {
      await createDraft('completed');
      await createDraft('completed');
      await createDraft('draft');

      const hasEngaged = await EngagementTracker.hasEngaged();

      expect(hasEngaged).toBe(true);
    });
  });

  describe('getEngagementLevel', () => {
    it('should return "none" when no completed drafts', async () => {
      const level = await EngagementTracker.getEngagementLevel();

      expect(level).toBe('none');
    });

    it('should return "low" when 1 completed draft', async () => {
      await createDraft('completed');

      const level = await EngagementTracker.getEngagementLevel();

      expect(level).toBe('low');
    });

    it('should return "low" when 2 completed drafts', async () => {
      await createDraft('completed');
      await createDraft('completed');

      const level = await EngagementTracker.getEngagementLevel();

      expect(level).toBe('low');
    });

    it('should return "medium" when 3 completed drafts', async () => {
      await createDraft('completed');
      await createDraft('completed');
      await createDraft('completed');

      const level = await EngagementTracker.getEngagementLevel();

      expect(level).toBe('medium');
    });

    it('should return "medium" when 5 completed drafts', async () => {
      await createDraft('completed');
      await createDraft('completed');
      await createDraft('completed');
      await createDraft('completed');
      await createDraft('completed');

      const level = await EngagementTracker.getEngagementLevel();

      expect(level).toBe('medium');
    });

    it('should return "high" when 6 completed drafts', async () => {
      await createDraft('completed');
      await createDraft('completed');
      await createDraft('completed');
      await createDraft('completed');
      await createDraft('completed');
      await createDraft('completed');

      const level = await EngagementTracker.getEngagementLevel();

      expect(level).toBe('high');
    });

    it('should return "high" when more than 6 completed drafts', async () => {
      for (let i = 0; i < 10; i++) {
        await createDraft('completed');
      }

      const level = await EngagementTracker.getEngagementLevel();

      expect(level).toBe('high');
    });

    it('should only count completed drafts, not draft or regenerated', async () => {
      await createDraft('draft');
      await createDraft('draft');
      await createDraft('completed');
      await createDraft('regenerated');

      const level = await EngagementTracker.getEngagementLevel();

      expect(level).toBe('low');
    });
  });
});
