#!/usr/bin/env bash
# CluPilot dev helper — wraps docker compose so all tooling runs INSIDE the
# container (R8). Run from anywhere; it cd's into the project dir itself.
set -euo pipefail

# Resolve the project root from the script's own location (follows the symlink
# in ~/.local/bin), so this works for any checkout / user / CI — not a fixed path.
SCRIPT_PATH="$(readlink -f "${BASH_SOURCE[0]}")"
PROJECT_DIR="$(cd "$(dirname "$SCRIPT_PATH")/.." && pwd)"
cd "$PROJECT_DIR"

# tooling runs as the configured host UID/GID (from .env), falling back to the
# invoking user — so bind-mounted files keep the right owner on any host (R8).
_huid="$(sed -n 's/^HOST_UID=//p' .env 2>/dev/null | head -1)"; _huid="${_huid:-$(id -u)}"
_hgid="$(sed -n 's/^HOST_GID=//p' .env 2>/dev/null | head -1)"; _hgid="${_hgid:-$(id -g)}"
UIDGID="${_huid}:${_hgid}"
RUN=(docker compose run --rm --no-deps -u "$UIDGID" -e HOME=/tmp app)

usage() {
    cat <<'USAGE'
CluPilot helper — Docker-first dev commands

  clupilot up             Stack starten (-d)
  clupilot down           Stack stoppen
  clupilot restart        Alle Container neu starten
  clupilot restart <svc>  Einen Service neu starten (app|reverb|queue|scheduler|mariadb|redis)
  clupilot rebuild        App-Image neu bauen + Stack neu hoch
  clupilot ps             Status aller Container
  clupilot logs [svc]     Logs folgen (alle oder ein Service)
  clupilot health         Kurzer Health-Check (HTTP + Services)

  clupilot artisan <...>  php artisan <...> im Container
  clupilot composer <...> composer <...> im Container
  clupilot npm <...>      npm <...> im Container
  clupilot tinker         php artisan tinker
  clupilot migrate        php artisan migrate
  clupilot fresh          php artisan migrate:fresh  (ACHTUNG: leert DB)
  clupilot shell          bash im laufenden app-Container
USAGE
}

cmd="${1:-help}"; shift || true
case "$cmd" in
    up)       docker compose up -d ;;
    down)     docker compose down ;;
    restart)
        if [[ $# -gt 0 ]]; then docker compose restart "$@"; else docker compose restart; fi ;;
    rebuild)  docker compose build app && docker compose up -d ;;
    ps)       docker compose ps ;;
    logs)     if [[ $# -gt 0 ]]; then docker compose logs -f "$@"; else docker compose logs -f; fi ;;
    health)
        # APP_PORT may carry a bind address (127.0.0.1:8080), which is the
        # SAFE default — Docker publishes ahead of UFW. Take the port only, or
        # the health check builds http://localhost:127.0.0.1:8080/ and reports
        # the site down when it is fine.
        _aport="$(sed -n 's/^APP_PORT=//p' .env 2>/dev/null | head -1)"; # 8080, matching the compose default. Checking 80 would report a healthy
        # default deployment as unreachable.
        _aport="${_aport:-127.0.0.1:8080}"
        _aport="${_aport##*:}"
        printf 'HTTP  : '; curl -sS -o /dev/null -w '%{http_code}\n' "http://localhost:${_aport}/" || true
        docker compose ps ;;
    artisan)  docker compose exec -u "$UIDGID" app php artisan "$@" ;;
    composer) "${RUN[@]}" composer "$@" ;;
    npm)      "${RUN[@]}" npm "$@" ;;
    tinker)   docker compose exec -u "$UIDGID" app php artisan tinker ;;
    migrate)  docker compose exec -u "$UIDGID" app php artisan migrate "$@" ;;
    fresh)    docker compose exec -u "$UIDGID" app php artisan migrate:fresh "$@" ;;
    shell)    docker compose exec -u "$UIDGID" app bash ;;
    help|-h|--help) usage ;;
    *) echo "Unbekannt: $cmd"; echo; usage; exit 1 ;;
esac
