#!/usr/bin/env bash
# prospector — Task Runner
# Usage: ./run <command> [args...]
#
#   ./run install         One-shot local setup: deps, env, DB + migrations, build.
#                         Add --launch to open the app after. See scripts/install.sh.
#   ./run app             Launch API + panel and open it as a Chrome app window.
#   ./run db:migrate      Apply pending SQL migrations (ledger-tracked).
#
#   ./run install:mcp     Installs the prospector MCP (new backend adapter) into
#                         Claude Desktop + global claude (~/.claude/mcp-config.json).
#                         See mcp/install-mcp.sh for details + safety notes.
#
# Follows the convention used across @applications and @projects (e.g. cocottetech/run).

set -uo pipefail

REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# Colors (best effort)
RED=$'\e[31m' YELLOW=$'\e[33m' BLUE=$'\e[34m' GREEN=$'\e[32m' NC=$'\e[0m' || true

usage() {
    echo -e "${BLUE}prospector${NC} — Task Runner"
    echo ""
    echo "Usage: ./run <command> [args...]"
    echo ""
    echo -e "${YELLOW}Local install / run${NC}"
    echo "  install                  Setup deps, env, DB + migrations, build."
    echo "  install --launch         …then open the app (Chrome PWA window)."
    echo "  app                      Launch API + panel, open as Chrome app window."
    echo "  app --build              Rebuild first, then launch."
    echo "  stop                     Stop the detached app (API + panel)."
    echo "  tray                     Build the menu-bar tray app (~/Applications)."
    echo "  db:migrate               Apply pending SQL migrations (ledger-tracked)."
    echo ""
    echo -e "${YELLOW}MCP (agent / Claude Desktop + global claude)${NC}"
    echo "  install:mcp              Build + register 'prospector' MCP in both"
    echo "                           Claude Desktop and ~/.claude/mcp-config.json"
    echo "                           (quits Desktop safely first when possible)."
    echo "  install:mcp --help       See full options (NO_RELAUNCH, VENV, etc.)"
    echo ""
    echo -e "${YELLOW}Local dev (delegated to npm in this tree)${NC}"
    echo "  build                    npm run build"
    echo "  typecheck                npm run typecheck"
    echo "  test                     npm test"
    echo "  start:dev                npm run start:dev"
    echo ""
    echo "All other commands are passed through if a cmd_* exists or fall back to npm."
}

cmd_build()      { (cd "$REPO_ROOT" && npm run build "$@"); }
cmd_typecheck()  { (cd "$REPO_ROOT" && npm run typecheck "$@"); }
cmd_test()       { (cd "$REPO_ROOT" && npm test "$@"); }
cmd_start_dev()  { (cd "$REPO_ROOT" && npm run start:dev "$@"); }
cmd_install()    { exec "$REPO_ROOT/scripts/install.sh" "$@"; }
cmd_app()        { exec "$REPO_ROOT/scripts/app.sh" "$@"; }
cmd_stop()       { exec "$REPO_ROOT/scripts/stop.sh" "$@"; }
cmd_tray()       { exec "$REPO_ROOT/scripts/make-tray.sh" "$@"; }
cmd_db_migrate() { exec "$REPO_ROOT/scripts/migrate.sh" "$@"; }

# The real work lives in the dedicated installer (easy to run directly too).
cmd_install_mcp() {
    shift || true
    exec "$REPO_ROOT/mcp/install-mcp.sh" "$@"
}

# Dispatch
COMMAND="${1:-}"
shift 2>/dev/null || true

case "$COMMAND" in
    help|--help|-h|"") usage; exit 0 ;;
    install:mcp|install-mcp) cmd_install_mcp install:mcp "$@"; exit $? ;;
    install)                 cmd_install "$@"; exit $? ;;
    app)                     cmd_app "$@"; exit $? ;;
    stop)                    cmd_stop "$@"; exit $? ;;
    tray)                    cmd_tray "$@"; exit $? ;;
    db:migrate|db-migrate)   cmd_db_migrate "$@"; exit $? ;;
    build)                   cmd_build "$@"; exit $? ;;
    typecheck)               cmd_typecheck "$@"; exit $? ;;
    test)                    cmd_test "$@"; exit $? ;;
    start:dev)               cmd_start_dev "$@"; exit $? ;;
    *)
        # Unknown: try npm script if it exists, otherwise usage
        if (cd "$REPO_ROOT" && npm run | grep -q "^  $COMMAND\$"); then
            (cd "$REPO_ROOT" && npm run "$COMMAND" -- "$@")
            exit $?
        fi
        echo -e "${RED}Unknown command: $COMMAND${NC}" >&2
        usage
        exit 1
        ;;
esac
