#!/usr/bin/env bash
# ─────────────────────────────────────────────────────────────────────────────
# Go Education Project — Deploy to Cloudflare Pages
# Usage: ./deploy.sh [--dry-run]
# ─────────────────────────────────────────────────────────────────────────────
set -euo pipefail

SITE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
CF_PROJECT="goeducation-site"
CF_BRANCH="main"
DRY_RUN=false

# ── Parse args ───────────────────────────────────────────────────────────────
for arg in "$@"; do
  case $arg in
    --dry-run) DRY_RUN=true ;;
    *) echo "Unknown argument: $arg"; exit 1 ;;
  esac
done

# ── Colors ───────────────────────────────────────────────────────────────────
RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'
CYAN='\033[0;36m'; BOLD='\033[1m'; RESET='\033[0m'

info()    { echo -e "${CYAN}▶ $*${RESET}"; }
success() { echo -e "${GREEN}✔ $*${RESET}"; }
warn()    { echo -e "${YELLOW}⚠ $*${RESET}"; }
error()   { echo -e "${RED}✖ $*${RESET}"; exit 1; }
step()    { echo -e "\n${BOLD}── $* ──────────────────────────────────────${RESET}"; }

# ── Checks ───────────────────────────────────────────────────────────────────
step "Pre-flight checks"

cd "$SITE_DIR"

# Node via nvm
if command -v nvm &>/dev/null || [ -s "$HOME/.nvm/nvm.sh" ]; then
  source "$HOME/.nvm/nvm.sh"
  nvm use 20 --silent
  success "Node $(node --version)"
else
  error "nvm not found. Install it or ensure Node 20 is in PATH."
fi

# Local node_modules/.bin takes priority — wrangler, pagefind, tailwindcss come from here
export PATH="$SITE_DIR/node_modules/.bin:$PATH"

# Install deps first if node_modules is missing (needed for tool checks below)
if [ ! -d "$SITE_DIR/node_modules" ]; then
  info "node_modules not found — running npm ci..."
  npm ci
fi

# Check required tools
command -v hugo        &>/dev/null || error "hugo not found."
command -v wrangler    &>/dev/null || error "wrangler not found. Run: npm install"
command -v pagefind    &>/dev/null || error "pagefind not found. Run: npm install"
command -v tailwindcss &>/dev/null || error "tailwindcss not found. Run: npm install"

info "Hugo:      $(hugo version | head -1 | cut -d' ' -f2)"
info "Wrangler:  $(wrangler --version 2>&1 | head -1)"
info "Pagefind:  $(pagefind --version 2>&1 | head -1)"

$DRY_RUN && warn "DRY RUN — build will run but upload will be skipped."

# ── npm deps (already handled above if missing) ───────────────────────────────
step "npm dependencies"
success "node_modules present (wrangler $(wrangler --version 2>&1 | head -1))"

# ── Hugo build ────────────────────────────────────────────────────────────────
step "Hugo build"

info "Building site..."
rm -rf "$SITE_DIR/public"
hugo --minify --environment production
success "Hugo build complete ($(find public -name '*.html' | wc -l | tr -d ' ') HTML pages)"

# ── Pagefind — search index ───────────────────────────────────────────────────
step "Pagefind search index"

info "Indexing content for search..."
pagefind --site public
success "Search index built"

# ── Deploy ────────────────────────────────────────────────────────────────────
step "Deploy → Cloudflare Pages"

if $DRY_RUN; then
  warn "Dry run: skipping upload."
  warn "Would deploy: $(du -sh public | cut -f1) to project '$CF_PROJECT' (branch: $CF_BRANCH)"
else
  info "Uploading to Cloudflare Pages..."
  wrangler pages deploy public \
    --project-name "$CF_PROJECT" \
    --branch "$CF_BRANCH"
  echo ""
  success "Deploy complete! → https://${CF_PROJECT}.pages.dev"
fi
