import { describe, it, expect } from 'vitest';
import { htmlRenderer } from './html-renderer';

const mockData = {
  exposureSummary: {
    totalExposures: 10,
    newExposures: 3,
    resolvedExposures: 2,
    criticalExposures: 1,
    warningExposures: 4,
    infoExposures: 5,
    exposuresBySource: { breach1: 5, breach2: 5 },
  },
  spamStats: {
    callsBlocked: 15,
    textsBlocked: 20,
    callsFlagged: 5,
    textsFlagged: 8,
    falsePositives: 2,
    totalSpamEvents: 35,
  },
  voiceStats: {
    analysesRun: 50,
    threatsDetected: 3,
    enrollmentsActive: 2,
    syntheticDetections: 3,
    voiceMismatchEvents: 3,
  },
  recommendations: [
    {
      category: 'dark_web',
      priority: 'high',
      title: 'Address Critical Exposures',
      description: '1 critical exposure detected.',
    },
  ],
  protectionScore: 75,
};

const renderContext = {
  reportTitle: 'Monthly Protection Report — January 2025',
  reportType: 'MONTHLY_PLUS',
  periodStart: '2025-01-01T00:00:00.000Z',
  periodEnd: '2025-01-31T23:59:59.000Z',
  generatedAt: '2025-02-01T00:00:00.000Z',
  userName: 'user-1',
  data: mockData,
  dashboardUrl: 'https://app.shieldai.com/reports/test-1',
  reportId: 'test-1',
};

describe('HtmlRenderer', () => {
  it('renders valid HTML with report title', () => {
    const html = htmlRenderer.render(renderContext);
    expect(html).toContain('Monthly Protection Report — January 2025');
    expect(html).toContain('<!DOCTYPE html>');
    expect(html).toContain('</html>');
  });

  it('renders protection score', () => {
    const html = htmlRenderer.render(renderContext);
    expect(html).toContain('75');
    expect(html).toContain('/ 100');
  });

  it('uses high score class for score >= 70', () => {
    const html = htmlRenderer.render(renderContext);
    expect(html).toContain('score-ring high');
  });

  it('uses medium score class for score 40-69', () => {
    const html = htmlRenderer.render({
      ...renderContext,
      data: { ...mockData, protectionScore: 50 },
    });
    expect(html).toContain('score-ring medium');
  });

  it('uses low score class for score < 40', () => {
    const html = htmlRenderer.render({
      ...renderContext,
      data: { ...mockData, protectionScore: 30 },
    });
    expect(html).toContain('score-ring low');
  });

  it('renders exposure summary stats', () => {
    const html = htmlRenderer.render(renderContext);
    expect(html).toContain('1'); // critical
    expect(html).toContain('4'); // warnings
    expect(html).toContain('3'); // new findings
    expect(html).toContain('2'); // resolved
  });

  it('renders spam protection stats', () => {
    const html = htmlRenderer.render(renderContext);
    expect(html).toContain('15'); // calls blocked
    expect(html).toContain('20'); // texts blocked
    expect(html).toContain('35'); // total events
  });

  it('renders voice protection stats', () => {
    const html = htmlRenderer.render(renderContext);
    expect(html).toContain('50'); // analyses run
    expect(html).toContain('3'); // threats detected
    expect(html).toContain('2'); // enrollments active
  });

  it('renders recommendations with priority styling', () => {
    const html = htmlRenderer.render(renderContext);
    expect(html).toContain('recommendation high');
    expect(html).toContain('Address Critical Exposures');
  });

  it('renders home title stats when present', () => {
    const html = htmlRenderer.render({
      ...renderContext,
      data: {
        ...mockData,
        homeTitleStats: {
          propertiesMonitored: 2,
          changesDetected: 1,
          alertsTriggered: 3,
        },
      },
    });
    expect(html).toContain('Home Title Monitoring');
    expect(html).toContain('Properties Monitored');
  });

  it('omits home title section when stats are undefined', () => {
    const html = htmlRenderer.render(renderContext);
    expect(html).not.toContain('Home Title Monitoring');
  });

  it('renders dashboard URL and report ID in footer', () => {
    const html = htmlRenderer.render(renderContext);
    expect(html).toContain('https://app.shieldai.com/reports/test-1');
    expect(html).toContain('test-1');
  });

  it('renders exposure sources table', () => {
    const html = htmlRenderer.render(renderContext);
    expect(html).toContain('breach1');
    expect(html).toContain('breach2');
  });
});
