#!/usr/bin/env bash
# Start Blockscout service on VMID 5000 (pve2) via SSH
# Usage: ./start-blockscout-remote.sh

VMID=5000
PVE2_HOST="192.168.11.12"
PVE2_PASS="password"

echo "========================================="
echo "Starting Blockscout Service (Remote)"
echo "VMID: $VMID"
echo "Host: $PVE2_HOST (pve2)"
echo "========================================="
echo ""

if ! command -v sshpass &>/dev/null; then
    echo "❌ sshpass not found. Install with: apt-get install sshpass"
    echo ""
    echo "Alternatively, SSH manually to pve2:"
    echo "  ssh root@$PVE2_HOST"
    echo "  pct exec $VMID -- systemctl start blockscout"
    exit 1
fi

echo "1. Checking container status..."
CONTAINER_STATUS=$(sshpass -p "$PVE2_PASS" ssh -o StrictHostKeyChecking=no -o ConnectTimeout=10 root@$PVE2_HOST "pct status $VMID 2>&1" 2>&1 || echo "error")

if echo "$CONTAINER_STATUS" | grep -q "running"; then
    echo "   ✅ Container is running"
    
    echo ""
    echo "2. Checking Blockscout service status..."
    SERVICE_STATUS=$(sshpass -p "$PVE2_PASS" ssh -o StrictHostKeyChecking=no -o ConnectTimeout=10 root@$PVE2_HOST "pct exec $VMID -- systemctl is-active blockscout.service 2>&1" 2>&1 || echo "error")
    
    if [[ "$SERVICE_STATUS" == "active" ]]; then
        echo "   ✅ Blockscout service is already active"
    else
        echo "   ⚠️  Blockscout service status: $SERVICE_STATUS"
        echo ""
        echo "3. Starting Blockscout service..."
        START_RESULT=$(sshpass -p "$PVE2_PASS" ssh -o StrictHostKeyChecking=no -o ConnectTimeout=10 root@$PVE2_HOST "pct exec $VMID -- systemctl start blockscout.service 2>&1" 2>&1)
        
        if [ $? -eq 0 ]; then
            sleep 3
            NEW_STATUS=$(sshpass -p "$PVE2_PASS" ssh -o StrictHostKeyChecking=no -o ConnectTimeout=10 root@$PVE2_HOST "pct exec $VMID -- systemctl is-active blockscout.service 2>&1" 2>&1 || echo "error")
            if [[ "$NEW_STATUS" == "active" ]]; then
                echo "   ✅ Blockscout service started successfully"
            else
                echo "   ⚠️  Service status: $NEW_STATUS"
                echo "   💡 Check logs: ssh root@$PVE2_HOST 'pct exec $VMID -- journalctl -u blockscout -n 50'"
            fi
        else
            echo "   ❌ Failed to start Blockscout service"
            echo "   Error: $START_RESULT"
        fi
    fi
    
    echo ""
    echo "4. Checking Docker containers..."
    DOCKER_PS=$(sshpass -p "$PVE2_PASS" ssh -o StrictHostKeyChecking=no -o ConnectTimeout=10 root@$PVE2_HOST "pct exec $VMID -- docker ps --format '{{.Names}}: {{.Status}}' 2>&1 | grep blockscout" 2>&1 || echo "")
    if [ -n "$DOCKER_PS" ]; then
        echo "   ✅ Blockscout containers running:"
        echo "$DOCKER_PS" | sed 's/^/      /'
    else
        echo "   ⚠️  No Blockscout containers running yet (may take a moment to start)"
    fi
    
elif echo "$CONTAINER_STATUS" | grep -q "stopped"; then
    echo "   ⚠️  Container is stopped"
    echo "   Starting container..."
    START_CONTAINER=$(sshpass -p "$PVE2_PASS" ssh -o StrictHostKeyChecking=no -o ConnectTimeout=10 root@$PVE2_HOST "pct start $VMID 2>&1" 2>&1)
    if [ $? -eq 0 ]; then
        echo "   ✅ Container started"
        echo "   💡 Wait a moment, then run this script again to start the service"
    else
        echo "   ❌ Failed to start container: $START_CONTAINER"
    fi
else
    echo "   ❌ Container status check failed: $CONTAINER_STATUS"
    echo "   💡 Container may not exist or be accessible"
fi

echo ""
echo "5. Testing API accessibility..."
sleep 5
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" --connect-timeout 10 "https://explorer.d-bis.org/api" 2>/dev/null || echo "000")
if [[ "$HTTP_CODE" == "200" ]]; then
    echo "   ✅ API is accessible (HTTP $HTTP_CODE)"
elif [[ "$HTTP_CODE" == "502" ]]; then
    echo "   ⚠️  API returns 502 (service may still be starting - wait 1-2 minutes)"
elif [[ "$HTTP_CODE" == "000" ]]; then
    echo "   ⚠️  API connection failed (may still be starting)"
else
    echo "   ⚠️  API returned HTTP $HTTP_CODE"
fi

echo ""
echo "========================================="
echo "Summary"
echo "========================================="
echo "VMID: $VMID"
echo "Host: $PVE2_HOST (pve2)"
echo "API: https://explorer.d-bis.org/api"
echo ""
echo "To check service status:"
echo "  ssh root@$PVE2_HOST 'pct exec $VMID -- systemctl status blockscout'"
echo ""
echo "To view logs:"
echo "  ssh root@$PVE2_HOST 'pct exec $VMID -- journalctl -u blockscout -n 50 -f'"
