#!/bin/bash

# ============================================================================
# POUNCE SYSTEMD SERVICE INSTALLER
# Run this script to install auto-start services on the server
# ============================================================================

set -euo pipefail

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

SERVER_USER="user"
SERVER_HOST="10.42.0.73"
SERVER_PASS="user"
SSH_OPTS="-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"

if ! command -v sshpass >/dev/null 2>&1; then
    echo "sshpass is required but not installed. Install with: brew install sshpass"
    exit 1
fi

echo -e "${BLUE}═══════════════════════════════════════════════════════════════${NC}"
echo -e "${BLUE}           POUNCE SYSTEMD SERVICE INSTALLER                    ${NC}"
echo -e "${BLUE}═══════════════════════════════════════════════════════════════${NC}"

# Get script directory
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"

echo -e "\n${YELLOW}[1/4] Copying service files to server...${NC}"
sshpass -p "$SERVER_PASS" scp $SSH_OPTS "$SCRIPT_DIR/pounce-backend.service" "$SERVER_USER@$SERVER_HOST:/tmp/"
sshpass -p "$SERVER_PASS" scp $SSH_OPTS "$SCRIPT_DIR/pounce-frontend.service" "$SERVER_USER@$SERVER_HOST:/tmp/"
echo "  ✓ Service files copied"

echo -e "\n${YELLOW}[2/4] Installing services (requires sudo)...${NC}"
sshpass -p "$SERVER_PASS" ssh $SSH_OPTS "$SERVER_USER@$SERVER_HOST" << 'EOF'
    # Move service files to systemd directory
    echo "user" | sudo -S cp /tmp/pounce-backend.service /etc/systemd/system/
    echo "user" | sudo -S cp /tmp/pounce-frontend.service /etc/systemd/system/
    
    # Set correct permissions
    echo "user" | sudo -S chmod 644 /etc/systemd/system/pounce-backend.service
    echo "user" | sudo -S chmod 644 /etc/systemd/system/pounce-frontend.service
    
    echo "  ✓ Service files installed"
EOF

echo -e "\n${YELLOW}[3/4] Stopping existing processes...${NC}"
sshpass -p "$SERVER_PASS" ssh $SSH_OPTS "$SERVER_USER@$SERVER_HOST" << 'EOF'
    # Kill existing nohup processes
    pkill -f 'uvicorn app.main:app' 2>/dev/null || true
    pkill -f 'node .next/standalone/server.js' 2>/dev/null || true
    sleep 2
    echo "  ✓ Existing processes stopped"
EOF

echo -e "\n${YELLOW}[4/4] Enabling and starting services...${NC}"
sshpass -p "$SERVER_PASS" ssh $SSH_OPTS "$SERVER_USER@$SERVER_HOST" << 'EOF'
    # Reload systemd daemon
    echo "user" | sudo -S systemctl daemon-reload
    
    # Enable services (auto-start on boot)
    echo "user" | sudo -S systemctl enable pounce-backend
    echo "user" | sudo -S systemctl enable pounce-frontend
    
    # Start services
    echo "user" | sudo -S systemctl start pounce-backend
    sleep 3
    echo "user" | sudo -S systemctl start pounce-frontend
    sleep 2
    
    # Check status
    echo ""
    echo "  Backend status:"
    systemctl status pounce-backend --no-pager -l | head -15 || true
    echo ""
    echo "  Frontend status:"
    systemctl status pounce-frontend --no-pager -l | head -15 || true
EOF

echo -e "\n${GREEN}═══════════════════════════════════════════════════════════════${NC}"
echo -e "${GREEN}                 ✅ SERVICES INSTALLED!                        ${NC}"
echo -e "${GREEN}═══════════════════════════════════════════════════════════════${NC}"
echo -e ""
echo -e "  Services will now auto-start after server reboot."
echo -e ""
echo -e "${BLUE}Useful commands (run on server):${NC}"
echo -e "  sudo systemctl status pounce-backend"
echo -e "  sudo systemctl status pounce-frontend"
echo -e "  sudo systemctl restart pounce-backend"
echo -e "  sudo systemctl restart pounce-frontend"
echo -e "  journalctl -u pounce-backend -f"
echo -e "  journalctl -u pounce-frontend -f"

