#!/usr/bin/env bash
#
# Run Systemd Architecture Diagnosis on Remote Server
# Executes the diagnostic script on the actual server to get real data
#

set -e

# Script configuration
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_DIR="$(cd "$SCRIPT_DIR/../.." && pwd)"

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

# Remote connection configuration (using same pattern as other scripts)
REMOTE_HOST="${1:-192.168.20.65}"
REMOTE_USER="${2:-thrillwiki}"
REMOTE_PORT="${3:-22}"
SSH_OPTIONS="-o IdentitiesOnly=yes -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o ConnectTimeout=30"

echo -e "${BLUE}🔍 Running ThrillWiki Systemd Service Architecture Diagnosis on Remote Server${NC}"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
echo "Target: ${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_PORT}"
echo ""

# Test SSH connection first
echo -e "${YELLOW}🔗 Testing SSH connection...${NC}"
if ssh $SSH_OPTIONS -p $REMOTE_PORT $REMOTE_USER@$REMOTE_HOST "echo 'SSH connection successful'" 2>/dev/null; then
    echo -e "${GREEN}✅ SSH connection verified${NC}"
else
    echo -e "${RED}❌ SSH connection failed${NC}"
    echo "Please check:"
    echo "1. SSH key is set up correctly"
    echo "2. Remote host is accessible: $REMOTE_HOST"
    echo "3. Remote user exists: $REMOTE_USER"
    echo "4. SSH port is correct: $REMOTE_PORT"
    exit 1
fi

echo ""
echo -e "${YELLOW}📤 Uploading diagnostic script to remote server...${NC}"

# Upload the diagnostic script to the remote server
if scp $SSH_OPTIONS -P $REMOTE_PORT "$SCRIPT_DIR/diagnose-systemd-architecture.sh" "$REMOTE_USER@$REMOTE_HOST:/tmp/diagnose-systemd-architecture.sh" 2>/dev/null; then
    echo -e "${GREEN}✅ Diagnostic script uploaded successfully${NC}"
else
    echo -e "${RED}❌ Failed to upload diagnostic script${NC}"
    exit 1
fi

echo ""
echo -e "${YELLOW}🔧 Making diagnostic script executable on remote server...${NC}"

# Make the script executable
if ssh $SSH_OPTIONS -p $REMOTE_PORT $REMOTE_USER@$REMOTE_HOST "chmod +x /tmp/diagnose-systemd-architecture.sh" 2>/dev/null; then
    echo -e "${GREEN}✅ Script made executable${NC}"
else
    echo -e "${RED}❌ Failed to make script executable${NC}"
    exit 1
fi

echo ""
echo -e "${YELLOW}🚀 Running diagnostic on remote server...${NC}"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""

# Run the diagnostic script on the remote server
ssh $SSH_OPTIONS -p $REMOTE_PORT $REMOTE_USER@$REMOTE_HOST "/tmp/diagnose-systemd-architecture.sh" || {
    echo ""
    echo -e "${RED}❌ Diagnostic script execution failed${NC}"
    exit 1
}

echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo -e "${GREEN}✅ Remote diagnostic completed successfully${NC}"

echo ""
echo -e "${YELLOW}🧹 Cleaning up temporary files on remote server...${NC}"

# Clean up the uploaded script
ssh $SSH_OPTIONS -p $REMOTE_PORT $REMOTE_USER@$REMOTE_HOST "rm -f /tmp/diagnose-systemd-architecture.sh" 2>/dev/null || {
    echo -e "${YELLOW}⚠️  Warning: Could not clean up temporary file${NC}"
}

echo -e "${GREEN}✅ Cleanup completed${NC}"
echo ""
echo -e "${BLUE}📋 Diagnosis complete. Review the output above to identify systemd service issues.${NC}"