#!/usr/bin/env bash
# =============================================================================
# AR-Autopilot — developer convenience script (Git Bash / WSL / Linux)
# =============================================================================
#
# Mirror of scripts/dev.ps1 for Bash environments. From the repo root:
#
#   bash scripts/dev.sh <task> [args...]
#
# Tasks: install | test | test-cov | lint | fix | format | typecheck
#        check | demo | clean | help
# =============================================================================

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
cd "$REPO_ROOT"

VENV_DIR="$REPO_ROOT/.venv"
if [[ -x "$VENV_DIR/Scripts/python.exe" ]]; then
    # Windows-style venv (Git Bash on Windows)
    PYTHON="$VENV_DIR/Scripts/python.exe"
    PYTEST="$VENV_DIR/Scripts/pytest.exe"
    RUFF="$VENV_DIR/Scripts/ruff.exe"
    MYPY="$VENV_DIR/Scripts/mypy.exe"
else
    PYTHON="$VENV_DIR/bin/python"
    PYTEST="$VENV_DIR/bin/pytest"
    RUFF="$VENV_DIR/bin/ruff"
    MYPY="$VENV_DIR/bin/mypy"
fi

assert_venv() {
    if [[ ! -x "$PYTHON" ]]; then
        echo "Virtual environment not found at $VENV_DIR" >&2
        echo "Run: bash scripts/dev.sh install" >&2
        exit 1
    fi
}

task_install() {
    if [[ ! -x "$PYTHON" ]]; then
        echo "[install] Creating virtual environment at $VENV_DIR ..."
        python -m venv "$VENV_DIR" || py -m venv "$VENV_DIR"
    fi
    echo "[install] Upgrading pip ..."
    "$PYTHON" -m pip install --upgrade pip
    echo "[install] Installing arautopilot[dev] in editable mode ..."
    "$PYTHON" -m pip install -e ".[dev]"
    echo "[install] OK"
}

task_test()       { assert_venv; "$PYTEST" "$@"; }
task_test_cov()   { assert_venv; "$PYTEST" --cov=arautopilot --cov-report=term-missing --cov-report=html "$@"; }
task_lint()       { assert_venv; "$RUFF" check arautopilot/; }
task_fix()        { assert_venv; "$RUFF" check arautopilot/ --fix; "$RUFF" format arautopilot/; }
task_format()     { assert_venv; "$RUFF" format arautopilot/; }
task_typecheck()  { assert_venv; "$MYPY" arautopilot/core arautopilot/library arautopilot/shared; }

task_check() {
    assert_venv
    echo "[check] (1/3) lint ..."
    "$RUFF" check arautopilot/
    echo "[check] (2/3) typecheck ..."
    "$MYPY" arautopilot/core arautopilot/library arautopilot/shared
    echo "[check] (3/3) tests ..."
    "$PYTEST"
    echo "[check] OK"
}

task_demo() { assert_venv; "$PYTHON" examples/sprint0_demo.py; }

task_clean() {
    find . -type d \( \
        -name '__pycache__' -o \
        -name '.pytest_cache' -o \
        -name '.mypy_cache' -o \
        -name '.ruff_cache' -o \
        -name 'build' -o \
        -name 'dist' -o \
        -name '*.egg-info' -o \
        -name 'htmlcov' \
    \) -not -path './.venv/*' -prune -exec rm -rf {} + 2>/dev/null || true
    find . -type f -name '*.pyc' -not -path './.venv/*' -delete 2>/dev/null || true
    rm -f .coverage
    rm -rf examples/output
    echo "[clean] OK"
}

task_help() {
    cat <<'HELP'
AR-Autopilot — dev tasks

Usage: bash scripts/dev.sh <task> [args...]

Tasks:
  install     Install package + dev deps into .venv
  test        Run pytest
  test-cov    Run pytest with coverage report
  lint        Run ruff check (read-only)
  fix         Run ruff check --fix + ruff format
  format      Run ruff format
  typecheck   Run mypy --strict
  check       lint + typecheck + test (full quality gate)
  demo        Run examples/sprint0_demo.py
  clean       Remove build / cache artefacts
  help        Show this message
HELP
}

task="${1:-help}"
shift || true
case "$task" in
    install)    task_install "$@" ;;
    test)       task_test "$@" ;;
    test-cov)   task_test_cov "$@" ;;
    lint)       task_lint "$@" ;;
    fix)        task_fix "$@" ;;
    format)     task_format "$@" ;;
    typecheck)  task_typecheck "$@" ;;
    check)      task_check "$@" ;;
    demo)       task_demo "$@" ;;
    clean)      task_clean "$@" ;;
    help|-h|--help) task_help ;;
    *)
        echo "Unknown task: $task" >&2
        task_help
        exit 2
        ;;
esac
