#!/bin/bash
# ─── dev.sh — Alterna entre modo dev e produção ───────────────────────────────
#
#  ./dev.sh up     → sobe em modo DEV  (tsx watch + next dev, sem build)
#  ./dev.sh down   → derruba tudo
#  ./dev.sh prod   → sobe em modo PROD (imagens buildadas)
#  ./dev.sh logs   → segue logs de backend e frontend
#
# ─────────────────────────────────────────────────────────────────────────────

DOCKER="DOCKER_HOST=unix:///run/user/1000/docker.sock /home/deploy/bin/docker"
DC="$DOCKER compose"
DC_DEV="$DC -f docker-compose.yml -f docker-compose.dev.yml"

cd "$(dirname "$0")"

case "$1" in
  up)
    echo "▶ Subindo em modo DEV (tsx watch + next dev)..."
    eval "$DC_DEV up -d newwhats-backend newwhats-frontend"
    echo ""
    echo "  Backend : tsx watch — edite ./newwhats.local/backend/src/**/*.ts"
    echo "  Frontend: next dev  — edite ./newwhats.local/frontend/**/*.tsx"
    echo ""
    echo "  Logs: ./dev.sh logs"
    ;;
  down)
    echo "■ Derrubando containers..."
    eval "$DC down"
    ;;
  prod)
    echo "▶ Subindo em modo PROD (imagens buildadas)..."
    eval "$DC up -d"
    ;;
  logs)
    eval "$DC logs -f --tail=60 newwhats-backend newwhats-frontend"
    ;;
  *)
    echo "Uso: ./dev.sh [up|down|prod|logs]"
    exit 1
    ;;
esac
