import { describe, it, expect } from 'vitest';
import { FountainExporter } from './fountain';
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.' },
  { 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: 'transition', content: 'CUT TO:' },
  { id: 'e7', type: 'sceneHeading', content: 'EXT. PARK - NIGHT' },
  { id: 'e8', type: 'action', content: 'The park is empty. Streetlights flicker.' },
  { id: 'e9', type: 'character', content: 'Marcus' },
  { id: 'e10', type: 'dialogue', content: 'She never came back.' },
  { id: 'e11', type: 'note', content: 'TODO: Add flashback sequence' },
  { id: 'e12', type: 'centered', content: 'THE END' },
];

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

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

  it('exports scene headings with # marker', () => {
    const result = exporter.export(sampleElements, { format: 'fountain' });
    expect(result.format).toBe('fountain');
    expect(result.data).toContain('# INT. COFFEE SHOP - DAY');
    expect(result.data).toContain('# EXT. PARK - NIGHT');
  });

  it('exports characters with = markers', () => {
    const result = exporter.export(sampleElements, { format: 'fountain' });
    expect(result.data).toContain('= JESSICA =');
    expect(result.data).toContain('= MARCUS =');
  });

  it('exports parentheticals in parentheses', () => {
    const result = exporter.export(sampleElements, { format: 'fountain' });
    expect(result.data).toContain('(to herself)');
  });

  it('exports transitions with -> marker', () => {
    const result = exporter.export(sampleElements, { format: 'fountain' });
    expect(result.data).toContain('-> CUT TO:');
  });

  it('exports notes with > marker', () => {
    const result = exporter.export(sampleElements, { format: 'fountain' });
    expect(result.data).toContain('> TODO: Add flashback sequence');
  });

  it('exports action with 2-space indent', () => {
    const result = exporter.export(sampleElements, { format: 'fountain' });
    expect(result.data).toContain('  A bustling coffee shop');
  });

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

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

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

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

  it('returns dialogue without markers', () => {
    const result = exporter.export(sampleElements, { format: 'fountain' });
    expect(result.data).toContain('I need to make a decision today.');
  });
});
