#!/bin/bash

REPO="SharkMI-0x7E/mc-minder"
BINARY_NAME="mc-minder"

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

log_info() {
    echo -e "${GREEN}[INFO]${NC} $1"
}

log_warn() {
    echo -e "${YELLOW}[WARN]${NC} $1"
}

log_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

detect_arch() {
    local arch=$(uname -m)
    local os=$(uname -s)

    case "$os" in
        Linux)
            if [ -n "$TERMUX_VERSION" ]; then
                # Termux/Android - use Bionic-linked binary
                echo "termux-aarch64"
            elif [ "$arch" = "aarch64" ] || [ "$arch" = "arm64" ]; then
                # Native ARM64 Linux (e.g. Raspberry Pi)
                echo "aarch64-linux"
            elif [ "$arch" = "x86_64" ]; then
                echo "x86_64-linux"
            else
                echo "unknown"
            fi
            ;;
        Darwin)
            log_error "macOS is NOT supported for pre-built binaries"
            echo ""
            log_info "Reason: The pre-compiled binaries are built for Linux/Android only"
            log_info "macOS requires different system libraries and cannot run them"
            echo ""
            log_info "To install on macOS, please compile from source:"
            log_info "  1. Install Rust: https://rustup.rs"
            log_info "  2. Clone repository: git clone https://github.com/$REPO.git"
            log_info "  3. Build: cd mc-minder && cargo build --release"
            echo ""
            exit 1
            ;;
        *)
            echo "unknown"
            ;;
    esac
}

check_dependencies() {
    local missing=()

    command -v curl >/dev/null 2>&1 || missing+=("curl")

    if [ ${#missing[@]} -ne 0 ]; then
        log_error "Missing dependencies: ${missing[*]}"

        if [ -n "$TERMUX_VERSION" ]; then
            log_info "Installing dependencies..."
            pkg install -y ${missing[*]}
        else
            log_info "Please install: ${missing[*]}"
            exit 1
        fi
    fi
}

get_latest_version() {
    local version=""
    local retry_count=0
    local max_retries=3

    while [ $retry_count -lt $max_retries ]; do
        version=$(curl -s \
            --connect-timeout 10 \
            --max-time 30 \
            --retry 2 \
            --retry-delay 3 \
            "https://api.github.com/repos/$REPO/releases/latest" | \
            grep '"tag_name"' | sed -E 's/.*"v([^"]+)".*/\1/')

        if [ -n "$version" ] && [ "$version" != "" ] && [ "$version" != "null" ]; then
            echo "$version"
            return 0
        fi

        retry_count=$((retry_count + 1))

        if [ $retry_count -lt $max_retries ]; then
            log_warn "Failed to get version (attempt $retry_count/$max_retries), retrying..."
            sleep 2
        fi
    done

    echo ""
    return 1
}

download_with_retry() {
    local url="$1"
    local output="$2"
    local description="$3"
    local retry_count=0
    local max_retries=3

    while [ $retry_count -lt $max_retries ]; do
        log_info "Downloading $description (attempt $((retry_count + 1))/$max_retries)..."

        if curl -L \
            --connect-timeout 15 \
            --max-time 120 \
            --retry 2 \
            --retry-delay 3 \
            --progress-bar \
            -o "$output" \
            "$url"; then

            if [ -f "$output" ] && [ -s "$output" ]; then
                return 0
            else
                log_warn "Downloaded file is empty or missing"
                rm -f "$output"
            fi
        else
            log_warn "Download failed"
            rm -f "$output"
        fi

        retry_count=$((retry_count + 1))

        if [ $retry_count -lt $max_retries ]; then
            sleep 3
        fi
    done

    return 1
}

download_binary() {
    local target="$1"
    local version="$2"

    local binary_name="$BINARY_NAME-$target"
    local url="https://github.com/$REPO/releases/download/v$version/$binary_name"

    log_info "Downloading MC-Minder v$version for $target..."

    if ! download_with_retry "$url" "$BINARY_NAME" "binary ($target)"; then
        return 1
    fi

    chmod +x "$BINARY_NAME"

    return 0
}

download_scripts() {
    local version="$1"
    local base_url="https://raw.githubusercontent.com/$REPO/v$version/scripts"

    if [ ! -f "backup.sh" ]; then
        log_info "Downloading backup.sh..."
        if download_with_retry "$base_url/backup.sh" "backup.sh" "backup script"; then
            chmod +x "backup.sh"
        else
            log_warn "Failed to download backup.sh (optional, skip)"
        fi
    else
        log_info "backup.sh already exists, skipping"
    fi

    if [ ! -f "start-tui.sh" ]; then
        log_info "Downloading start-tui.sh (TUI launcher)..."
        if download_with_retry "$base_url/start-tui.sh" "start-tui.sh" "TUI startup script"; then
            chmod +x "start-tui.sh"
            log_info ""
            log_info "TUI script downloaded! You can use it with:"
            log_info "  ./start-tui.sh"
            log_info ""
        else
            log_warn "Failed to download start-tui.sh (optional, run './mc-minder tui' directly)"
        fi
    else
        log_info "start-tui.sh already exists, skipping"
    fi
}

show_post_install_instructions() {
    echo ""
    log_info "Installation complete!"
    echo ""
    echo -e "${BLUE}Next steps:${NC}"
    echo "  1. Run: ./$BINARY_NAME init          # Initialize configuration"
    echo "  2. Place fabric-server.jar here         # Minecraft server jar"
    echo "  3. Run: ./$BINARY_NAME tui             # Start with native TUI"
    echo ""
    echo -e "${BLUE}Useful commands:${NC}"
    echo "  ./$BINARY_NAME --version               # Show version"
    echo "  ./$BINARY_NAME self-update             # Update to latest version"
    echo "  ./$BINARY_NAME init                    # Interactive configuration"
    echo ""
    echo -e "${BLUE}For more information:${NC} https://github.com/$REPO"
}

main() {
    echo ""
    echo -e "${BLUE}MC-Minder Installer${NC}"
    echo "========================="
    echo ""

    check_dependencies

    local target=$(detect_arch)

    if [ "$target" = "unknown" ]; then
        log_error "Unsupported platform: $(uname -s) $(uname -m)"
        log_info "Supported platforms:"
        log_info "  - Linux x86_64"
        log_info "  - Termux/Android ARM64"
        echo ""
        log_info "Please compile from source: https://github.com/$REPO"
        exit 1
    fi

    log_info "Detected platform: $target"

    local version=""

    if [ $# -gt 0 ] && [ -n "$1" ]; then
        version="$1"
        log_info "Using specified version: $version"
    else
        log_info "Checking latest version from GitHub..."
        version=$(get_latest_version)

        if [ -z "$version" ]; then
            log_error "Failed to get latest version from GitHub API"
            echo ""
            log_info "Possible reasons:"
            log_info "  1. Network connection issue"
            log_info "  2. GitHub API rate limited (60 req/hour for unauthenticated)"
            log_info "  3. No releases available yet"
            echo ""
            log_info "You can specify version manually:"
            log_info "  $0 <version>"
            log_info "Example: $0 0.5.1"
            exit 1
        fi

        log_info "Latest version: $version"
    fi

    if [ -f "$BINARY_NAME" ]; then
        local existing_size=$(du -h "$BINARY_NAME" 2>/dev/null | cut -f1)
        log_warn "MC-Minder already exists (size: $existing_size)"
        echo ""
        echo "Options:"
        echo "  y) Reinstall (overwrite existing binary)"
        echo "  N) Keep existing binary and continue"
        echo "  C) Cancel installation"
        echo ""
        read -p "Your choice [y/N/C]: " choice
        case "$choice" in
            y|Y )
                log_info "Removing old binary..."
                rm -f "$BINARY_NAME"
                ;;
            c|C )
                log_info "Installation cancelled"
                exit 0
                ;;
            * )
                log_info "Keeping existing binary"
                ;;
        esac
    fi

    if [ ! -f "$BINARY_NAME" ]; then
        echo ""
        if ! download_binary "$target" "$version"; then
            log_error "Failed to download MC-Minder v$version"
            echo ""
            log_info "Troubleshooting:"
            log_info "  1. Check your internet connection"
            log_info "  2. Verify the version exists: https://github.com/$REPO/releases/tag/v$version"
            log_info "  3. Try compiling from source: https://github.com/$REPO"
            exit 1
        fi

        local size=$(du -h "$BINARY_NAME" | cut -f1)
        log_info "Downloaded successfully ($size)"
    fi

    echo ""
    download_scripts "$version"

    show_post_install_instructions
}

main "$@"