#!/usr/bin/env node
/**
 * Dashboard 精简 CLI — spec / list
 *   npx tfds-dashboard spec smart-line-chart
 */
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import {
  cmdSpec,
  cmdList,
  printCliHelp,
  resolveSkillDir,
} from './lib/tfds-spec-core.mjs';

const CLI = 'tfds-dashboard';
const SKILL_NAME = '产品设计-体服平台数据看板-界面生成';
const pkgRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
const skillDir = resolveSkillDir(pkgRoot, SKILL_NAME);

function fail(msg) {
  console.error(`[${CLI}] ${msg}`);
  process.exit(1);
}

function main() {
  const [cmd, ...args] = process.argv.slice(2);
  try {
    switch (cmd) {
      case 'spec': {
        const patternIdx = args.findIndex((a) => a === '--pattern' || a === '-p');
        const query = patternIdx >= 0 ? args[patternIdx + 1] : args[0];
        cmdSpec({ skillDir, query, patternMode: patternIdx >= 0, cliName: CLI });
        return;
      }
      case 'list':
        cmdList({ skillDir, kind: args[0] });
        return;
      case undefined:
      case 'help':
      case '-h':
      case '--help':
        printCliHelp(CLI);
        return;
      default:
        fail(`未知命令：${cmd}`);
    }
  } catch (error) {
    fail(error.message || error);
  }
}

main();
