#!/usr/bin/env bash
set -euo pipefail

PLUGIN_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
RUNTIME_ROOT="$PLUGIN_ROOT/runtime/carbon"

usage() {
  printf '%s\n' \
    'CARBON plugin runtime' \
    '' \
    'usage: carbon-runtime <command> [arguments]' \
    '' \
    'commands:' \
    '  workspace      first-run project inspection, local state, focus, history, and relocation' \
    '  advise         read-only project analysis and CARBON workflow recommendations' \
    '  framework      product/API/change model, adaptive plans, regressions, reports, CI' \
    '  intelligence   readiness doctor, evidence graph, failure clusters, freshness, benchmark, report' \
    '  stats          project activity, usage, unit economics, savings scenarios, and fun metrics' \
    '  run            durable plan execution, findings, completion, and run reports' \
    '  plan-view      safe live execution-plan snapshots and adaptive time estimates' \
    '  browser        carbon-browser/v1 contract, adapter negotiation, and operation ledger' \
    '  vibium         native Vibium CLI/test compatibility plus CARBON generation APIs' \
    '  evidence       page artifacts, persona feedback, exploration, and analysis' \
    '  learn          unfamiliar-surface learning and candidate promotion' \
    '  record         browser-flow recording and evidence-qualified playback' \
    '  auto           bounded test-fix-retest loop state' \
    '  failure        copy-only failure injection and error-condition run state' \
    '  intent         prompt-defined tests and legacy conversion' \
    '  bake           evidence-gated imported-test execution, repair, and activation' \
    '  context        safe global username, credential, prompt, and document context' \
    '  command-report render a branded single-file HTML result for any CARBON command' \
    '  chatbot       persist, extend, inspect, and render large conversational test suites' \
    '  ai-upgrade     inspect automation and install optional AI screenshot quick-check support' \
    '  flowgen        synthetic flow inputs and candidate cases' \
    '  select-items   select relevant control and product-item test packs' \
    '  select-flows   select relevant user-flow packs' \
    '  audit-library  validate the plugin-level CARBON item library' \
    '  audit-flows    validate the plugin-level CARBON flow library'
}

command="${1:-help}"
if [[ $# -gt 0 ]]; then shift; fi

if [[ "$command" != "help" && "$command" != "-h" && "$command" != "--help" ]]; then
  node "$PLUGIN_ROOT/bin/carbon-analytics.mjs" "$command" >/dev/null 2>&1 &
fi

case "$command" in
  help|-h|--help) usage ;;
  workspace) exec python3 "$RUNTIME_ROOT/scripts/carbon_workspace.py" "$@" ;;
  advise) exec python3 "$RUNTIME_ROOT/scripts/carbon_advisor.py" "$@" ;;
  framework) exec python3 "$RUNTIME_ROOT/scripts/carbon_framework.py" "$@" ;;
  intelligence) exec python3 "$RUNTIME_ROOT/scripts/carbon_intelligence.py" "$@" ;;
  stats) exec python3 "$RUNTIME_ROOT/scripts/carbon_stats.py" "$@" ;;
  run) exec python3 "$RUNTIME_ROOT/scripts/carbon_run.py" "$@" ;;
  plan-view) exec python3 "$RUNTIME_ROOT/scripts/carbon_plan_view.py" "$@" ;;
  browser) exec python3 "$RUNTIME_ROOT/scripts/carbon_browser.py" "$@" ;;
  vibium) exec python3 "$RUNTIME_ROOT/scripts/carbon_vibium.py" "$@" ;;
  evidence) exec python3 "$RUNTIME_ROOT/scripts/carbon_evidence.py" "$@" ;;
  learn) exec python3 "$RUNTIME_ROOT/scripts/carbon_learn.py" "$@" ;;
  record) exec python3 "$RUNTIME_ROOT/scripts/carbon_record.py" "$@" ;;
  auto) exec python3 "$RUNTIME_ROOT/scripts/carbon_auto.py" "$@" ;;
  failure) exec python3 "$RUNTIME_ROOT/scripts/carbon_failure.py" "$@" ;;
  intent) exec python3 "$RUNTIME_ROOT/scripts/carbon_intent.py" "$@" ;;
  bake) exec python3 "$RUNTIME_ROOT/scripts/carbon_bake.py" "$@" ;;
  context) exec python3 "$RUNTIME_ROOT/scripts/carbon_context.py" "$@" ;;
  command-report) exec python3 "$RUNTIME_ROOT/scripts/carbon_command_report.py" "$@" ;;
  chatbot) exec python3 "$RUNTIME_ROOT/scripts/carbon_chatbot.py" "$@" ;;
  ai-upgrade) exec python3 "$RUNTIME_ROOT/scripts/carbon_ai_upgrade.py" "$@" ;;
  flowgen) exec python3 "$RUNTIME_ROOT/scripts/carbon_flowgen.py" "$@" ;;
  select-items) exec python3 "$RUNTIME_ROOT/scripts/select_items.py" "$@" ;;
  select-flows) exec python3 "$RUNTIME_ROOT/scripts/select_flows.py" "$@" ;;
  audit-library) exec python3 "$RUNTIME_ROOT/scripts/audit_library.py" "$@" ;;
  audit-flows) exec python3 "$RUNTIME_ROOT/scripts/audit_flows.py" "$@" ;;
  *) printf 'Unknown CARBON runtime command: %s\n\n' "$command" >&2; usage >&2; exit 2 ;;
esac
