#!/bin/bash
# Development helper script for HoneyDue API (Go)

set -e

COMPOSE_FILES="-f docker-compose.yml -f docker-compose.dev.yml"

case "$1" in
  up)
    echo "🚀 Starting development environment..."
    docker-compose $COMPOSE_FILES up
    ;;
  down)
    echo "⏹️  Stopping development environment..."
    docker-compose $COMPOSE_FILES down
    ;;
  logs)
    if [ -n "$2" ]; then
      docker-compose $COMPOSE_FILES logs -f "$2"
    else
      docker-compose $COMPOSE_FILES logs -f
    fi
    ;;
  restart)
    echo "🔄 Restarting development environment..."
    docker-compose $COMPOSE_FILES restart
    ;;
  build)
    echo "🔨 Rebuilding containers..."
    docker-compose $COMPOSE_FILES up --build
    ;;
  bash)
    echo "🐚 Opening bash in API container..."
    docker-compose $COMPOSE_FILES exec api sh
    ;;
  test)
    echo "🧪 Running tests..."
    go test -v ./...
    ;;
  test-docker)
    echo "🧪 Running tests in Docker..."
    docker-compose $COMPOSE_FILES exec api go test -v ./...
    ;;
  lint)
    echo "🔍 Running linter..."
    golangci-lint run ./...
    ;;
  fmt)
    echo "📝 Formatting code..."
    go fmt ./...
    ;;
  build-local)
    echo "🔨 Building binaries locally..."
    go build -o bin/api ./cmd/api
    go build -o bin/worker ./cmd/worker
    echo "🔨 Building admin panel..."
    cd admin && npm run build && cd ..
    echo "✅ Binaries built in ./bin/, admin panel built in ./admin/.next/standalone/"
    ;;
  run-api)
    echo "🚀 Running API server locally..."
    go run ./cmd/api
    ;;
  run-worker)
    echo "⚙️  Running worker locally..."
    go run ./cmd/worker
    ;;
  db)
    echo "🐘 Connecting to PostgreSQL..."
    docker-compose $COMPOSE_FILES exec db psql -U ${POSTGRES_USER:-honeydue} -d ${POSTGRES_DB:-honeydue}
    ;;
  redis)
    echo "📮 Connecting to Redis..."
    docker-compose $COMPOSE_FILES exec redis redis-cli
    ;;
  seed)
    echo "🌱 Seeding lookup data..."
    docker-compose $COMPOSE_FILES exec -T db psql -U ${POSTGRES_USER:-postgres} -d ${POSTGRES_DB:-honeydue} -f - < seeds/001_lookups.sql
    echo "✅ Lookup data seeded"
    ;;
  seed-test)
    echo "🧪 Seeding test data..."
    docker-compose $COMPOSE_FILES exec -T db psql -U ${POSTGRES_USER:-postgres} -d ${POSTGRES_DB:-honeydue} -f - < seeds/002_test_data.sql
    echo "✅ Test data seeded"
    ;;
  seed-all)
    echo "🌱 Seeding all data..."
    docker-compose $COMPOSE_FILES exec -T db psql -U ${POSTGRES_USER:-postgres} -d ${POSTGRES_DB:-honeydue} -f - < seeds/001_lookups.sql
    docker-compose $COMPOSE_FILES exec -T db psql -U ${POSTGRES_USER:-postgres} -d ${POSTGRES_DB:-honeydue} -f - < seeds/002_test_data.sql
    echo "✅ All data seeded"
    ;;
  seed-admin)
    echo "🔐 Seeding admin user..."
    docker-compose $COMPOSE_FILES exec -T db psql -U ${POSTGRES_USER:-postgres} -d ${POSTGRES_DB:-honeydue} -f - < seeds/003_admin_user.sql
    echo "✅ Admin user seeded"
    ;;
  admin-install)
    echo "📦 Installing admin panel dependencies..."
    cd admin && npm install
    echo "✅ Admin panel dependencies installed"
    ;;
  admin-dev)
    echo "🚀 Starting admin panel in development mode..."
    cd admin && npm run dev
    ;;
  admin-build)
    echo "🔨 Building admin panel..."
    cd admin && npm run build
    echo "✅ Admin panel built (standalone mode)"
    ;;
  admin-logs)
    echo "📋 Viewing admin panel logs..."
    docker-compose $COMPOSE_FILES logs -f admin
    ;;
  migrate)
    echo "📊 Running database migrations..."
    # GORM auto-migrates on API startup, but we need GoAdmin tables
    docker-compose $COMPOSE_FILES exec -T db psql -U ${POSTGRES_USER:-postgres} -d ${POSTGRES_DB:-honeydue} -f - < migrations/002_goadmin_tables.up.sql
    echo "✅ Migrations complete"
    ;;
  clean)
    echo "🧹 Cleaning up..."
    docker-compose $COMPOSE_FILES down -v
    rm -rf bin/
    echo "✅ Cleaned containers and binaries"
    ;;
  status)
    echo "📊 Docker container status:"
    docker-compose $COMPOSE_FILES ps
    ;;
  *)
    # If no argument provided, default to 'up'
    if [ -z "$1" ]; then
      echo "🚀 Starting development environment..."
      docker-compose $COMPOSE_FILES up
    else
      echo "Development helper script for HoneyDue API (Go)"
      echo ""
      echo "Usage: ./dev.sh [command]"
      echo ""
      echo "Docker Commands:"
      echo "  (no args)       Start development environment (default)"
      echo "  up              Start development environment"
      echo "  down            Stop development environment"
      echo "  logs [service]  View logs (optionally for specific service)"
      echo "  restart         Restart all services"
      echo "  build           Rebuild and start containers"
      echo "  bash            Open shell in API container"
      echo "  status          Show container status"
      echo "  clean           Stop containers and remove volumes"
      echo ""
      echo "Local Development:"
      echo "  build-local     Build Go binaries + admin panel locally"
      echo "  run-api         Run API server locally (port 8000)"
      echo "  run-worker      Run worker locally"
      echo "  test            Run tests locally"
      echo "  test-docker     Run tests in Docker"
      echo "  lint            Run linter"
      echo "  fmt             Format code"
      echo ""
      echo "Database:"
      echo "  db              Connect to PostgreSQL"
      echo "  redis           Connect to Redis"
      echo "  migrate         Run database migrations"
      echo "  seed            Seed lookup data (categories, priorities, etc.)"
      echo "  seed-test       Seed test data (users, residences, tasks)"
      echo "  seed-all        Seed all data (lookups + test data)"
      echo "  seed-admin      Seed admin user"
      echo ""
      echo "Admin Panel:"
      echo "  admin-install   Install admin panel npm dependencies"
      echo "  admin-dev       Start admin panel dev server (port 3000)"
      echo "  admin-build     Build admin panel for production"
      echo "  admin-logs      View admin panel container logs"
      echo ""
      echo "Services:"
      echo "  api             - API server (port 8000)"
      echo "  admin           - Admin panel (port 3000)"
      echo "  worker          - Background job worker"
      echo "  db              - PostgreSQL database"
      echo "  redis           - Redis cache"
      echo ""
      echo "Examples:"
      echo "  ./dev.sh                 # Start dev environment"
      echo "  ./dev.sh up              # Same as above"
      echo "  ./dev.sh logs api        # View API server logs"
      echo "  ./dev.sh logs worker     # View worker logs"
      echo "  ./dev.sh build           # Rebuild and start"
      echo "  ./dev.sh run-api         # Run API locally (without Docker)"
      echo "  ./dev.sh test            # Run tests"
      echo "  ./dev.sh db              # Connect to PostgreSQL"
    fi
    ;;
esac
