#!/usr/bin/env bash
# 360LM Walkthroughs — laptop installer
# Run: curl -fsSL https://srv1111289.hstgr.cloud/walkthroughs/install.sh | bash

set -euo pipefail

REPO_URL="${LM_REPO_URL:-https://srv1111289.hstgr.cloud/walkthroughs}"
INSTALL_DIR="${LM_INSTALL_DIR:-${HOME}/360lm-walkthroughs}"
SSH_HOST="${LM_SSH_HOST:-srv1111289.hstgr.cloud}"
SSH_USER="${LM_SSH_USER:-root}"

C_RESET='\033[0m'; C_BOLD='\033[1m'; C_GN='\033[32m'; C_YL='\033[33m'; C_RD='\033[31m'; C_BL='\033[34m'
say() { printf "${C_BOLD}${C_BL}==${C_RESET} %s\n" "$*"; }
ok()  { printf "  ${C_GN}✓${C_RESET} %s\n" "$*"; }
warn(){ printf "  ${C_YL}⚠${C_RESET} %s\n" "$*"; }
err() { printf "  ${C_RD}✗${C_RESET} %s\n" "$*"; }
die() { err "$*"; exit 1; }

say "360LM Walkthroughs installer"
say "Install dir: ${INSTALL_DIR}"
say "VPS:         ${SSH_USER}@${SSH_HOST}"
echo

# 1. Node check
if ! command -v node >/dev/null 2>&1; then
  die "Node.js not found. Install Node 20+ first:
   macOS:    brew install node
   Linux:    https://nodejs.org/en/download
   Windows:  https://nodejs.org/en/download (pick LTS, then re-open this terminal)"
fi
NODE_MAJOR=$(node -v | sed 's/v//' | cut -d. -f1)
if [ "${NODE_MAJOR}" -lt 20 ]; then
  die "Node version $(node -v) is too old. Need 20+."
fi
ok "Node $(node -v)"

# 2. npm check
command -v npm >/dev/null 2>&1 || die "npm not found (usually ships with Node)"
ok "npm $(npm -v)"

# 3. ssh / curl / tar
for tool in ssh curl tar; do
  command -v ${tool} >/dev/null 2>&1 || die "${tool} not found in PATH"
done
ok "ssh, curl, tar all present"

# 4. dig (optional — runner falls back to SSH if absent)
if command -v dig >/dev/null 2>&1; then ok "dig present (DNS checks will run locally)"
else warn "dig not present — DNS checks will be proxied through SSH. (Install: brew install bind or apt install dnsutils)"
fi

# 5. Create install dir + download tarball
say "Downloading framework from ${REPO_URL}"
mkdir -p "${INSTALL_DIR}"
cd "${INSTALL_DIR}"
TMP=$(mktemp -t walkthroughs.XXXXXX.tar.gz)
curl -fsSL "${REPO_URL}/dist/walkthroughs.tar.gz" -o "${TMP}" || die "Download failed (is the VPS reachable from this laptop?)"
tar -xzf "${TMP}"
rm -f "${TMP}"
ok "Framework extracted to ${INSTALL_DIR}"

# 6. npm install
say "Installing npm dependencies"
npm install --silent --no-fund --no-audit
ok "npm deps installed"

# 7. Playwright Chromium
say "Installing Playwright Chromium (~150MB download — first run only)"
npx --yes playwright install chromium
ok "Chromium ready"

# 8. SSH to VPS test
say "Testing passwordless SSH to ${SSH_USER}@${SSH_HOST}"
if ssh -o BatchMode=yes -o ConnectTimeout=6 "${SSH_USER}@${SSH_HOST}" "echo ok" >/dev/null 2>&1; then
  ok "SSH works"
else
  warn "SSH key not set up yet."
  echo "      Run this once, then re-run the installer:"
  echo "        ssh-copy-id ${SSH_USER}@${SSH_HOST}"
fi

# 9. PATH wiring
SHELL_RC=""
case "${SHELL:-}" in
  */zsh)  SHELL_RC="${HOME}/.zshrc" ;;
  */bash) SHELL_RC="${HOME}/.bashrc" ;;
  *)      [ -f "${HOME}/.zshrc" ] && SHELL_RC="${HOME}/.zshrc" || SHELL_RC="${HOME}/.bashrc" ;;
esac
LINE='export PATH="$HOME/360lm-walkthroughs/bin:$PATH"'
if [ -n "${SHELL_RC}" ] && [ -f "${SHELL_RC}" ] && ! grep -qF "360lm-walkthroughs/bin" "${SHELL_RC}"; then
  echo "${LINE}" >> "${SHELL_RC}"
  ok "Added walkthrough to PATH in ${SHELL_RC}"
  warn "Open a NEW terminal (or run:  source ${SHELL_RC} )"
else
  ok "PATH already configured"
fi
chmod +x "${INSTALL_DIR}/bin/walkthrough"

echo
say "Installed. Verify with:"
echo "    cd ${INSTALL_DIR}"
echo "    ./bin/walkthrough doctor"
echo "    ./bin/walkthrough list"
echo "    ./bin/walkthrough run hello"
echo
say "(once PATH is loaded:  walkthrough list  / walkthrough run brevo_domain_auth )"
