/**
 * E2E Smoke Test: File Generation
 *
 * This test verifies that the code generation process actually writes files to disk.
 * It catches bugs like the v0.4.1 issue where generators returned relative paths
 * and files weren't being written.
 */

import * as fs from 'fs';
import * as path from 'path';
import { GenerationOrchestrator } from '../generators/generation-orchestrator';
import { Workflow } from '../types/workflow';

describe('E2E: File Generation Smoke Test', () => {
  const testOutputDir = path.join(__dirname, '../../.e2e-output/file-generation-smoke');
  let orchestrator: GenerationOrchestrator;

  beforeEach(() => {
    // Clean up test directory before each test
    if (fs.existsSync(testOutputDir)) {
      fs.rmSync(testOutputDir, { recursive: true, force: true });
    }

    orchestrator = new GenerationOrchestrator();
  });

  afterEach(() => {
    // Clean up test directory after each test
    if (fs.existsSync(testOutputDir)) {
      fs.rmSync(testOutputDir, { recursive: true, force: true });
    }
  });

  it('should actually write files to disk with absolute paths', async () => {
    // Create a minimal workflow for testing
    const minimalWorkflow: Workflow = {
      workflow: {
        id: 'e2e-smoke-test',
        version: '1.0.0',
        name: 'E2E Smoke Test',
        description: 'Minimal workflow for E2E testing',
      },
      stack: {
        api: {
          type: 'backend',
          technology: 'typescript/express',
          config: { port: 3000 },
        },
      },
      stages: {
        local: {
          infrastructure: {
            databases: { primary: 'postgresql://localhost/test' },
            services: {},
          },
        },
      },
    };

    // Generate code
    const result = await orchestrator.generate(minimalWorkflow, {
      outputDirectory: testOutputDir,
      dryRun: false,
      verbose: false,
    });

    // Verify files were actually generated
    expect(result.files.length).toBeGreaterThan(0);

    // Verify all returned paths are absolute
    result.files.forEach(file => {
      expect(path.isAbsolute(file.path)).toBe(true);
    });

    // Verify files actually exist on disk
    const filesOnDisk: string[] = [];
    result.files.forEach(file => {
      if (fs.existsSync(file.path)) {
        filesOnDisk.push(file.path);
      }
    });

    // At least some files should exist (this catches the v0.4.1 bug)
    expect(filesOnDisk.length).toBeGreaterThan(0);

    // Verify the files are in the correct directory
    filesOnDisk.forEach(filePath => {
      expect(filePath.startsWith(testOutputDir)).toBe(true);
    });

    // Verify files written count matches
    expect(result.written.length).toBeGreaterThan(0);
  });

  it('should write files with content matching returned data', async () => {
    const workflow: Workflow = {
      workflow: {
        id: 'content-test',
        version: '1.0.0',
        name: 'Content Test',
        description: 'Test workflow',
      },
      stack: {
        api: {
          type: 'backend',
          technology: 'typescript/express',
          config: { port: 3000 },
        },
      },
      stages: {
        local: {
          infrastructure: {
            databases: { primary: 'postgresql://localhost/test' },
            services: {},
          },
        },
      },
    };

    const result = await orchestrator.generate(workflow, {
      outputDirectory: testOutputDir,
      dryRun: false,
      verbose: false,
    });

    // Verify at least one file was written
    expect(result.written.length).toBeGreaterThan(0);

    // Pick the first file and verify content matches
    const firstFile = result.files[0];
    if (fs.existsSync(firstFile.path)) {
      const fileContent = fs.readFileSync(firstFile.path, 'utf-8');
      expect(fileContent).toBe(firstFile.content);
    }
  });
});
