#!/usr/bin/env bash
# Hostking Pro CLI installer.
#
# Usage:
#   curl -fsSL https://www.hostkingpro.com/install.sh | sh
#   curl -fsSL https://www.hostkingpro.com/install.sh | sh -s -- --version=v0.2.0
#   curl -fsSL https://www.hostkingpro.com/install.sh | sh -s -- --dest=/usr/local/bin
#
# What it does:
#   1. Detects the OS + architecture.
#   2. Downloads the matching tarball from GitHub releases.
#   3. Extracts the hostking binary to a directory on PATH.
#   4. Optionally installs the bash completion script.
#
# Requirements: curl, tar. That's it.

set -euo pipefail

REPO="razorwebs/hostkingpro"
DEST="${HOME}/.local/bin"
VERSION="latest"
INSTALL_COMPLETION=1

# Parse flags
while [[ $# -gt 0 ]]; do
  case "$1" in
    --version=*) VERSION="${1#*=}" ;;
    --version)   VERSION="$2"; shift ;;
    --dest=*)    DEST="${1#*=}" ;;
    --dest)      DEST="$2"; shift ;;
    --no-completion) INSTALL_COMPLETION=0 ;;
    -h|--help)
      cat <<'USAGE'
Usage: install.sh [options]

Options:
  --version <v>       Install a specific version (default: latest)
  --dest <path>       Where to put the binary (default: ~/.local/bin)
  --no-completion     Skip the bash completion install
  -h, --help          Show this help

Examples:
  curl -fsSL https://www.hostkingpro.com/install.sh | sh
  curl -fsSL https://www.hostkingpro.com/install.sh | sh -s -- --version=v0.2.0
USAGE
      exit 0
      ;;
    *) echo "Unknown flag: $1" >&2; exit 1 ;;
  esac
  shift
done

# Detect OS + arch
OS=$(uname -s | tr '[:upper:]' '[:lower:]')
ARCH=$(uname -m)
case "$ARCH" in
  x86_64) ARCH=amd64 ;;
  aarch64|arm64) ARCH=arm64 ;;
  *) echo "Unsupported architecture: $ARCH" >&2; exit 1 ;;
esac
case "$OS" in
  linux|darwin) ;;
  *) echo "Unsupported OS: $OS" >&2; exit 1 ;;
esac

# Resolve the version to a tag
if [[ "$VERSION" == "latest" ]]; then
  VERSION=$(curl -fsSL "https://api.github.com/repos/$REPO/releases/latest" | grep -oE '"tag_name": *"[^"]+"' | head -1 | cut -d'"' -f4)
  if [[ -z "$VERSION" ]]; then
    echo "Could not determine the latest version. Check your network or pass --version=v0.2.0." >&2
    exit 1
  fi
fi

# Download
TARBALL="hostking-${VERSION}-${OS}-${ARCH}.tar.gz"
URL="https://github.com/$REPO/releases/download/$VERSION/$TARBALL"
TMP=$(mktemp -d)
trap "rm -rf $TMP" EXIT

echo "downloading $TARBALL..."
if ! curl -fsSL -o "$TMP/$TARBALL" "$URL"; then
  echo "download failed: $URL" >&2
  echo "If this release doesn't have a prebuilt binary yet, install via npm:" >&2
  echo "  npm install -g hostking" >&2
  exit 1
fi

tar -xzf "$TMP/$TARBALL" -C "$TMP"
mkdir -p "$DEST"
install -m 0755 "$TMP/hostking" "$DEST/hostking"

# Bash completion
if [[ $INSTALL_COMPLETION -eq 1 ]] && [[ -d "$HOME/.local/share/bash-completion/completions" ]]; then
  cp "$TMP/hostking.bash" "$HOME/.local/share/bash-completion/completions/hostking" 2>/dev/null || true
  echo "bash completion installed (restart your shell to enable)"
fi

echo ""
echo "✓ installed $DEST/hostking (version $VERSION)"
echo ""
echo "next steps:"
echo "  1. Make sure $DEST is on your PATH:"
echo "     export PATH=\"\$HOME/.local/bin:\$PATH\""
echo "  2. Mint an API token at https://www.hostkingpro.com/dashboard/api-keys"
echo "  3. Authenticate:"
echo "     hostking login"
echo "  4. Try it:"
echo "     hostking sites:list"
