#!/bin/bash

# KidsAI Explorer - Docker Startup Script
# This script helps you get started with the containerized Next.js application

set -e

echo "🚀 KidsAI Explorer - Docker Setup"
echo "=================================="

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

# Check if .env file exists
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 continuing."
    echo "   You can get OpenAI API key from: https://platform.openai.com/api-keys"
    echo ""
    echo "Would you like to continue with the default setup? (y/n)"
    read -r response
    if [[ ! "$response" =~ ^[Yy]$ ]]; then
        echo "Please edit .env and run this script again."
        exit 1
    fi
fi

echo ""
echo "🐳 Building and starting KidsAI Explorer..."
echo ""

# Build and start the containers
docker compose down --remove-orphans 2>/dev/null || true
docker compose up --build -d

echo ""
echo "📦 Waiting for the application to start..."
sleep 10

# Check if the container is running
if docker compose ps | grep -q "Up"; then
    echo ""
    echo "✅ KidsAI Explorer is now running!"
    echo ""
    echo "🌐 Access the application at: http://localhost:3000"
    echo ""
    echo "📋 Useful commands:"
    echo "   View logs:     docker compose logs -f"
    echo "   Stop app:      docker compose down"
    echo "   Restart:       docker compose restart"
    echo "   Rebuild:       docker compose up --build"
    echo ""
    echo "🔧 For development:"
    echo "   Enter container: docker compose exec app sh"
    echo "   View processes:  docker compose ps"
    echo ""
else
    echo "❌ Error: Application failed to start."
    echo "View logs with: docker compose logs"
    exit 1
fi
