#!/usr/bin/env node
/**
 * SessionStart hook: reminds Claude to index DOCS/*.md into context-mode.
 * Scans for DOCS/ or docs/ folder and lists .md files to index.
 */
import { readdirSync, statSync, existsSync } from 'fs';
import { join, relative } from 'path';

const projectRoot = process.env.PROJECT_DIR || process.cwd();

let docsDir = null;
for (const candidate of ['DOCS', 'docs']) {
  const fullPath = join(projectRoot, candidate);
  if (existsSync(fullPath)) {
    docsDir = fullPath;
    break;
  }
}

if (!docsDir) {
  process.exit(0);
}

function getMdFiles(dir) {
  const files = [];
  for (const entry of readdirSync(dir, { withFileTypes: true })) {
    const fullPath = join(dir, entry.name);
    if (entry.isDirectory()) {
      files.push(...getMdFiles(fullPath));
    } else if (entry.name.endsWith('.md')) {
      files.push(relative(projectRoot, fullPath).replace(/\\/g, '/'));
    }
  }
  return files;
}

const mdFiles = getMdFiles(docsDir);

if (mdFiles.length > 0) {
  const instructions = {
    result: "INDEX_DOCS_REMINDER",
    message: `Na starcie sesji zaindeksuj poniższe pliki dokumentacji do context-mode (ctx_index) aby oszczędzać okno kontekstu. Użyj pełnych ścieżek z PROJECT_DIR.`,
    files: mdFiles,
    projectDir: projectRoot.replace(/\\/g, '/')
  };
  console.log(JSON.stringify(instructions));
}
