#!/bin/bash

# Docker development script for Mawidi site
# Starts the development container with hot reload

echo "🔧 Starting Docker development environment..."

# Change to project root
cd "$(dirname "$0")/.." || exit 1

# Check if docker-compose is available
if ! command -v docker-compose &> /dev/null; then
    echo "Using docker compose (v2) instead of docker-compose..."
    COMPOSE_CMD="docker compose"
else
    COMPOSE_CMD="docker-compose"
fi

# Stop any existing containers
echo "🛑 Stopping existing containers..."
$COMPOSE_CMD --profile dev down

# Build and start development service
echo "📦 Building development Docker image..."
$COMPOSE_CMD --profile dev build mawidi-dev

if [ $? -eq 0 ]; then
    echo "✅ Development image built successfully"

    # Start development service
    echo "🚀 Starting development container..."
    $COMPOSE_CMD --profile dev up -d mawidi-dev

    if [ $? -eq 0 ]; then
        echo "✅ Development container started!"
        echo "🌐 Development server available at: http://localhost:3000"
        echo "📝 Hot reload is enabled - changes will auto-refresh"

        # Show logs
        echo ""
        echo "📋 Container logs (Ctrl+C to exit):"
        $COMPOSE_CMD --profile dev logs -f mawidi-dev
    else
        echo "❌ Failed to start development container"
        exit 1
    fi
else
    echo "❌ Docker build failed"
    exit 1
fi