#!/bin/bash
# =============================================================================
# Frontend Staging Deployment Script
# =============================================================================
# Usage: ./scripts/deploy-frontend.sh <site> [--skip-build]
#
# Deploys a frontend site on sv-frontend via SSH.
#
# Sites:
#   blogwoman    - frontend.blogwoman.de
#   porwoll      - frontend.porwoll.de
#   all          - Deploy all frontends
#
# Examples:
#   ./scripts/deploy-frontend.sh blogwoman
#   ./scripts/deploy-frontend.sh porwoll --skip-build
#   ./scripts/deploy-frontend.sh all
# =============================================================================

set -e

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

# Configuration
SSH_HOST="sv-frontend"
BRANCH="${DEPLOY_BRANCH:-develop}"

# Parse arguments
SITE="$1"
SKIP_BUILD=false

if [ -z "$SITE" ]; then
  echo -e "${RED}Error: No site specified${NC}"
  echo "Usage: $0 <blogwoman|porwoll|all> [--skip-build]"
  exit 1
fi

for arg in "${@:2}"; do
  case $arg in
    --skip-build)
      SKIP_BUILD=true
      ;;
  esac
done

# Map site to directory
get_project_dir() {
  case "$1" in
    blogwoman) echo "frontend.blogwoman.de" ;;
    porwoll)   echo "frontend.porwoll.de" ;;
    *)
      echo -e "${RED}Unknown site: $1${NC}" >&2
      echo "Available: blogwoman, porwoll" >&2
      return 1
      ;;
  esac
}

# Deploy a single frontend
deploy_site() {
  local site="$1"
  local project_dir
  project_dir=$(get_project_dir "$site") || exit 1

  echo ""
  echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
  echo -e "${BLUE}  Deploying: ${GREEN}$site${BLUE} ($project_dir)${NC}"
  echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"

  # Test SSH connection
  if ! ssh -o ConnectTimeout=5 "$SSH_HOST" "echo ok" > /dev/null 2>&1; then
    echo -e "${RED}Cannot connect to $SSH_HOST${NC}"
    exit 1
  fi

  # Deploy via SSH
  ssh "$SSH_HOST" bash -s <<REMOTE_SCRIPT
    set -e
    cd ~/\$project_dir || { echo "Directory not found: ~/\$project_dir"; exit 1; }

    echo "[1/4] Pulling latest code..."
    git fetch origin "$BRANCH"
    git checkout "$BRANCH"
    git pull origin "$BRANCH"
    echo "  Commit: \$(git log -1 --format='%h %s')"

    echo "[2/4] Installing dependencies..."
    pnpm install --frozen-lockfile

    if [ "$SKIP_BUILD" = "false" ]; then
      echo "[3/4] Building..."
      pnpm build
      echo "  Build complete"
    else
      echo "[3/4] Skipping build (--skip-build)"
    fi

    echo "[4/4] Done"
REMOTE_SCRIPT

  local project_dir_escaped="${project_dir}"
  ssh "$SSH_HOST" "cd ~/${project_dir_escaped} && git log -1 --format='%h %s'" 2>/dev/null

  echo -e "${GREEN}  ✓ $site deployed successfully${NC}"
}

# Main
echo "=============================================="
echo "  Frontend Staging Deployment"
echo "  Target: sv-frontend ($SSH_HOST)"
echo "  Branch: $BRANCH"
echo "=============================================="

if [ "$SITE" = "all" ]; then
  deploy_site "blogwoman"
  deploy_site "porwoll"
else
  deploy_site "$SITE"
fi

echo ""
echo "=============================================="
echo -e "${GREEN}  Deployment Complete!${NC}"
echo "=============================================="
echo "  Time: $(date)"
echo "=============================================="
