import { describe, it, beforeEach } from 'node:test';
import assert from 'node:assert/strict';
import { resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { resetCounter } from '../../scanners/lib/output.mjs';
import { discoverConfigFiles } from '../../scanners/lib/file-discovery.mjs';
import { scan } from '../../scanners/settings-validator.mjs';

const __dirname = fileURLToPath(new URL('.', import.meta.url));
const FIXTURES = resolve(__dirname, '../fixtures');

describe('SET scanner — healthy project', () => {
  let result;
  beforeEach(async () => {
    resetCounter();
    const discovery = await discoverConfigFiles(resolve(FIXTURES, 'healthy-project'));
    result = await scan(resolve(FIXTURES, 'healthy-project'), discovery);
  });

  it('returns status ok', () => {
    assert.strictEqual(result.status, 'ok');
  });

  it('has scanner prefix SET', () => {
    assert.strictEqual(result.scanner, 'SET');
  });

  it('finds no critical issues', () => {
    const critical = result.findings.filter(f => f.severity === 'critical');
    assert.strictEqual(critical.length, 0);
  });

  it('all finding IDs match CA-SET-NNN', () => {
    for (const f of result.findings) {
      assert.match(f.id, /^CA-SET-\d{3}$/);
    }
  });
});

describe('SET scanner — broken project', () => {
  let result;
  beforeEach(async () => {
    resetCounter();
    const discovery = await discoverConfigFiles(resolve(FIXTURES, 'broken-project'));
    result = await scan(resolve(FIXTURES, 'broken-project'), discovery);
  });

  it('detects unknown settings key', () => {
    const found = result.findings.some(f => f.title === 'Unknown settings key');
    assert.ok(found, 'Should detect unknownKey123');
  });

  it('detects deprecated key (includeCoAuthoredBy)', () => {
    const found = result.findings.some(f => f.title === 'Deprecated settings key');
    assert.ok(found, 'Should detect includeCoAuthoredBy');
  });

  it('detects type mismatch (alwaysThinkingEnabled as string)', () => {
    const found = result.findings.some(f => f.title === 'Type mismatch in settings');
    assert.ok(found, 'Should detect boolean/string mismatch');
  });

  it('detects invalid effortLevel value', () => {
    const found = result.findings.some(f => f.title === 'Invalid effortLevel value');
    assert.ok(found, 'Should detect effortLevel "turbo"');
  });

  it('detects hooks as array', () => {
    const found = result.findings.some(f => f.title.includes('array instead of object'));
    assert.ok(found, 'Should detect hooks array format');
  });

  it('marks hooks-as-array as critical', () => {
    const f = result.findings.find(f => f.title.includes('array instead of object'));
    assert.strictEqual(f?.severity, 'critical');
  });
});

describe('SET scanner — empty project', () => {
  let result;
  beforeEach(async () => {
    resetCounter();
    const discovery = await discoverConfigFiles(resolve(FIXTURES, 'empty-project'));
    result = await scan(resolve(FIXTURES, 'empty-project'), discovery);
  });

  it('returns skipped when no settings files', () => {
    assert.strictEqual(result.status, 'skipped');
  });

  it('has 0 findings', () => {
    assert.strictEqual(result.findings.length, 0);
  });
});
