#!/usr/bin/env bash
set -euo pipefail

S3_BASE="https://askrike-cli-dev.s3.eu-central-1.amazonaws.com"
OS="$(uname -s)"
ARCH="$(uname -m)"

INSTALL_DIR="${INSTALL_DIR:-$HOME/.local/bin}"

# Map to GoReleaser archive naming (spawned_VERSION_Os_arch.tar.gz)
case "$OS" in
    Linux*)  OS_NAME="Linux" ;;
    Darwin*) OS_NAME="Darwin" ;;
    MINGW*|MSYS*|CYGWIN*)
        OS_NAME="Windows"
        INSTALL_DIR="${INSTALL_DIR:-$HOME/bin}"
        ;;
    *) echo "Unsupported OS: $OS"; exit 1 ;;
esac

case "$ARCH" in
    x86_64)        ARCH_NAME="x86_64" ;;
    arm64|aarch64) ARCH_NAME="arm64" ;;
    *) echo "Unsupported architecture: $ARCH"; exit 1 ;;
esac

# Get current version
VERSION=$(curl -fsSL "${S3_BASE}/version.txt")
if [ -z "$VERSION" ]; then
    echo "Failed to fetch version from ${S3_BASE}/version.txt"
    exit 1
fi

# Construct archive name
if [ "$OS_NAME" = "Windows" ]; then
    ARCHIVE="spawned_${VERSION}_${OS_NAME}_${ARCH_NAME}.zip"
else
    ARCHIVE="spawned_${VERSION}_${OS_NAME}_${ARCH_NAME}.tar.gz"
fi

DOWNLOAD_URL="${S3_BASE}/latest/${ARCHIVE}"

# Create install directory
if ! mkdir -p "$INSTALL_DIR" 2>/dev/null; then
    echo "Failed to create directory: $INSTALL_DIR"
    echo "Try running with sudo or set INSTALL_DIR to a writable location"
    exit 1
fi

# Download, extract, install
TMP=$(mktemp -d)
trap 'rm -rf "$TMP"' EXIT

if ! curl -fsSL "$DOWNLOAD_URL" -o "$TMP/$ARCHIVE"; then
    echo "Failed to download from: $DOWNLOAD_URL"
    exit 1
fi

if [ "$OS_NAME" = "Windows" ]; then
    unzip -q "$TMP/$ARCHIVE" -d "$TMP"
    cp "$TMP/spawned.exe" "$INSTALL_DIR/spawned.exe"
else
    tar xzf "$TMP/$ARCHIVE" -C "$TMP"
    cp "$TMP/spawned" "$INSTALL_DIR/spawned"
    chmod +x "$INSTALL_DIR/spawned"
fi

echo "Installed spawned ${VERSION} to ${INSTALL_DIR}/spawned"

# Install shell completion
SHELL_NAME="$(basename "${SHELL:-}")"
COMP_INSTALLED=false

case "$SHELL_NAME" in
    bash)
        COMP_DIR="$HOME/.local/share/bash-completion/completions"
        COMP_SRC="$TMP/completions/spawned.bash"
        COMP_DST="$COMP_DIR/spawned"
        ;;
    zsh)
        COMP_DIR="${ZDOTDIR:-$HOME}/.zfunc"
        COMP_SRC="$TMP/completions/spawned.zsh"
        COMP_DST="$COMP_DIR/_spawned"
        ;;
    fish)
        COMP_DIR="$HOME/.config/fish/completions"
        COMP_SRC="$TMP/completions/spawned.fish"
        COMP_DST="$COMP_DIR/spawned.fish"
        ;;
esac

if [ -n "${COMP_SRC:-}" ] && [ -f "$COMP_SRC" ]; then
    mkdir -p "$COMP_DIR"
    cp "$COMP_SRC" "$COMP_DST"
    echo "Installed $SHELL_NAME completion to $COMP_DST"
    COMP_INSTALLED=true
fi

if ! command -v spawned >/dev/null 2>&1; then
    echo ""
    echo "spawned is not in your PATH."
    echo "Add this to your shell profile:"
    echo "  export PATH=\"$INSTALL_DIR:\$PATH\""
    echo ""
    echo "For bash: ~/.bashrc or ~/.bash_profile"
    echo "For zsh:  ~/.zshrc"
fi

if [ "$COMP_INSTALLED" = true ] && [ "$SHELL_NAME" = "zsh" ]; then
    echo ""
    echo "To enable zsh completions, add this to your .zshrc if not already present:"
    echo "  fpath+=(~/.zfunc)"
fi
