#!/usr/bin/env node
/**
 * validate-conventions/index.ts — Entry point.
 * parse args → validate → execute checks → print JSON on stdout.
 *
 * Invoked by Claude via Bash. See sibling SKILL.md for the invocation contract.
 */
import { Command } from 'commander';
import { validateArgs } from './validate.js';
import { execute } from './execute.js';
import type { ValidateConventionsOutput } from './types.js';

const program = new Command();

program
  .name('validate-conventions')
  .description('Audit a SmartStack project for convention violations')
  .option('--project-path <path>', 'Absolute path of the SmartStack project to audit (defaults to cwd)')
  .option(
    '--checks <list>',
    'Comma-separated list of checks (namespaces, entities, controllers, all)',
    'all',
  )
  .option('--base-namespace <name>', 'Override the detected base namespace')
  .parse(process.argv);

const opts = program.opts();

const raw = {
  projectPath: opts.projectPath as string | undefined,
  checks: typeof opts.checks === 'string'
    ? opts.checks.split(',').map((s: string) => s.trim()).filter(Boolean)
    : ['all'],
  baseNamespace: opts.baseNamespace as string | undefined,
};

const validation = validateArgs(raw);
if (!validation.valid || !validation.args) {
  const output: ValidateConventionsOutput = {
    success: false,
    command: 'validate-conventions',
    report: null,
    errors: validation.errors,
    warnings: [],
    nextSteps: [],
  };
  console.log(JSON.stringify(output, null, 2));
  process.exit(1);
}

try {
  const result = await execute(validation.args);
  console.log(JSON.stringify(result, null, 2));
  process.exit(result.success && (result.data?.valid ?? false) ? 0 : 1);
} catch (err) {
  const message = err instanceof Error ? err.message : String(err);
  const output: ValidateConventionsOutput = {
    success: false,
    command: 'validate-conventions',
    report: null,
    errors: [`Unexpected error: ${message}`],
    warnings: [],
    nextSteps: [],
  };
  console.log(JSON.stringify(output, null, 2));
  process.exit(1);
}
