#!/bin/bash

# KidsAI Explorer - Setup Verification Script
# This script verifies that Docker Bake and the migration setup is working correctly

set -e

echo "🔍 KidsAI Explorer - Setup Verification"
echo "======================================="

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

# Function to print colored status
print_status() {
    if [ "$2" = "OK" ]; then
        echo -e "${GREEN}✅ $1${NC}"
    elif [ "$2" = "WARN" ]; then
        echo -e "${YELLOW}⚠️  $1${NC}"
    elif [ "$2" = "ERROR" ]; then
        echo -e "${RED}❌ $1${NC}"
    else
        echo -e "${BLUE}ℹ️  $1${NC}"
    fi
}

# Check Docker
if command -v docker &> /dev/null; then
    if docker info > /dev/null 2>&1; then
        print_status "Docker is installed and running" "OK"
    else
        print_status "Docker is installed but not running" "ERROR"
        exit 1
    fi
else
    print_status "Docker is not installed" "ERROR"
    exit 1
fi

# Check Docker Compose
if command -v docker-compose &> /dev/null; then
    COMPOSE_VERSION=$(docker-compose version --short 2>/dev/null || echo "unknown")
    print_status "Docker Compose is installed (version: $COMPOSE_VERSION)" "OK"
else
    print_status "Docker Compose is not installed" "ERROR"
    exit 1
fi

# Check Docker Buildx (for Bake support)
if docker buildx version &> /dev/null; then
    print_status "Docker Buildx is available (required for Bake)" "OK"
else
    print_status "Docker Buildx is not available" "WARN"
fi

# Check if docker-bake.hcl exists
if [ -f "docker-bake.hcl" ]; then
    print_status "Docker Bake configuration file exists" "OK"
else
    print_status "Docker Bake configuration file missing" "ERROR"
fi

# Check if .env file exists
if [ -f ".env" ]; then
    print_status ".env file exists" "OK"
    
    # Check for COMPOSE_BAKE setting
    if grep -q "COMPOSE_BAKE=true" .env; then
        print_status "COMPOSE_BAKE is enabled in .env" "OK"
    else
        print_status "COMPOSE_BAKE not enabled in .env" "WARN"
        echo -e "${YELLOW}  Add 'COMPOSE_BAKE=true' to your .env file for better build performance${NC}"
    fi
    
    # Check for API keys
    if grep -q "OPENAI_API_KEY=your_openai_api_key_here" .env; then
        print_status "OpenAI API key needs to be configured" "WARN"
    else
        print_status "OpenAI API key appears to be configured" "OK"
    fi
else
    print_status ".env file missing - copying from template" "WARN"
    if [ -f ".env.example" ]; then
        cp .env.example .env
        print_status "Created .env from .env.example" "OK"
    else
        print_status ".env.example also missing" "ERROR"
    fi
fi

# Check essential files
essential_files=(
    "package.json"
    "next.config.js"
    "tailwind.config.js"
    "tsconfig.json"
    "Dockerfile"
    "docker-compose.yml"
)

for file in "${essential_files[@]}"; do
    if [ -f "$file" ]; then
        print_status "$file exists" "OK"
    else
        print_status "$file is missing" "ERROR"
    fi
done

# Check directory structure
essential_dirs=(
    "src"
    "src/app"
    "src/components"
    "src/lib"
    "src/types"
    "public"
)

for dir in "${essential_dirs[@]}"; do
    if [ -d "$dir" ]; then
        print_status "Directory $dir exists" "OK"
    else
        print_status "Directory $dir is missing" "ERROR"
    fi
done

echo ""
echo "🧪 Testing Docker Bake functionality..."

# Test Docker Bake
if [ -f "docker-bake.hcl" ]; then
    echo "📋 Available Docker Bake targets:"
    docker buildx bake --print 2>/dev/null | grep -E '"target"|"context"' || echo "Could not parse bake targets"
    
    # Test bake file syntax
    if docker buildx bake --print > /dev/null 2>&1; then
        print_status "Docker Bake configuration is valid" "OK"
    else
        print_status "Docker Bake configuration has errors" "ERROR"
    fi
fi

echo ""
echo "📊 Summary"
echo "=========="
echo "Your KidsAI Explorer project is set up for:"
echo "• Next.js 15 with TypeScript"
echo "• Tailwind CSS for styling"
echo "• Docker containerization"
echo "• Docker Bake for improved build performance"
echo ""

if [ -f ".env" ] && ! grep -q "your_openai_api_key_here" .env; then
    echo -e "${GREEN}🚀 Ready to start! Run: ./docker-dev.sh dev${NC}"
else
    echo -e "${YELLOW}⚠️  Before starting:${NC}"
    echo "1. Edit .env file and add your OpenAI API key"
    echo "2. Run: ./docker-dev.sh dev"
fi

echo ""
echo "For help with commands, run: ./docker-dev.sh help"
