const fs = require('fs');
const path = require('path');
const { generateDocs } = require('../sysdoc.js');
const GraphStore = require('../graph.js');

const FIXTURE_DIR = path.join(__dirname, 'fixtures/system-docs');
const FIXTURE_SRC = path.join(FIXTURE_DIR, 'src');
const SNAPSHOT = path.join(FIXTURE_DIR, 'snapshot.json');
const OUT_DIR = path.join(__dirname, 'tmp-docs');

let passed = 0;
let failed = 0;

function assert(condition, name) {
  if (condition) { passed++; console.log(`  ✓ ${name}`); }
  else { failed++; console.log(`  ✗ ${name}`); }
}

console.log('=== 7D: Hierarchical Doc Generator Tests ===\n');

// Clean and run
if (fs.existsSync(OUT_DIR)) {
  fs.rmSync(OUT_DIR, { recursive: true, force: true });
}

const graph = GraphStore.loadSnapshot(SNAPSHOT);
const result = generateDocs(graph, FIXTURE_SRC, OUT_DIR, {
  entryPoints: ['channels/telegram.ts:onTelegramMessage', 'gateway/session.ts:refreshSession'],
  srcDir: '/src/',
  minTraffic: 3,
  crossCuttingThreshold: 0.6
});

// Test 1: Output directory structure (Divio)
console.log('Test 1: Output directory structure');
assert(fs.existsSync(path.join(OUT_DIR, 'reference/subsystems')), 'reference/subsystems exists');
assert(fs.existsSync(path.join(OUT_DIR, 'reference/contracts')), 'reference/contracts exists');
assert(fs.existsSync(path.join(OUT_DIR, 'explanation')), 'explanation exists');
assert(fs.existsSync(path.join(OUT_DIR, 'diagrams')), 'diagrams exists');

// Test 2: System Architecture overview
console.log('\nTest 2: System Architecture document');
const sysArch = fs.readFileSync(path.join(OUT_DIR, 'reference/system-architecture.md'), 'utf8');
assert(sysArch.includes('# System Architecture'), 'Has title');
assert(sysArch.includes('- **gateway**'), 'Lists gateway subsystem');
assert(sysArch.includes('- **config**'), 'Lists config cross-cutting concern');
assert(sysArch.includes('graph TD'), 'Embeds Mermaid dependency diagram');

// Test 3: Subsystem Docs
console.log('\nTest 3: Per-Subsystem documents');
const gatewayDoc = fs.readFileSync(path.join(OUT_DIR, 'reference/subsystems/gateway.md'), 'utf8');
assert(gatewayDoc.includes('# Subsystem: gateway'), 'Gateway doc has title');
assert(gatewayDoc.includes('- `handleRequest`'), 'Gateway doc lists public exports');
assert(gatewayDoc.includes('classDiagram'), 'Gateway doc embeds contract diagram');
assert(gatewayDoc.includes('GatewayConfig'), 'Gateway doc includes its contracts');

// Test 4: Data Flows
console.log('\nTest 4: Data Flows document');
const flowsDoc = fs.readFileSync(path.join(OUT_DIR, 'explanation/data-flows.md'), 'utf8');
assert(flowsDoc.includes('# Data Flows'), 'Has title');
assert(flowsDoc.includes('channels/telegram.ts:onTelegramMessage'), 'Has first flow entry point');
assert(flowsDoc.includes('gateway/session.ts:refreshSession'), 'Has second flow entry point');
assert(flowsDoc.includes('sequenceDiagram'), 'Embeds Mermaid sequence diagrams');
assert(flowsDoc.includes('channels → utils → gateway → config'), 'Shows subsystem sequence');

// Test 5: Mermaid Artifacts
console.log('\nTest 5: Diagram files generated');
assert(fs.existsSync(path.join(OUT_DIR, 'diagrams/system-deps.mmd')), 'system-deps.mmd generated');
assert(fs.existsSync(path.join(OUT_DIR, 'diagrams/gateway-contracts.mmd')), 'gateway-contracts.mmd generated');
assert(fs.existsSync(path.join(OUT_DIR, 'diagrams/all-contracts.mmd')), 'all-contracts.mmd generated');
assert(fs.existsSync(path.join(OUT_DIR, 'diagrams/flow-0.mmd')), 'flow-0.mmd generated');

console.log(`\n=== Results: ${passed} passed, ${failed} failed ===`);
process.exit(failed > 0 ? 1 : 0);
