#!/usr/bin/env bash
set -euo pipefail

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

# Configuration
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
FRONTEND_DIR="$SCRIPT_DIR/frontend"
BACKEND_DIR="$SCRIPT_DIR/backend"
STATIC_DIR="/var/lib/nvidia-oc/static"
SERVICE_NAME="nvidia-oc"

# Error handler
error_exit() {
    echo -e "${RED}Error: $1${NC}" >&2
    exit 1
}

# Header
echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE}NVIDIA OC Production Upgrade${NC}"
echo -e "${BLUE}========================================${NC}"
echo

# Step 1: Pre-flight checks
echo -e "${YELLOW}[1/7] Running pre-flight checks...${NC}"

# Verify we're in the project root
if [[ ! -f "$SCRIPT_DIR/pyproject.toml" ]] || [[ ! -f "$SCRIPT_DIR/package.json" ]]; then
    error_exit "Not in project root directory"
fi

# Check if systemd service exists
if [[ ! -f "/etc/systemd/system/$SERVICE_NAME.service" ]]; then
    echo -e "${RED}Error: Systemd service not installed${NC}"
    echo -e "${YELLOW}Run: sudo ./scripts/install-service.sh${NC}"
    exit 1
fi

# Check for required commands
for cmd in pnpm uv curl; do
    if ! command -v $cmd &> /dev/null; then
        error_exit "Required command not found: $cmd"
    fi
done

# Check if dev processes are running (port 9421 or 3420)
if lsof -Pi :9421 -sTCP:LISTEN -t >/dev/null 2>&1 || lsof -Pi :3420 -sTCP:LISTEN -t >/dev/null 2>&1; then
    echo -e "${YELLOW}Warning: Development processes detected on ports 9421 or 3420${NC}"
    echo -e "${YELLOW}This is OK - production uses port 9420${NC}"
fi

echo -e "${GREEN}✓ Pre-flight checks passed${NC}"

# Step 2: Build frontend
echo -e "${YELLOW}[2/7] Building frontend...${NC}"
cd "$FRONTEND_DIR"

# Install dependencies
echo -e "  Installing dependencies..."
pnpm install --frozen-lockfile || error_exit "Failed to install frontend dependencies"

# Build production bundle
echo -e "  Building production bundle..."
pnpm build || error_exit "Frontend build failed"

# Verify build output
if [[ ! -d "$FRONTEND_DIR/dist" ]] || [[ -z "$(ls -A $FRONTEND_DIR/dist)" ]]; then
    error_exit "Build output directory is empty or missing"
fi

echo -e "${GREEN}✓ Frontend built successfully${NC}"

# Step 3: Deploy static files
echo -e "${YELLOW}[3/7] Deploying static files...${NC}"

# Create static directory if it doesn't exist (requires sudo)
if [[ ! -d "$STATIC_DIR" ]]; then
    echo -e "  Creating static directory..."
    sudo mkdir -p "$STATIC_DIR" || error_exit "Failed to create static directory (may need sudo)"
fi

# Backup existing deployment if it exists
if [[ -d "$STATIC_DIR" ]] && [[ -n "$(ls -A $STATIC_DIR 2>/dev/null)" ]]; then
    BACKUP_DIR="$STATIC_DIR.backup.$(date +%Y%m%d_%H%M%S)"
    echo -e "  Backing up existing deployment to: $BACKUP_DIR"
    sudo mv "$STATIC_DIR" "$BACKUP_DIR" || echo -e "${YELLOW}  Warning: Backup failed, continuing...${NC}"
    sudo mkdir -p "$STATIC_DIR"
fi

# Copy new build to static directory
echo -e "  Copying files to $STATIC_DIR..."
sudo cp -r "$FRONTEND_DIR/dist"/* "$STATIC_DIR/" || error_exit "Failed to copy static files"

# Set proper permissions
sudo chmod -R 755 "$STATIC_DIR" || echo -e "${YELLOW}  Warning: Failed to set permissions${NC}"

echo -e "${GREEN}✓ Static files deployed${NC}"

# Step 4: Update backend dependencies
echo -e "${YELLOW}[4/7] Updating backend dependencies...${NC}"
cd "$BACKEND_DIR"

echo -e "  Syncing Python dependencies..."
uv sync || error_exit "Failed to sync backend dependencies"

echo -e "${GREEN}✓ Backend dependencies updated${NC}"

# Step 5: Restart service
echo -e "${YELLOW}[5/7] Restarting service...${NC}"

# Check if service is active
if sudo systemctl is-active --quiet "$SERVICE_NAME"; then
    echo -e "  Service is running, restarting..."
    sudo systemctl restart "$SERVICE_NAME" || error_exit "Failed to restart service"
else
    echo -e "  Service is not running, starting..."
    sudo systemctl start "$SERVICE_NAME" || error_exit "Failed to start service"
fi

echo -e "${GREEN}✓ Service restarted${NC}"

# Step 6: Wait and health check
echo -e "${YELLOW}[6/7] Verifying deployment...${NC}"

echo -e "  Waiting for service to initialize..."
sleep 3

# Check if service is running
if ! sudo systemctl is-active --quiet "$SERVICE_NAME"; then
    error_exit "Service is not running after restart"
fi

# Health check
echo -e "  Running health check..."
if curl -sf http://localhost:9420/health > /dev/null; then
    echo -e "${GREEN}✓ Health check passed${NC}"
else
    echo -e "${RED}✗ Health check failed${NC}"
    echo -e "${YELLOW}  Check logs: sudo journalctl -u $SERVICE_NAME -n 50${NC}"
    exit 1
fi

# Step 7: Display status
echo -e "${YELLOW}[7/7] Deployment summary...${NC}"

echo
echo -e "${BLUE}========================================${NC}"
echo -e "${GREEN}Deployment Complete!${NC}"
echo -e "${BLUE}========================================${NC}"
echo

echo -e "${YELLOW}Service Status:${NC}"
sudo systemctl status "$SERVICE_NAME" --no-pager --lines=0 || true
echo

echo -e "${YELLOW}Recent Logs:${NC}"
sudo journalctl -u "$SERVICE_NAME" -n 10 --no-pager || true
echo

echo -e "${YELLOW}Access URLs:${NC}"
echo -e "  Production API: ${BLUE}http://localhost:9420${NC}"
echo -e "  Health Check:   ${BLUE}http://localhost:9420/health${NC}"
echo

echo -e "${YELLOW}Management Commands:${NC}"
echo -e "  View logs:    ${BLUE}sudo journalctl -u $SERVICE_NAME -f${NC}"
echo -e "  Restart:      ${BLUE}sudo systemctl restart $SERVICE_NAME${NC}"
echo -e "  Stop:         ${BLUE}sudo systemctl stop $SERVICE_NAME${NC}"
echo -e "  Status:       ${BLUE}sudo systemctl status $SERVICE_NAME${NC}"
echo

echo -e "${GREEN}Production deployment successful!${NC}"
