#!/bin/bash
# deploy-dev.sh — Rsync uncommitted local changes to the dev instance
#
# The dev instance lives at dev.techtoschool.com on the same server as prod.
# It runs from /var/www/html/mkl-dev/ on ports 3004 (core) + 3005 (products),
# sharing the prod MariaDB. See plans/sequential-pondering-volcano.md.
#
# Unlike deploy.sh, this script rsyncs your current working tree (committed
# or not) so you can iterate on UI changes without committing first. The
# PM2 processes mkl-dev-core / mkl-dev-products are restarted after sync.
#
# Usage:
#   ./scripts/deploy-dev.sh              # Full sync + restart both services
#   ./scripts/deploy-dev.sh --core       # Full sync + restart only mkl-dev-core
#   ./scripts/deploy-dev.sh --products   # Full sync + restart only mkl-dev-products
#   ./scripts/deploy-dev.sh --no-restart # Sync only, no restart
#   ./scripts/deploy-dev.sh --status     # Show what would change (dry-run)

set -euo pipefail

REMOTE="mkl"
REMOTE_DIR="/var/www/html/mkl-dev"
LOCAL_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"

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

# ── Parse args ──────────────────────────────────────────
RESTART_CORE=false
RESTART_PRODUCTS=false
SKIP_RESTART=false
STATUS_ONLY=false

for arg in "$@"; do
    case "$arg" in
        --core) RESTART_CORE=true ;;
        --products) RESTART_PRODUCTS=true ;;
        --no-restart) SKIP_RESTART=true ;;
        --status) STATUS_ONLY=true ;;
        -h|--help)
            head -20 "$0" | tail -16
            exit 0
            ;;
    esac
done

# Default: restart both if no specific target
if [[ "$RESTART_CORE" == "false" && "$RESTART_PRODUCTS" == "false" && "$SKIP_RESTART" == "false" ]]; then
    RESTART_CORE=true
    RESTART_PRODUCTS=true
fi

echo -e "${CYAN}═══ MKL Dev Deploy (dev.techtoschool.com) ═══${NC}"
echo ""
echo -e "Local:     ${GREEN}${LOCAL_DIR}${NC}"
echo -e "Remote:    ${GREEN}${REMOTE}:${REMOTE_DIR}${NC}"
echo ""

# Rsync excludes — matches mkl-dev isolation (shared node_modules, separate .env,
# no git tree, no prod-only state like orders/.deploy-sha)
RSYNC_EXCLUDES=(
    --exclude=node_modules
    --exclude=.git
    --exclude=.env
    --exclude=.env.bak-*
    --exclude="orders/.deploy-sha"
    --exclude=uploads
    --exclude=tmp
    --exclude=.claude
    --exclude=.worktrees
    --exclude="*.pyc"
    --exclude=__pycache__
    --exclude=".DS_Store"
)

if [[ "$STATUS_ONLY" == "true" ]]; then
    echo -e "${CYAN}Dry-run — showing what would change:${NC}"
    rsync -avn --delete --filter=':- .gitignore' "${RSYNC_EXCLUDES[@]}" "$LOCAL_DIR/" "$REMOTE:$REMOTE_DIR/" | head -80
    echo ""
    echo -e "${YELLOW}(Dry run — nothing changed. Re-run without --status to deploy.)${NC}"
    exit 0
fi

echo -e "${CYAN}Syncing files to dev instance...${NC}"
rsync -a --delete --filter=':- .gitignore' "${RSYNC_EXCLUDES[@]}" "$LOCAL_DIR/" "$REMOTE:$REMOTE_DIR/"
echo -e "${GREEN}Sync complete ✓${NC}"
echo ""

if [[ "$SKIP_RESTART" == "true" ]]; then
    echo -e "${YELLOW}Skipping restart (--no-restart specified)${NC}"
    exit 0
fi

if [[ "$RESTART_CORE" == "true" ]]; then
    echo -e "${CYAN}Restarting mkl-dev-core...${NC}"
    ssh "$REMOTE" 'pm2 restart mkl-dev-core --update-env' | grep -E '(restart|online|error)' || true
    echo -e "${GREEN}mkl-dev-core restarted ✓${NC}"
fi

if [[ "$RESTART_PRODUCTS" == "true" ]]; then
    echo -e "${CYAN}Restarting mkl-dev-products...${NC}"
    ssh "$REMOTE" 'pm2 restart mkl-dev-products --update-env' | grep -E '(restart|online|error)' || true
    echo -e "${GREEN}mkl-dev-products restarted ✓${NC}"
fi

echo ""
echo -e "${GREEN}═══ Dev deploy complete ═══${NC}"
echo -e "Visit: ${CYAN}https://dev.techtoschool.com${NC}"
