#!/bin/bash

# KidsAI Explorer - Docker Development Setup Script
# This script sets up the development environment with Docker Bake + Compose v2 support

set -e

echo "🚀 KidsAI Explorer - Docker Bake + Compose v2 Setup"
echo "==================================================="

# Check if Docker is running
if ! docker info > /dev/null 2>&1; then
    echo "❌ Docker is not running. Please start Docker first."
    exit 1
fi

# Check for Docker Compose v2
if docker compose version > /dev/null 2>&1; then
    COMPOSE_CMD="docker compose"
    echo "✅ Docker Compose v2 detected"
elif command -v docker-compose &> /dev/null; then
    COMPOSE_CMD="docker-compose"
    echo "⚠️  Using Docker Compose v1 (consider upgrading to v2)"
else
    echo "❌ Docker Compose is not installed. Please install Docker Compose v2."
    exit 1
fi

# Check if Docker Buildx is available for Bake support
if docker buildx version > /dev/null 2>&1; then
    BUILDX_AVAILABLE=true
    echo "✅ Docker Buildx available - Bake support enabled"
else
    BUILDX_AVAILABLE=false
    echo "⚠️  Docker Buildx not available - falling back to standard builds"
fi

# Create .env file if it doesn't exist
if [ ! -f .env ]; then
    echo "📝 Creating .env file from template..."
    cp .env.example .env
    echo "⚠️  Please edit .env file and add your API keys before running the application."
fi

# Check if COMPOSE_BAKE is enabled
if [ -f .env ] && grep -q "COMPOSE_BAKE=true" .env && [ "$BUILDX_AVAILABLE" = true ]; then
    BAKE_ENABLED=true
    echo "🔥 Docker Bake integration ENABLED"
else
    BAKE_ENABLED=false
    echo "📦 Using standard Docker builds"
fi
# Function to build with Bake if available, otherwise use Compose
build_images() {
    local target=${1:-development}
    
    if [ "$BAKE_ENABLED" = true ]; then
        echo "🔥 Building with Docker Bake (target: $target)..."
        docker buildx bake --load $target
    else
        echo "📦 Building with Docker Compose..."
        $COMPOSE_CMD build app
    fi
}

# Function to show available commands
show_help() {
    echo ""
    echo "Available commands:"
    echo "  dev         - Start development environment"
    echo "  prod        - Start production environment"
    echo "  build       - Build images with Bake (or fallback to Compose)"
    echo "  build-dev   - Build development image"
    echo "  build-prod  - Build production image"
    echo "  bake-all    - Build all targets with Bake"
    echo "  stop        - Stop all services"
    echo "  clean       - Clean up containers and images"
    echo "  logs        - Show application logs"
    echo "  shell       - Open shell in running container"
    echo "  help        - Show this help"
}

# Parse command line arguments
case "${1:-help}" in
    "dev")
        echo "🔧 Starting development environment..."
        build_images development
        $COMPOSE_CMD up app
        ;;
    "prod")
        echo "🚀 Starting production environment..."
        build_images production
        $COMPOSE_CMD --profile production up app-prod
        ;;
    "build")
        echo "🏗️  Building default images..."
        build_images development
        ;;
    "build-dev")
        echo "🏗️  Building development image..."
        build_images development
        ;;
    "build-prod")
        echo "🏗️  Building production image..."
        build_images production
        ;;
    "bake-all")
        if [ "$BAKE_ENABLED" = true ]; then
            echo "🔥 Building all targets with Docker Bake..."
            docker buildx bake all --load
        else
            echo "❌ Docker Bake is not available. Use 'build' instead."
            exit 1
        fi
        ;;
    "stop")
        echo "🛑 Stopping all services..."
        $COMPOSE_CMD down
        ;;
    "clean")
        echo "🧹 Cleaning up containers and images..."
        $COMPOSE_CMD down --rmi all --volumes --remove-orphans
        docker system prune -f
        ;;
    "logs")
        echo "📋 Showing application logs..."
        $COMPOSE_CMD logs -f app
        ;;
    "shell")
        echo "🐚 Opening shell in running development container..."
        $COMPOSE_CMD exec app sh
        ;;
    "help"|*)
        show_help
        ;;
esac

echo ""
echo "✅ Done! Your KidsAI Explorer is ready."
if [ "$BAKE_ENABLED" = true ]; then
    echo "🔥 Docker Bake integration is ACTIVE for faster builds!"
fi
echo "🌐 Access your app at: http://localhost:3444"
