import { describe, it, expect } from 'vitest';
import { ScreenplayProExporter } from './screenplay-pro';
import type { ScreenplayElement } from '../screenplay/types';

const sampleElements: ScreenplayElement[] = [
  { id: 'e1', type: 'sceneHeading', content: 'INT. COFFEE SHOP - DAY', page: 1, line: 1 },
  { id: 'e2', type: 'action', content: 'A bustling coffee shop.', page: 1, line: 3 },
  { id: 'e3', type: 'character', content: 'Jessica', page: 1, line: 5 },
  { id: 'e4', type: 'dialogue', content: 'Hello there.', page: 1, line: 6 },
  { id: 'e5', type: 'parenthetical', content: 'smiling', page: 1, line: 7 },
  { id: 'e6', type: 'transition', content: 'CUT TO:', page: 2, line: 1 },
];

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

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

  it('produces header section', () => {
    const result = exporter.export(sampleElements, {
      format: 'screenplay-pro',
      title: 'Test Script',
      author: 'Jane Doe',
    });
    expect(result.data).toContain('[HEADER]');
    expect(result.data).toContain('Version\t1.0');
    expect(result.data).toContain('Title\tTest Script');
    expect(result.data).toContain('Author\tJane Doe');
  });

  it('produces content section with type markers', () => {
    const result = exporter.export(sampleElements, { format: 'screenplay-pro' });
    expect(result.data).toContain('[CONTENT]');
    expect(result.data).toContain('[SCENE]');
    expect(result.data).toContain('[ACTION]');
    expect(result.data).toContain('[CHAR]');
    expect(result.data).toContain('[DIALOG]');
    expect(result.data).toContain('[PAREN]');
    expect(result.data).toContain('[TRANS]');
  });

  it('uses tab-separated values', () => {
    const result = exporter.export(sampleElements, { format: 'screenplay-pro' });
    const lines = (result.data as string).split('\n');
    const contentLines = lines.filter((l: string) => l.startsWith('[SCENE]') || l.startsWith('[ACTION]'));
    for (const line of contentLines) {
      expect(line).toContain('\t');
    }
  });

  it('includes page and line metadata', () => {
    const result = exporter.export(sampleElements, { format: 'screenplay-pro' });
    const lines = (result.data as string).split('\n');
    const sceneLine = lines.find((l: string) => l.startsWith('[SCENE]'));
    expect(sceneLine).toContain('1\t1');
  });

  it('includes end marker', () => {
    const result = exporter.export(sampleElements, { format: 'screenplay-pro' });
    expect(result.data).toContain('[END]');
  });

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

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

  it('includes contact when provided', () => {
    const result = exporter.export(sampleElements, {
      format: 'screenplay-pro',
      contact: 'jane@example.com',
    });
    expect(result.data).toContain('Contact\tjane@example.com');
  });

  it('includes template in header', () => {
    const result = exporter.export(sampleElements, {
      format: 'screenplay-pro',
      template: 'sitcom',
    });
    expect(result.data).toContain('Template\tsitcom');
  });

  it('handles empty elements array', () => {
    const result = exporter.export([], { format: 'screenplay-pro' });
    expect(result.data).toContain('[HEADER]');
    expect(result.data).toContain('[CONTENT]');
    expect(result.data).toContain('[END]');
  });
});
