#!/bin/bash

# Ghost Chess Deployment Script for Oracle A1 Instance
# Usage: ./deploy.sh [OPTIONS]
# Options:
#   --build-only    Only build the Docker image
#   --no-nginx     Deploy without nginx reverse proxy
#   --with-ssl     Deploy with SSL (requires SSL certificates)

set -e

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# Default settings
BUILD_ONLY=false
USE_NGINX=true
WITH_SSL=false

# Parse command line arguments
while [[ $# -gt 0 ]]; do
  case $1 in
    --build-only)
      BUILD_ONLY=true
      shift
      ;;
    --no-nginx)
      USE_NGINX=false
      shift
      ;;
    --with-ssl)
      WITH_SSL=true
      shift
      ;;
    -h|--help)
      echo "Usage: $0 [OPTIONS]"
      echo "Options:"
      echo "  --build-only    Only build the Docker image"
      echo "  --no-nginx     Deploy without nginx reverse proxy"
      echo "  --with-ssl     Deploy with SSL (requires SSL certificates)"
      echo "  -h, --help     Show this help message"
      exit 0
      ;;
    *)
      echo -e "${RED}Unknown option: $1${NC}"
      exit 1
      ;;
  esac
done

echo -e "${GREEN}🚀 Starting Ghost Chess deployment...${NC}"

# Check if Docker is installed and running
if ! command -v docker &> /dev/null; then
    echo -e "${RED}❌ Docker is not installed. Please install Docker first.${NC}"
    exit 1
fi

if ! docker info &> /dev/null; then
    echo -e "${RED}❌ Docker is not running. Please start Docker first.${NC}"
    exit 1
fi

# Check if docker-compose is available
if ! command -v docker-compose &> /dev/null && ! docker compose version &> /dev/null; then
    echo -e "${RED}❌ Docker Compose is not available. Please install Docker Compose.${NC}"
    exit 1
fi

# Use docker compose or docker-compose based on availability
COMPOSE_CMD="docker compose"
if ! docker compose version &> /dev/null; then
    COMPOSE_CMD="docker-compose"
fi

echo -e "${YELLOW}📦 Building Docker images...${NC}"

# Build the application
if [ "$USE_NGINX" = true ]; then
    if [ "$WITH_SSL" = true ]; then
        echo -e "${YELLOW}🔒 Building with SSL support...${NC}"
        $COMPOSE_CMD --profile production build
    else
        echo -e "${YELLOW}🌐 Building with nginx (HTTP only)...${NC}"
        $COMPOSE_CMD --profile production build
    fi
else
    echo -e "${YELLOW}📱 Building application only...${NC}"
    $COMPOSE_CMD build ghost-chess
fi

if [ "$BUILD_ONLY" = true ]; then
    echo -e "${GREEN}✅ Build completed successfully!${NC}"
    echo -e "${YELLOW}To deploy, run: $0${NC}"
    exit 0
fi

echo -e "${YELLOW}🔧 Stopping existing containers...${NC}"
$COMPOSE_CMD down --remove-orphans

echo -e "${YELLOW}🚀 Starting deployment...${NC}"

# Deploy based on configuration
if [ "$USE_NGINX" = true ]; then
    if [ "$WITH_SSL" = true ]; then
        # Check if SSL certificates exist
        if [ ! -d "./ssl" ] || [ ! -f "./ssl/cert.pem" ] || [ ! -f "./ssl/key.pem" ]; then
            echo -e "${RED}❌ SSL certificates not found in ./ssl/ directory${NC}"
            echo -e "${YELLOW}Please provide cert.pem and key.pem in ./ssl/ directory${NC}"
            exit 1
        fi
        echo -e "${GREEN}🔒 Deploying with SSL...${NC}"
        $COMPOSE_CMD --profile production up -d
    else
        echo -e "${GREEN}🌐 Deploying with nginx (HTTP only)...${NC}"
        $COMPOSE_CMD --profile production up -d
    fi
    
    echo -e "${GREEN}✅ Deployment completed!${NC}"
    echo -e "${YELLOW}📝 Application URLs:${NC}"
    echo -e "   - HTTP: http://$(curl -s ifconfig.me):80"
    if [ "$WITH_SSL" = true ]; then
        echo -e "   - HTTPS: https://$(curl -s ifconfig.me):443"
    fi
else
    echo -e "${GREEN}📱 Deploying application only...${NC}"
    $COMPOSE_CMD up -d ghost-chess
    echo -e "${GREEN}✅ Deployment completed!${NC}"
    echo -e "${YELLOW}📝 Application URL: http://$(curl -s ifconfig.me):3000${NC}"
fi

echo ""
echo -e "${GREEN}🎉 Ghost Chess is now running!${NC}"
echo -e "${YELLOW}📊 To check status: $COMPOSE_CMD ps${NC}"
echo -e "${YELLOW}📋 To view logs: $COMPOSE_CMD logs -f${NC}"
echo -e "${YELLOW}🛑 To stop: $COMPOSE_CMD down${NC}"

# Show running containers
echo ""
echo -e "${YELLOW}📋 Container Status:${NC}"
$COMPOSE_CMD ps