#!/bin/bash
# Switch between dev and prod environments
# Usage: ./scripts/env-switch.sh [dev|prod]

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"

ENV=${1:-dev}

if [[ "$ENV" != "dev" && "$ENV" != "prod" ]]; then
    echo "Usage: $0 [dev|prod]"
    echo "  dev  - localhost URLs for local development"
    echo "  prod - gameplay-demo.manticorum.com URLs"
    exit 1
fi

echo "Switching to $ENV environment..."

# Backend
if [[ -f "$PROJECT_ROOT/backend/.env.$ENV" ]]; then
    cp "$PROJECT_ROOT/backend/.env.$ENV" "$PROJECT_ROOT/backend/.env"
    echo "  ✓ backend/.env -> $ENV"
else
    echo "  ✗ backend/.env.$ENV not found"
    exit 1
fi

# Frontend SBA
if [[ -f "$PROJECT_ROOT/frontend-sba/.env.$ENV" ]]; then
    cp "$PROJECT_ROOT/frontend-sba/.env.$ENV" "$PROJECT_ROOT/frontend-sba/.env"
    echo "  ✓ frontend-sba/.env -> $ENV"
else
    echo "  ✗ frontend-sba/.env.$ENV not found"
    exit 1
fi

# Frontend PD (if exists)
if [[ -f "$PROJECT_ROOT/frontend-pd/.env.$ENV" ]]; then
    cp "$PROJECT_ROOT/frontend-pd/.env.$ENV" "$PROJECT_ROOT/frontend-pd/.env"
    echo "  ✓ frontend-pd/.env -> $ENV"
fi

echo ""
echo "Environment switched to: $ENV"

if [[ "$ENV" == "dev" ]]; then
    echo ""
    echo "Local URLs:"
    echo "  Backend:      http://localhost:8000"
    echo "  Frontend SBA: http://localhost:3000"
    echo "  Frontend PD:  http://localhost:3001"
    echo ""
    echo "Start manually:"
    echo "  cd backend && uv run python -m app.main"
    echo "  cd frontend-sba && npm run dev"
    echo ""
    echo "Or with Docker:"
    echo "  docker compose up"
else
    echo ""
    echo "Production URLs:"
    echo "  https://gameplay-demo.manticorum.com"
    echo ""
    echo "Start manually:"
    echo "  cd backend && uv run python -m app.main"
    echo "  cd frontend-sba && npm run dev"
    echo ""
    echo "Or with Docker:"
    echo "  docker compose up -d"
fi
