#!/bin/bash
# ============================================================
# remote.sh – Schnellbefehle für den Deployment-Server
# Usage:
#   ./remote.sh logs          → Live-Logs
#   ./remote.sh status        → Service-Status
#   ./remote.sh restart       → Service neu starten
#   ./remote.sh migrate       → alembic upgrade head
#   ./remote.sh shell         → SSH Shell öffnen
#   ./remote.sh psql          → PostgreSQL Shell
#   ./remote.sh test          → Tests auf dem Server ausführen
#   ./remote.sh env           → .env auf Server bearbeiten
# ============================================================

SERVER="root@192.168.1.137"
REMOTE="/opt/timemaster"

case "$1" in
  logs)
    ssh "$SERVER" "journalctl -u timemaster -f"
    ;;
  status)
    ssh "$SERVER" "systemctl status timemaster --no-pager"
    ;;
  restart)
    ssh "$SERVER" "systemctl restart timemaster && systemctl is-active timemaster"
    ;;
  migrate)
    ssh "$SERVER" "cd $REMOTE/backend && source venv/bin/activate && alembic upgrade head"
    ;;
  shell)
    ssh "$SERVER"
    ;;
  psql)
    ssh -t "$SERVER" "sudo -u postgres psql timemaster_db"
    ;;
  test)
    ssh "$SERVER" "cd $REMOTE/backend && source venv/bin/activate && pip install aiosqlite -q && pytest -v"
    ;;
  env)
    ssh -t "$SERVER" "nano $REMOTE/backend/.env"
    ;;
  health)
    curl -s http://192.168.1.137:8000/health | python3 -m json.tool
    ;;
  *)
    echo "Usage: ./remote.sh [logs|status|restart|migrate|shell|psql|test|env|health]"
    ;;
esac
