import { describe, it, afterEach } from 'node:test';
import assert from 'node:assert/strict';
import { resolve, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { execFileSync } from 'node:child_process';
import { deleteBaseline } from '../../scanners/lib/baseline.mjs';

const __dirname = fileURLToPath(new URL('.', import.meta.url));
const FIXTURES = resolve(__dirname, '../fixtures');
const HEALTHY = resolve(FIXTURES, 'healthy-project');
const DRIFT_CLI = resolve(__dirname, '../../scanners/drift-cli.mjs');

const TEST_BASELINE = `_drift_test_${Date.now()}`;

afterEach(async () => {
  await deleteBaseline(TEST_BASELINE);
});

describe('drift-cli --save', () => {
  it('saves a baseline and confirms', () => {
    const result = execFileSync('node', [DRIFT_CLI, HEALTHY, '--save', '--name', TEST_BASELINE, '--json'], {
      encoding: 'utf-8',
      timeout: 30000,
    });
    const output = JSON.parse(result);
    assert.equal(output.saved, true);
    assert.equal(output.name, TEST_BASELINE);
    assert.ok(output.path);
  });
});

describe('drift-cli --list', () => {
  it('lists baselines including saved one', async () => {
    // Save first
    execFileSync('node', [DRIFT_CLI, HEALTHY, '--save', '--name', TEST_BASELINE], {
      encoding: 'utf-8',
      timeout: 30000,
    });

    const result = execFileSync('node', [DRIFT_CLI, '--list', '--json'], {
      encoding: 'utf-8',
      timeout: 30000,
    });
    const output = JSON.parse(result);
    assert.ok(Array.isArray(output.baselines));
    const found = output.baselines.find(b => b.name === TEST_BASELINE);
    assert.ok(found, 'Should find test baseline in list');
  });
});

describe('drift-cli compare', () => {
  it('outputs valid JSON with --json flag', () => {
    // Save baseline first
    execFileSync('node', [DRIFT_CLI, HEALTHY, '--save', '--name', TEST_BASELINE], {
      encoding: 'utf-8',
      timeout: 30000,
    });

    // Compare same fixture against itself
    const result = execFileSync('node', [DRIFT_CLI, HEALTHY, '--baseline', TEST_BASELINE, '--json'], {
      encoding: 'utf-8',
      timeout: 30000,
    });
    const diff = JSON.parse(result);
    assert.ok('newFindings' in diff);
    assert.ok('resolvedFindings' in diff);
    assert.ok('unchangedFindings' in diff);
    assert.ok('movedFindings' in diff);
    assert.ok('scoreChange' in diff);
    assert.ok('summary' in diff);
  });

  it('shows stable trend when comparing same fixture', () => {
    execFileSync('node', [DRIFT_CLI, HEALTHY, '--save', '--name', TEST_BASELINE], {
      encoding: 'utf-8',
      timeout: 30000,
    });

    const result = execFileSync('node', [DRIFT_CLI, HEALTHY, '--baseline', TEST_BASELINE, '--json'], {
      encoding: 'utf-8',
      timeout: 30000,
    });
    const diff = JSON.parse(result);
    assert.equal(diff.summary.trend, 'stable');
    assert.equal(diff.summary.newCount, 0);
    assert.equal(diff.summary.resolvedCount, 0);
  });

  it('exits with code 1 when baseline not found', () => {
    assert.throws(() => {
      execFileSync('node', [DRIFT_CLI, HEALTHY, '--baseline', `nonexistent_${Date.now()}`, '--json'], {
        encoding: 'utf-8',
        timeout: 30000,
      });
    }, (err) => {
      assert.equal(err.status, 1);
      return true;
    });
  });
});
