#!/usr/bin/env bash

# Detect platform
PLATFORM="$(uname)"

# Function to detect Docker runtime on macOS
detect_docker_runtime_macos() {
    if command -v colima &>/dev/null; then
        echo "colima"
    elif [ -d "/Applications/Docker.app" ] || command -v docker &>/dev/null; then
        echo "docker-desktop"
    else
        echo "none"
    fi
}
detect_linux_package_manager() {
    if command -v apt-get &>/dev/null; then
        echo "apt"
    elif command -v yum &>/dev/null; then
        echo "yum"
    elif command -v dnf &>/dev/null; then
        echo "dnf"
    elif command -v pacman &>/dev/null; then
        echo "pacman"
    else
        echo "unknown"
    fi
}

# Function to install Docker on Linux
install_docker_linux() {
    local pkg_manager
    pkg_manager=$(detect_linux_package_manager)
    
    echo "Docker is not installed."
    read -rp "Do you want to install Docker using $pkg_manager? (y/n): " choice
    if [ "$choice" == "y" ]; then
        case $pkg_manager in
            "apt")
                sudo apt-get update
                sudo apt-get install -y docker.io docker-compose
                sudo systemctl enable --now docker
                sudo usermod -aG docker "$USER"
                echo "Docker has been installed. Please log out and back in, then restart this script."
                ;;
            "yum"|"dnf")
                sudo "$pkg_manager" install -y docker docker-compose
                sudo systemctl enable --now docker
                sudo usermod -aG docker "$USER"
                echo "Docker has been installed. Please log out and back in, then restart this script."
                ;;
            "pacman")
                sudo pacman -S docker docker-compose
                sudo systemctl enable --now docker
                sudo usermod -aG docker "$USER"
                echo "Docker has been installed. Please log out and back in, then restart this script."
                ;;
            *)
                echo "Unsupported package manager. Please install Docker manually."
                exit 1
                ;;
        esac
        exit 0
    else
        echo "Docker is required to run this script. Exiting."
        exit 1
    fi
}

# Function to start Docker if not running
start_docker() {
    if [ "$PLATFORM" == "Darwin" ]; then
        local runtime
        runtime=$(detect_docker_runtime_macos)
        
        if [ "$runtime" == "colima" ]; then
            if ! colima status 2>/dev/null | grep -q "Running"; then
                echo "Docker is not running. Starting Colima..."
                colima start
                sleep 3
            fi
        elif [ "$runtime" == "docker-desktop" ]; then
            echo "Docker is not running. Starting Docker Desktop..."
            open --background -a Docker
            sleep 3
        else
            echo "No Docker runtime found (neither Colima nor Docker Desktop)."
            install_docker
        fi
    elif [ "$PLATFORM" == "Linux" ]; then
        echo "Docker is not running. Starting Docker..."
        sudo systemctl start docker
        sleep 3
    else
        echo "Unsupported platform: $PLATFORM"
        exit 1
    fi
}

# Check if Docker is installed
install_docker() {
    if [ "$PLATFORM" == "Darwin" ]; then
        echo "Docker is not installed."
        echo "Options:"
        echo "  1. Install Colima (lightweight, recommended)"
        echo "  2. Install Docker Desktop (official)"
        read -rp "Choose option (1/2): " choice
        
        case "$choice" in
            1)
                if command -v brew &>/dev/null; then
                    brew install colima
                    echo "Colima installed. Starting..."
                    colima start
                else
                    echo "Homebrew not found. Please install Homebrew first."
                    exit 1
                fi
                ;;
            2)
                read -rp "Do you want to install Docker Desktop using Homebrew? (y/n): " confirm
                if [ "$confirm" == "y" ]; then
                    brew install --cask docker
                    echo "Docker Desktop has been installed. Please restart this script."
                else
                    echo "Docker is required to run this script. Exiting."
                    exit 1
                fi
                ;;
            *)
                echo "Invalid choice. Exiting."
                exit 1
                ;;
        esac
        exit 0
    elif [ "$PLATFORM" == "Linux" ]; then
        install_docker_linux
    else
        echo "Unsupported platform: $PLATFORM"
        exit 1
    fi
}

# Check if docker-compose is installed
install_docker_compose() {
    if [ "$PLATFORM" == "Darwin" ]; then
        echo "docker-compose is not installed."
        read -rp "Do you want to install docker-compose using Homebrew? (y/n): " choice
        if [ "$choice" == "y" ]; then
            brew install docker-compose
            echo "Docker Compose has been installed. Please restart this script."
            exit 0
        else
            echo "Docker Compose is required to run this script. Exiting."
            exit 1
        fi
    elif [ "$PLATFORM" == "Linux" ]; then
        local pkg_manager
        pkg_manager=$(detect_linux_package_manager)
        
        echo "docker-compose is not installed."
        read -rp "Do you want to install docker-compose using $pkg_manager? (y/n): " choice
        if [ "$choice" == "y" ]; then
            case $pkg_manager in
                "apt")
                    sudo apt-get update
                    sudo apt-get install -y docker-compose
                    ;;
                "yum"|"dnf")
                    sudo "$pkg_manager" install -y docker-compose
                    ;;
                "pacman")
                    sudo pacman -S docker-compose
                    ;;
                *)
                    echo "Unsupported package manager. Please install docker-compose manually."
                    exit 1
                    ;;
            esac
            echo "Docker Compose has been installed. Please restart this script."
            exit 0
        else
            echo "Docker Compose is required to run this script. Exiting."
            exit 1
        fi
    else
        echo "Unsupported platform: $PLATFORM"
        exit 1
    fi
}

if ! command -v docker &>/dev/null; then
    install_docker
fi

# Check if Docker is running
if [ "$(docker info >/dev/null 2>&1 && echo "running")" != "running" ]; then
    start_docker
fi

# Check if docker-compose is called
if [ "$1" == "docker-compose" ]; then
    # If docker-compose is called, check if Docker is running again
    if [ "$(docker info >/dev/null 2>&1 && echo "running")" != "running" ]; then
        if ! command -v docker-compose &>/dev/null; then
            start_docker
        fi
    fi
fi

# Run the provided command
"$@"
