#!/bin/bash

# Production Docker Compose Runner
# This script sets up and runs the complete application stack in production mode

set -e

echo "🚀 Starting Developer Portfolio Tracker in Production Mode"
echo "============================================================"

# Check if .env file exists, if not copy from .env.production
if [ ! -f .env ]; then
    echo "📋 Creating .env file from .env.production template..."
    cp .env.production .env
    echo "⚠️  IMPORTANT: Please edit .env file and update all passwords and secrets!"
    echo "⚠️  Default passwords are NOT secure for production use!"
    echo ""
    echo "💡 You can also copy from .env.example for a simpler template"
    echo ""
    read -p "Press Enter to continue after updating .env file..."
fi

# Build all services
echo "🔨 Building all services..."
docker-compose build --no-cache

# Start the complete application stack
echo "🚀 Starting all services..."
docker-compose up -d

# Wait for services to be healthy
echo "⏳ Waiting for services to be healthy..."
sleep 30

# Check service health
echo "🏥 Checking service health..."
docker-compose ps

echo ""
echo "✅ Application started successfully!"
echo ""
echo "🌐 Access points:"
echo "   Frontend:        http://localhost:80"
echo "   API Gateway:     http://localhost:3000"
echo "   Health Check:    http://localhost:3000/health"
echo ""
echo "📊 To view logs:"
echo "   docker-compose logs -f [service-name]"
echo ""
echo "🛑 To stop all services:"
echo "   docker-compose down"
echo ""
echo "🗑️  To stop and remove all data:"
echo "   docker-compose down -v"