import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import { importCodeSymbols } from '../src/codeImporter';
import { writeFileSync, unlinkSync, existsSync } from 'fs';
import { join } from 'path';

describe('codeImporter', () => {
  const testFilePath = join(__dirname, 'test.ts');

  beforeEach(() => {
    // Clean up any existing test file
    if (existsSync(testFilePath)) {
      unlinkSync(testFilePath);
    }
  });

  afterEach(() => {
    // Clean up test file
    if (existsSync(testFilePath)) {
      unlinkSync(testFilePath);
    }
  });

  it('extracts a function from a TypeScript file', () => {
    const content = `
import { something } from 'somewhere';

export function targetFunction(x: number): number {
  return x * 2;
}

function otherFunction() {
  console.log('hello');
}
`;
    writeFileSync(testFilePath, content);

    const result = importCodeSymbols({
      filePath: testFilePath,
      symbolList: ['targetFunction']
    });

    expect(result.symbolsFound).toBe(true);
    expect(result.duplicateSymbols).toBe(false);
    expect(result.content.trim()).toBe(`# Code Symbols

\`\`\`typescript
export function targetFunction(x: number): number {
  return x * 2;
}
\`\`\``);
  });

  it('extracts multiple symbols in order', () => {
    const content = `
export interface MyInterface {
  prop: string;
}

export class MyClass {
  constructor() {}
}

export function myFunction() {
  return true;
}
`;
    writeFileSync(testFilePath, content);

    const result = importCodeSymbols({
      filePath: testFilePath,
      symbolList: ['MyInterface', 'MyClass', 'myFunction']
    });

    expect(result.symbolsFound).toBe(true);
    expect(result.duplicateSymbols).toBe(false);
    expect(result.content.trim()).toBe(`# Code Symbols

\`\`\`typescript
export interface MyInterface {
  prop: string;
}

export class MyClass {
  constructor() {}
}

export function myFunction() {
  return true;
}
\`\`\``);
  });

  it('handles single-line interface declarations', () => {
    const content = `
export interface Empty {}
export interface Extended extends Base;
`;
    writeFileSync(testFilePath, content);

    const result = importCodeSymbols({
      filePath: testFilePath,
      symbolList: ['Empty', 'Extended']
    });

    expect(result.symbolsFound).toBe(true);
    expect(result.duplicateSymbols).toBe(false);
    expect(result.content.trim()).toBe(`# Code Symbols

\`\`\`typescript
export interface Empty {}

export interface Extended extends Base;
\`\`\``);
  });

  it('detects duplicate symbols', () => {
    const content = `
export function duplicate() {
  return 1;
}

export class Other {}

export function duplicate() {
  return 2;
}
`;
    writeFileSync(testFilePath, content);

    const result = importCodeSymbols({
      filePath: testFilePath,
      symbolList: ['duplicate']
    });

    expect(result.symbolsFound).toBe(true);
    expect(result.duplicateSymbols).toBe(true);
    expect(result.content).toBe('');
  });

  it('returns empty content when symbol not found', () => {
    const content = `
export function existingFunction() {
  return true;
}
`;
    writeFileSync(testFilePath, content);

    const result = importCodeSymbols({
      filePath: testFilePath,
      symbolList: ['nonexistentFunction']
    });

    expect(result.symbolsFound).toBe(false);
    expect(result.duplicateSymbols).toBe(false);
    expect(result.content).toBe('');
  });

  it('throws error when file not found', () => {
    expect(() => importCodeSymbols({
      filePath: 'nonexistent.ts',
      symbolList: ['someFunction']
    })).toThrow('File not found');
  });

  it('adjusts heading level when requested', () => {
    const content = `
export function targetFunction() {
  return true;
}
`;
    writeFileSync(testFilePath, content);

    const result = importCodeSymbols({
      filePath: testFilePath,
      symbolList: ['targetFunction'],
      headingLevelOverride: '###'
    });

    expect(result.symbolsFound).toBe(true);
    expect(result.duplicateSymbols).toBe(false);
    expect(result.content.trim()).toBe(`### Code Symbols

\`\`\`typescript
export function targetFunction() {
  return true;
}
\`\`\``);
  });

  it('overrides heading text when requested', () => {
    const content = `
export function targetFunction() {
  return true;
}
`;
    writeFileSync(testFilePath, content);

    const result = importCodeSymbols({
      filePath: testFilePath,
      symbolList: ['targetFunction'],
      headingTextOverride: 'Functions'
    });

    expect(result.symbolsFound).toBe(true);
    expect(result.duplicateSymbols).toBe(false);
    expect(result.content.trim()).toBe(`# Functions

\`\`\`typescript
export function targetFunction() {
  return true;
}
\`\`\``);
  });

  it('adjusts both heading level and text when requested', () => {
    const content = `
export function targetFunction() {
  return true;
}
`;
    writeFileSync(testFilePath, content);

    const result = importCodeSymbols({
      filePath: testFilePath,
      symbolList: ['targetFunction'],
      headingLevelOverride: '###',
      headingTextOverride: 'Functions'
    });

    expect(result.symbolsFound).toBe(true);
    expect(result.duplicateSymbols).toBe(false);
    expect(result.content.trim()).toBe(`### Functions

\`\`\`typescript
export function targetFunction() {
  return true;
}
\`\`\``);
  });
}); 