import { describe, it, expect } from 'vitest';
import { detectElementType, detectWithType, isSceneHeading, isTransition, isCharacter, isParenthetical, detectNamedEntities } from './detect';
import { ScreenplayElementType } from './types';

describe('detectElementType', () => {
  it('detects INT scene headings', () => {
    expect(detectElementType('INT. LIVING ROOM - DAY')).toBe('sceneHeading');
  });

  it('detects EXT scene headings', () => {
    expect(detectElementType('EXT. PARK - NIGHT')).toBe('sceneHeading');
  });

  it('detects INT/EXT scene headings', () => {
    expect(detectElementType('INT/EXT. BUILDING - DAY')).toBe('sceneHeading');
  });

  it('detects transitions', () => {
    expect(detectElementType('CUT TO:')).toBe('transition');
    expect(detectElementType('FADE IN:')).toBe('transition');
    expect(detectElementType('FADE OUT.')).toBe('transition');
    expect(detectElementType('DISSOLVE TO:')).toBe('transition');
  });

  it('detects parentheticals', () => {
    expect(detectElementType('(whispering)')).toBe('parenthetical');
    expect(detectElementType('(V.O.)')).toBe('parenthetical');
  });

  it('detects character names with indentation', () => {
    expect(detectElementType('        JOHN')).toBe('character');
    expect(detectElementType('            MARY')).toBe('character');
  });

  it('detects all-caps character names without spaces', () => {
    expect(detectElementType('JOHN')).toBe('character');
    expect(detectElementType('MARY-ANN')).toBe('character');
  });

  it('defaults to dialogue for normal text', () => {
    expect(detectElementType('Hello, how are you?')).toBe('dialogue');
  });

  it('treats empty lines as action', () => {
    expect(detectElementType('')).toBe('action');
    expect(detectElementType('   ')).toBe('action');
  });
});

describe('detectWithType', () => {
  it('returns high confidence for scene headings', () => {
    const result = detectWithType('INT. LIVING ROOM - DAY');
    expect(result.type).toBe('sceneHeading');
    expect(result.confidence).toBeCloseTo(0.95);
  });

  it('returns high confidence for transitions', () => {
    const result = detectWithType('CUT TO:');
    expect(result.type).toBe('transition');
    expect(result.confidence).toBeCloseTo(0.95);
  });

  it('returns medium confidence for ambiguous text', () => {
    const result = detectWithType('Hello world');
    expect(result.confidence).toBe(0.5);
  });
});

describe('helper functions', () => {
  it('isSceneHeading returns true for scene headings', () => {
    expect(isSceneHeading('INT. ROOM - DAY')).toBe(true);
    expect(isSceneHeading('HELLO')).toBe(false);
  });

  it('isTransition returns true for transitions', () => {
    expect(isTransition('FADE IN:')).toBe(true);
    expect(isTransition('HELLO')).toBe(false);
  });

  it('isCharacter returns true for character names', () => {
    expect(isCharacter('        JOHN')).toBe(true);
    expect(isCharacter('hello')).toBe(false);
  });

  it('isParenthetical returns true for parentheticals', () => {
    expect(isParenthetical('(smiling)')).toBe(true);
    expect(isParenthetical('hello')).toBe(false);
  });
});

describe('detectNamedEntities', () => {
  it('extracts character names from indented lines', () => {
    const names = detectNamedEntities('        JOHN DOE');
    expect(names).toContain('JOHN DOE');
  });

  it('extracts location from scene headings', () => {
    const names = detectNamedEntities('INT. LIVING ROOM - DAY');
    expect(names).toContain('LIVING ROOM - DAY');
  });

  it('returns empty array for regular text', () => {
    const names = detectNamedEntities('Hello world');
    expect(names).toEqual([]);
  });
});
