const fs = require('fs');
const path = require('path');

const bundle = process.argv[2];
if (!bundle) {
  console.error("Please specify a bundle name (e.g., node sweep_step1.js dev)");
  process.exit(1);
}

const bundleRoot = process.env.IRIS_BUNDLE_ROOT;
if (!bundleRoot) {
  console.error("Error: Environment variable IRIS_BUNDLE_ROOT is not defined.");
  process.exit(1);
}

const bundleDir = path.join(bundleRoot, bundle);
const wikiDir = path.join(bundleDir, 'wiki');
const sourcesDir = path.join(bundleDir, 'raw_sources');

if (!fs.existsSync(wikiDir)) {
  console.error(`Wiki directory not found: ${wikiDir}`);
  process.exit(1);
}
if (!fs.existsSync(sourcesDir)) {
  console.error(`Sources directory not found: ${sourcesDir}`);
  process.exit(1);
}

const reservedFiles = ['index.md', 'agents.md', 'claude.md'];

// Helper to walk directory
function getAllFiles(dir, ext, fileList = []) {
  if (!fs.existsSync(dir)) return fileList;
  const files = fs.readdirSync(dir);
  files.forEach(file => {
    const filePath = path.join(dir, file);
    if (fs.statSync(filePath).isDirectory()) {
      getAllFiles(filePath, ext, fileList);
    } else if (file.endsWith(ext)) {
      fileList.push(filePath);
    }
  });
  return fileList;
}

// 1. Collect Concept IDs from wiki/
const wikiFiles = getAllFiles(wikiDir, '.md');
const wikiConceptIds = [];

wikiFiles.forEach(file => {
  const relPath = path.relative(wikiDir, file).replace(/\\/g, '/');
  if (reservedFiles.includes(relPath.toLowerCase())) return;

  // Concept ID: relative path under wiki/ without .md, converted to lowercase
  const conceptId = relPath.replace(/\.md$/, '').toLowerCase();
  wikiConceptIds.push(conceptId);
});

// 2. Collect Concept IDs from metadata json
const metadataFiles = getAllFiles(sourcesDir, '-metadata.json');
const activeConceptIds = new Set();

metadataFiles.forEach(file => {
  try {
    const meta = JSON.parse(fs.readFileSync(file, 'utf8'));
    if (Array.isArray(meta.created_concepts)) {
      meta.created_concepts.forEach(id => {
        activeConceptIds.add(id.toLowerCase());
      });
    }
  } catch (err) {
    console.error(`Error reading metadata file ${file}:`, err);
  }
});

console.log("=== Step 1 Results ===");
console.log(`Bundle: ${bundle}`);
console.log(`\n[Wiki Concept IDs (${wikiConceptIds.length})]`);
wikiConceptIds.sort().forEach(id => console.log(`  - ${id}`));

console.log(`\n[Active Concept IDs in Metadata (${activeConceptIds.size})]`);
Array.from(activeConceptIds).sort().forEach(id => console.log(`  - ${id}`));
