#!/bin/bash
# TimeSafari Docker Run Script
# Author: Matthew Raymer
# Description: Convenient script to run TimeSafari in different Docker configurations
#
# Usage:
#   ./docker/run.sh dev          # Run development mode
#   ./docker/run.sh staging      # Run staging mode
#   ./docker/run.sh production   # Run production mode
#   ./docker/run.sh custom       # Run custom mode with environment variables
#
# Environment Variables:
#   BUILD_MODE: development, staging, or production
#   NODE_ENV: node environment
#   VITE_PLATFORM: vite platform
#   PWA: automatically enabled for web platforms
#   PORT: port to expose
#   ENV_FILE: environment file to use

set -e

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

# Logging functions
log_info() {
    echo -e "${BLUE}[$(date '+%Y-%m-%d %H:%M:%S')] [INFO]${NC} $1"
}

log_success() {
    echo -e "${GREEN}[$(date '+%Y-%m-%d %H:%M:%S')] [SUCCESS]${NC} $1"
}

log_warn() {
    echo -e "${YELLOW}[$(date '+%Y-%m-%d %H:%M:%S')] [WARN]${NC} $1"
}

log_error() {
    echo -e "${RED}[$(date '+%Y-%m-%d %H:%M:%S')] [ERROR]${NC} $1"
}

# Function to show usage
show_usage() {
    echo "TimeSafari Docker Run Script"
    echo ""
    echo "Usage: $0 <mode> [options]"
    echo ""
    echo "Modes:"
    echo "  dev         - Development mode with hot reloading"
    echo "  staging     - Staging mode for testing"
    echo "  production  - Production mode"
    echo "  custom      - Custom mode with environment variables"
    echo ""
    echo "Options:"
    echo "  --port <port>     - Custom port (default: 5173 for dev, 8080 for staging, 80 for production)"
    echo "  --env <file>      - Environment file (default: .env.<mode>)"
    echo "  --build-args      - Show build arguments for the mode"
    echo "  --help            - Show this help message"
    echo ""
    echo "Examples:"
    echo "  $0 dev"
    echo "  $0 staging --port 9000"
    echo "  $0 production --env .env.prod"
    echo "  BUILD_MODE=staging $0 custom"
    echo ""
    echo "Environment Variables:"
    echo "  BUILD_MODE: development, staging, or production"
    echo "  NODE_ENV: node environment"
    echo "  VITE_PLATFORM: vite platform"
    echo "  PWA: automatically enabled for web platforms"
    echo "  PORT: port to expose"
    echo "  ENV_FILE: environment file to use"
}

# Function to show build arguments for a mode
show_build_args() {
    local mode=$1
    echo "Build arguments for $mode mode:"
    echo ""
    case $mode in
        dev)
            echo "  BUILD_MODE: development"
            echo "  NODE_ENV: development"
            echo "  VITE_PLATFORM: web"
            echo "  PWA: enabled (web platform)"
            echo "  Target: development"
            echo "  Port: 5173"
            ;;
        staging)
            echo "  BUILD_MODE: staging"
            echo "  NODE_ENV: staging"
            echo "  VITE_PLATFORM: web"
            echo "  PWA: enabled (web platform)"
            echo "  Target: staging"
            echo "  Port: 80 (mapped to 8080)"
            ;;
        production)
            echo "  BUILD_MODE: production"
            echo "  NODE_ENV: production"
            echo "  VITE_PLATFORM: web"
            echo "  PWA: enabled (web platform)"
            echo "  Target: production"
            echo "  Port: 80"
            ;;
        custom)
            echo "  BUILD_MODE: \${BUILD_MODE:-production}"
            echo "  NODE_ENV: \${NODE_ENV:-production}"
            echo "  VITE_PLATFORM: \${VITE_PLATFORM:-web}"
            echo "  PWA: enabled for web platforms"
            echo "  Target: \${BUILD_TARGET:-production}"
            echo "  Port: \${CUSTOM_PORT:-8080}:\${CUSTOM_INTERNAL_PORT:-80}"
            ;;
        *)
            log_error "Unknown mode: $mode"
            exit 1
            ;;
    esac
}

# Function to check if Docker is running
check_docker() {
    if ! docker info > /dev/null 2>&1; then
        log_error "Docker is not running. Please start Docker and try again."
        exit 1
    fi
}

# Function to check if docker-compose is available
check_docker_compose() {
    if ! command -v docker-compose > /dev/null 2>&1; then
        log_error "docker-compose is not installed. Please install docker-compose and try again."
        exit 1
    fi
}

# Function to check if required files exist
check_files() {
    local mode=$1
    local env_file=$2
    
    if [ ! -f "Dockerfile" ]; then
        log_error "Dockerfile not found. Please run this script from the project root."
        exit 1
    fi
    
    if [ ! -f "docker-compose.yml" ]; then
        log_error "docker-compose.yml not found. Please run this script from the project root."
        exit 1
    fi
    
    if [ -n "$env_file" ] && [ ! -f "$env_file" ]; then
        log_warn "Environment file $env_file not found. Using defaults."
    fi
}

# Function to run the container
run_container() {
    local mode=$1
    local port=$2
    local env_file=$3
    
    log_info "Starting TimeSafari in $mode mode..."
    
    # Set environment variables based on mode
    case $mode in
        dev)
            export DEV_PORT=${port:-5173}
            if [ -n "$env_file" ]; then
                export DEV_ENV_FILE="$env_file"
            fi
            docker-compose up dev
            ;;
        staging)
            export STAGING_PORT=${port:-8080}
            if [ -n "$env_file" ]; then
                export STAGING_ENV_FILE="$env_file"
            fi
            docker-compose up staging
            ;;
        production)
            export PROD_PORT=${port:-80}
            if [ -n "$env_file" ]; then
                export PROD_ENV_FILE="$env_file"
            fi
            docker-compose up production
            ;;
        custom)
            export CUSTOM_PORT=${port:-8080}
            if [ -n "$env_file" ]; then
                export CUSTOM_ENV_FILE="$env_file"
            fi
            docker-compose up custom
            ;;
        *)
            log_error "Unknown mode: $mode"
            exit 1
            ;;
    esac
}

# Main script
main() {
    local mode=""
    local port=""
    local env_file=""
    local show_args=false
    
    # Parse command line arguments
    while [[ $# -gt 0 ]]; do
        case $1 in
            dev|staging|production|custom)
                mode="$1"
                shift
                ;;
            --port)
                port="$2"
                shift 2
                ;;
            --env)
                env_file="$2"
                shift 2
                ;;
            --build-args)
                show_args=true
                shift
                ;;
            --help|-h)
                show_usage
                exit 0
                ;;
            *)
                log_error "Unknown option: $1"
                show_usage
                exit 1
                ;;
        esac
    done
    
    # Check if mode is provided
    if [ -z "$mode" ]; then
        log_error "No mode specified."
        show_usage
        exit 1
    fi
    
    # Show build arguments if requested
    if [ "$show_args" = true ]; then
        show_build_args "$mode"
        exit 0
    fi
    
    # Check prerequisites
    check_docker
    check_docker_compose
    check_files "$mode" "$env_file"
    
    # Run the container
    run_container "$mode" "$port" "$env_file"
}

# Run main function with all arguments
main "$@" 