#!/bin/bash

# Hebrew Soldier App Docker Deployment Script with Hostname

echo "🚀 Starting Hebrew Soldier App deployment with hostname..."

# Get the current hostname
CURRENT_HOSTNAME=$(hostname)
echo "📍 Current hostname: $CURRENT_HOSTNAME"

# 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

# Create config.json if it doesn't exist
if [ ! -f "config.json" ]; then
    echo "📝 Creating config.json from template..."
    cp config.json.template config.json
    echo "⚠️  Please edit config.json with your database settings before continuing."
    echo "   Press Enter when ready..."
    read
fi

# Export hostname for docker-compose
export HOSTNAME=$CURRENT_HOSTNAME

# Build and start the application
echo "🔨 Building and starting containers with hostname: $CURRENT_HOSTNAME..."
docker-compose up --build -d

# Wait for services to be ready
echo "⏳ Waiting for services to start..."
sleep 10

# Check if services are running
if docker-compose ps | grep -q "Up"; then
    echo "✅ Application is running!"
    echo "🌐 Access the app at: http://localhost:3000"
    echo "📊 Database at: localhost:3306"
    echo "🖥️  Running on hostname: $CURRENT_HOSTNAME"
    echo ""
    echo "📋 Useful commands:"
    echo "   View logs: docker-compose logs -f"
    echo "   Stop app: docker-compose down"
    echo "   Restart: docker-compose restart"
else
    echo "❌ Something went wrong. Check logs with: docker-compose logs"
fi
