import { describe, it, expect } from 'vitest';
import { PdfExporter } from './pdf';
import type { ScreenplayElement } from '../screenplay/types';

const sampleElements: ScreenplayElement[] = [
  { id: 'e1', type: 'sceneHeading', content: 'INT. COFFEE SHOP - DAY' },
  { id: 'e2', type: 'action', content: 'A bustling coffee shop. JESSICA sits alone at a corner table, staring at her laptop.' },
  { id: 'e3', type: 'character', content: 'Jessica' },
  { id: 'e4', type: 'dialogue', content: 'I need to make a decision today.' },
  { id: 'e5', type: 'parenthetical', content: 'to herself' },
  { id: 'e6', type: 'dialogue', content: 'There is no other option.' },
  { id: 'e7', type: 'transition', content: 'CUT TO:' },
  { id: 'e8', type: 'sceneHeading', content: 'EXT. PARK - NIGHT' },
  { id: 'e9', type: 'action', content: 'The park is empty. Streetlights flicker overhead.' },
  { id: 'e10', type: 'character', content: 'Marcus' },
  { id: 'e11', type: 'dialogue', content: 'She never came back.' },
];

describe('PdfExporter', () => {
  const exporter = new PdfExporter();

  it('supports pdf format', () => {
    expect(exporter.supportedFormats).toContain('pdf');
  });

  it('produces HTML output', () => {
    const result = exporter.export(sampleElements, { format: 'pdf' });
    expect(result.format).toBe('pdf');
    expect(result.data).toContain('<!DOCTYPE html>');
    expect(result.data).toContain('<html>');
    expect(result.data).toContain('</html>');
  });

  it('includes proper page setup CSS', () => {
    const result = exporter.export(sampleElements, { format: 'pdf' });
    expect(result.data).toContain('@page');
    expect(result.data).toContain('size: 8.5in 11in');
  });

  it('uses Courier font', () => {
    const result = exporter.export(sampleElements, { format: 'pdf' });
    expect(result.data).toContain('Courier');
  });

  it('uses 12pt font size', () => {
    const result = exporter.export(sampleElements, { format: 'pdf' });
    expect(result.data).toContain('font-size: 12pt');
  });

  it('renders scene headings in uppercase and bold', () => {
    const result = exporter.export(sampleElements, { format: 'pdf' });
    expect(result.data).toContain('INT. COFFEE SHOP - DAY');
    expect(result.data).toContain('font-weight: bold');
  });

  it('renders dialogue content', () => {
    const result = exporter.export(sampleElements, { format: 'pdf' });
    expect(result.data).toContain('I need to make a decision today.');
    expect(result.data).toContain('She never came back.');
  });

  it('renders character cues in uppercase', () => {
    const result = exporter.export(sampleElements, { format: 'pdf' });
    expect(result.data).toContain('JESSICA');
    expect(result.data).toContain('MARCUS');
  });

  it('includes page numbers', () => {
    const result = exporter.export(sampleElements, { format: 'pdf' });
    expect(result.data).toContain('1</div>');
  });

  it('includes cover page when requested', () => {
    const result = exporter.export(sampleElements, {
      format: 'pdf',
      title: 'Test Script',
      author: 'Jane Doe',
      includeCoverPage: true,
    });
    expect(result.data).toContain('Test Script');
    expect(result.data).toContain('by');
    expect(result.data).toContain('Jane Doe');
  });

  it('uses correct content type and extension', () => {
    const result = exporter.export(sampleElements, { format: 'pdf' });
    expect(result.contentType).toBe('application/pdf');
    expect(result.extension).toBe('.pdf');
  });

  it('generates filename from title', () => {
    const result = exporter.export(sampleElements, { format: 'pdf', title: 'My Script' });
    expect(result.filename).toBe('My_Script.pdf');
  });

  it('reports page count', () => {
    const result = exporter.export(sampleElements, { format: 'pdf' });
    expect(result.pageCount).toBeGreaterThan(0);
  });

  it('escapes HTML special characters', () => {
    const elements: ScreenplayElement[] = [
      { id: 'e1', type: 'action', content: 'A sign reads: "Hello & Goodbye"' },
    ];
    const result = exporter.export(elements, { format: 'pdf' });
    expect(result.data).toContain('&amp;');
    expect(result.data).toContain('&quot;');
  });

  it('handles empty elements array', () => {
    const result = exporter.export([], { format: 'pdf' });
    expect(result.pageCount).toBe(0);
  });

  it('supports gutter margin option', () => {
    const result = exporter.export(sampleElements, {
      format: 'pdf',
      gutterMargin: 0.5,
    });
    expect(result.data).toContain('1.5in');
  });

  it('supports sitcom template', () => {
    const result = exporter.export(sampleElements, {
      format: 'pdf',
      template: 'sitcom',
    });
    expect(result.data).toContain('font-weight: bold');
  });
});
