import { describe, it, expect, afterEach } from 'vitest';
import { writeFileSync, unlinkSync } from 'fs';
import { join } from 'path';
import { importMarkdownSection } from '../src/markdownImporter';

describe('markdownImporter', () => {
  const testFilePath = join(__dirname, 'test-temp.md');

  afterEach(() => {
    try {
      unlinkSync(testFilePath);
    } catch (error) {
      // Ignore errors if file doesn't exist
    }
  });

  it('extracts a section from a markdown file', () => {
    const content = `
# First Heading
Some content here.

## Installation
Step 1: Do this
Step 2: Do that

# Next Section
More content.
`;
    writeFileSync(testFilePath, content);

    const result = importMarkdownSection({
      filePath: testFilePath,
      heading: 'Installation'
    });

    expect(result.headingFound).toBe(true);
    expect(result.duplicateHeadings).toBe(false);
    expect(result.content.trim()).toBe(`## Installation
Step 1: Do this
Step 2: Do that`);
  });

  it('extracts a section until the end if no next heading', () => {
    const content = `
# First Heading
Some content here.

## Installation
Step 1: Do this
Step 2: Do that
`;
    writeFileSync(testFilePath, content);

    const result = importMarkdownSection({
      filePath: testFilePath,
      heading: 'Installation'
    });

    expect(result.headingFound).toBe(true);
    expect(result.duplicateHeadings).toBe(false);
    expect(result.content.trim()).toBe(`## Installation
Step 1: Do this
Step 2: Do that`);
  });

  it('ignores headings in code blocks', () => {
    const content = `
# First Heading
Some content here.

## Installation
Step 1: Do this
\`\`\`
## Not a real heading
\`\`\`
Step 2: Do that

# Next Section
More content.
`;
    writeFileSync(testFilePath, content);

    const result = importMarkdownSection({
      filePath: testFilePath,
      heading: 'Installation'
    });

    expect(result.headingFound).toBe(true);
    expect(result.duplicateHeadings).toBe(false);
    expect(result.content.trim()).toBe(`## Installation
Step 1: Do this
\`\`\`
## Not a real heading
\`\`\`
Step 2: Do that`);
  });

  it('detects duplicate headings', () => {
    const content = `
# First Heading
Some content here.

## Installation
Step 1: Do this

## Installation
Step 2: Do that

# Next Section
More content.
`;
    writeFileSync(testFilePath, content);

    const result = importMarkdownSection({
      filePath: testFilePath,
      heading: 'Installation'
    });

    expect(result.headingFound).toBe(true);
    expect(result.duplicateHeadings).toBe(true);
  });

  it('returns empty content when heading not found', () => {
    const content = `
# First Heading
Some content here.

## Installation
Step 1: Do this
Step 2: Do that

# Next Section
More content.
`;
    writeFileSync(testFilePath, content);

    const result = importMarkdownSection({
      filePath: testFilePath,
      heading: 'Nonexistent'
    });

    expect(result.headingFound).toBe(false);
    expect(result.duplicateHeadings).toBe(false);
    expect(result.content).toBe('');
  });

  it('throws error when file not found', () => {
    expect(() => importMarkdownSection({
      filePath: 'nonexistent.md',
      heading: 'Installation'
    })).toThrow('File not found');
  });

  it('imports the entire file when no heading is provided', () => {
    const content = `# First Heading
Some content here.

## Installation
Step 1: Do this
Step 2: Do that

# Next Section
More content.`;
    writeFileSync(testFilePath, content);

    const result = importMarkdownSection({
      filePath: testFilePath
    });

    expect(result.headingFound).toBe(true);
    expect(result.duplicateHeadings).toBe(false);
    expect(result.content).toBe(content);
  });

  it('continues through subheadings until next heading of same level', () => {
    const content = `# First Heading
Some content here.

## Installation
Step 1: Do this

### Subheading
More details

#### Deep heading
Even more details

## Next Section
Different content.`;
    writeFileSync(testFilePath, content);

    const result = importMarkdownSection({
      filePath: testFilePath,
      heading: 'Installation'
    });

    expect(result.headingFound).toBe(true);
    expect(result.duplicateHeadings).toBe(false);
    expect(result.content).toContain('## Installation');
    expect(result.content).toContain('### Subheading');
    expect(result.content).toContain('#### Deep heading');
    expect(result.content).not.toContain('## Next Section');
  });
}); 