const assert = require('node:assert');
const fs = require('fs');
const path = require('path');
const { profileRepo, ARCHETYPES } = require('../repo-profiler');

const TEST_DIR = path.join(__dirname, 'temp-fixtures');

function setupFixture(name, files) {
  const dir = path.join(TEST_DIR, name);
  fs.mkdirSync(dir, { recursive: true });
  for (const [file, content] of Object.entries(files)) {
    fs.writeFileSync(path.join(dir, file), content);
  }
  return dir;
}

function teardownFixtures() {
  if (fs.existsSync(TEST_DIR)) {
    fs.rmSync(TEST_DIR, { recursive: true, force: true });
  }
}

async function runTests() {
  teardownFixtures();

  try {
    // 1. Test Infrastructure detection
    const infraDir = setupFixture('infra', {
      'Chart.yaml': 'apiVersion: v2\nname: my-chart',
      'main.tf': 'resource "aws_vpc" "main" {}'
    });
    let result = profileRepo(infraDir);
    assert.strictEqual(result.archetype, ARCHETYPES.INFRASTRUCTURE, 'Should detect Infrastructure');
    assert.ok(result.confidence > 0.8, 'Confidence should be high');

    // 2. Test Frontend SPA detection
    const frontendDir = setupFixture('frontend', {
      'package.json': JSON.stringify({
        dependencies: {
          react: '^18.0.0',
          'react-dom': '^18.0.0'
        },
        devDependencies: {
          vite: '^4.0.0'
        }
      })
    });
    result = profileRepo(frontendDir);
    assert.strictEqual(result.archetype, ARCHETYPES.FRONTEND, 'Should detect Frontend SPA');
    assert.ok(result.signals.includes('frontend_framework'));

    // 3. Test Backend API detection
    const backendDir = setupFixture('backend', {
      'package.json': JSON.stringify({
        dependencies: {
          express: '^4.18.2'
        }
      })
    });
    result = profileRepo(backendDir);
    assert.strictEqual(result.archetype, ARCHETYPES.BACKEND, 'Should detect Backend API');

    // 4. Test Library detection
    const libraryDir = setupFixture('library', {
      'package.json': JSON.stringify({
        main: 'index.js',
        exports: {
          '.': './index.js'
        },
        dependencies: {
          lodash: '^4.17.21'
        }
      })
    });
    result = profileRepo(libraryDir);
    assert.strictEqual(result.archetype, ARCHETYPES.LIBRARY, 'Should detect Library');

    // 5. Monorepo detection
    const monorepoDir = setupFixture('monorepo', {
      'package.json': JSON.stringify({
        workspaces: ['packages/*']
      }),
      'turbo.json': '{}'
    });
    result = profileRepo(monorepoDir);
    assert.strictEqual(result.archetype, ARCHETYPES.MONOREPO, 'Should detect Monorepo');

    console.log('✅ repo-profiler tests passed!');
  } finally {
    teardownFixtures();
  }
}

runTests().catch(err => {
  console.error(err);
  process.exit(1);
});
