#!/usr/bin/env node
/**
 * Hygiene lint: fail if any *.bak or *.orig file is committed in server/src
 * or client/src. Run via `npm run lint:hygiene`.
 *
 * Exits 0 if clean, 1 if any forbidden files are found.
 */
const fs = require('fs');
const path = require('path');

const FORBIDDEN_PATTERNS = [/\.bak$/, /\.orig$/, /~$/];
const ROOTS = [
  path.join(__dirname, '..', 'src'),
  path.join(__dirname, '..', '..', 'client', 'src'),
];

function* walk(dir) {
  if (!fs.existsSync(dir)) return;
  for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
    const full = path.join(dir, entry.name);
    if (entry.isDirectory()) {
      // skip node_modules etc just in case
      if (entry.name === 'node_modules' || entry.name.startsWith('.')) continue;
      yield* walk(full);
    } else {
      yield full;
    }
  }
}

const offenders = [];
for (const root of ROOTS) {
  for (const file of walk(root)) {
    if (FORBIDDEN_PATTERNS.some((re) => re.test(file))) {
      offenders.push(file);
    }
  }
}

if (offenders.length === 0) {
  console.log('[lint:hygiene] OK — no .bak / .orig / ~ files in server/src or client/src');
  process.exit(0);
}

console.error('[lint:hygiene] FAILED — forbidden files found:');
for (const f of offenders) console.error('  ' + f);
console.error('\nMove these to Trash or rename to remove the suffix.');
process.exit(1);
