#!/usr/bin/env bash
# memento-vault CLI wrapper
# Entry point for Homebrew and manual installations.
#
# Usage:
#   memento-vault install [--experimental] [--mcp] [--remote URL] [--reinstall] [--force]
#   memento-vault uninstall
#   memento-vault update          # git pull + reinstall
#   memento-vault warmup          # warm optional local dependencies quietly
#   memento-vault health          # run read-only vault diagnostics
#   memento-vault doctor          # alias for health
#   memento-vault version
set -euo pipefail

# Resolve the repo root (bin/ lives one level below it), following symlinks.
SCRIPT_PATH="${BASH_SOURCE[0]}"
while [ -L "$SCRIPT_PATH" ]; do
    SCRIPT_LINK_DIR="$(cd "$(dirname "$SCRIPT_PATH")" && pwd)"
    SCRIPT_TARGET="$(readlink "$SCRIPT_PATH")"
    case "$SCRIPT_TARGET" in
        /*) SCRIPT_PATH="$SCRIPT_TARGET" ;;
        *) SCRIPT_PATH="$SCRIPT_LINK_DIR/$SCRIPT_TARGET" ;;
    esac
done
SCRIPT_DIR="$(cd "$(dirname "$SCRIPT_PATH")/.." && pwd)"

# Homebrew installs to a prefix — check both layouts
if [ ! -f "$SCRIPT_DIR/install.sh" ]; then
    # Homebrew cellar layout: libexec/memento-vault/install.sh
    if [ -f "$SCRIPT_DIR/libexec/memento-vault/install.sh" ]; then
        SCRIPT_DIR="$SCRIPT_DIR/libexec/memento-vault"
    else
        echo "Error: cannot find memento-vault installation at $SCRIPT_DIR" >&2
        exit 1
    fi
fi

usage() {
    echo "Usage: memento-vault <command> [options]"
    echo ""
    echo "Commands:"
    echo "  install     Install hooks, skills, and agents into Claude Code"
    echo "  uninstall   Remove memento-vault from Claude Code"
    echo "  update      Pull latest version and reinstall"
    echo "  warmup      Warm optional local dependencies quietly (defaults to QMD)"
    echo "  health      Run read-only vault diagnostics"
    echo "  doctor      Alias for health"
    echo "  version     Show installed version"
    echo ""
    echo "Install options:"
    echo "  --experimental   Enable Tenet retrieval + Inception consolidation"
    echo "  --mcp            Install MCP server config"
    echo "  --remote URL     Connect to a remote vault"
    echo "  --reinstall      Safely rerun same-version install"
    echo "  --force          DANGEROUS: overwrite local edits to memento-managed files"
}

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

case "$cmd" in
    install)
        exec "$SCRIPT_DIR/install.sh" "$@"
        ;;
    uninstall)
        exec "$SCRIPT_DIR/uninstall.sh" "$@"
        ;;
    update)
        if [ -d "$SCRIPT_DIR/.git" ]; then
            echo "Updating memento-vault..."
            git -C "$SCRIPT_DIR" pull --ff-only 2>/dev/null || git -C "$SCRIPT_DIR" pull --rebase
            echo ""
            exec "$SCRIPT_DIR/install.sh" "$@"
        else
            echo "Error: not a git checkout — reinstall via brew or bootstrap" >&2
            exit 1
        fi
        ;;
    warmup)
        for opt in "$@"; do
            case "$opt" in
                --qmd|--quiet|-q) ;;
                --help|-h)
                    echo "Usage: memento-vault warmup [--qmd] [--quiet]"
                    echo ""
                    echo "Warms optional local dependencies quietly. Defaults to QMD."
                    exit 0
                    ;;
                *)
                    echo "Unknown warmup option: $opt" >&2
                    exit 1
                    ;;
            esac
        done

        if ! qmd_path="$(command -v qmd 2>/dev/null)"; then
            exit 0
        fi

        nohup "$qmd_path" vsearch "warmup" -c "${MEMENTO_QMD_COLLECTION:-memento}" -n 1 </dev/null >/dev/null 2>&1 &
        exit 0
        ;;
    health|doctor)
        export PYTHONPATH="$SCRIPT_DIR${PYTHONPATH:+:$PYTHONPATH}"
        exec python3 -m memento.health "$@"
        ;;
    version|-v|--version)
        cat "$SCRIPT_DIR/VERSION" 2>/dev/null || echo "unknown"
        ;;
    help|-h|--help)
        usage
        ;;
    "")
        usage
        exit 1
        ;;
    *)
        echo "Unknown command: $cmd" >&2
        echo ""
        usage
        exit 1
        ;;
esac
