#!/usr/bin/env bash
# Start Blockscout service on VMID 5000 (pve2)
# Usage: ./start-blockscout.sh [host]

VMID=5000
PROXMOX_HOST="${1:-pve2}"

echo "========================================="
echo "Starting Blockscout Service"
echo "VMID: $VMID"
echo "Host: $PROXMOX_HOST"
echo "========================================="
echo ""

# Check if we're on Proxmox host or need to use pct directly
if command -v pct &>/dev/null; then
    echo "1. Checking container status..."
    CONTAINER_STATUS=$(pct status $VMID 2>/dev/null || echo "not found")
    
    if [[ "$CONTAINER_STATUS" == *"running"* ]]; then
        echo "   ✅ Container is running"
        
        echo ""
        echo "2. Checking Blockscout service status..."
        SERVICE_STATUS=$(pct exec $VMID -- systemctl is-active blockscout.service 2>/dev/null || echo "inactive")
        
        if [[ "$SERVICE_STATUS" == "active" ]]; then
            echo "   ✅ Blockscout service is already active"
        else
            echo "   ⚠️  Blockscout service is $SERVICE_STATUS"
            echo ""
            echo "3. Starting Blockscout service..."
            if pct exec $VMID -- systemctl start blockscout.service 2>/dev/null; then
                sleep 3
                NEW_STATUS=$(pct exec $VMID -- systemctl is-active blockscout.service 2>/dev/null || echo "inactive")
                if [[ "$NEW_STATUS" == "active" ]]; then
                    echo "   ✅ Blockscout service started successfully"
                else
                    echo "   ⚠️  Service status: $NEW_STATUS"
                    echo "   💡 Check logs: pct exec $VMID -- journalctl -u blockscout -n 50"
                fi
            else
                echo "   ❌ Failed to start Blockscout service"
                echo "   💡 Check logs: pct exec $VMID -- journalctl -u blockscout -n 50"
            fi
        fi
        
        echo ""
        echo "4. Checking Docker containers..."
        DOCKER_PS=$(pct exec $VMID -- docker ps --format "{{.Names}}: {{.Status}}" 2>/dev/null | grep blockscout || echo "")
        if [ -n "$DOCKER_PS" ]; then
            echo "   ✅ Blockscout containers running:"
            echo "$DOCKER_PS" | sed 's/^/      /'
        else
            echo "   ⚠️  No Blockscout containers running"
            echo "   💡 Check service logs for details"
        fi
        
        echo ""
        echo "5. Testing API accessibility..."
        sleep 2
        HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" --connect-timeout 5 "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)"
            echo "   💡 Wait a few minutes and check again"
        else
            echo "   ⚠️  API returned HTTP $HTTP_CODE"
        fi
        
    elif [[ "$CONTAINER_STATUS" == *"stopped"* ]]; then
        echo "   ⚠️  Container is stopped"
        echo "   Starting container..."
        if pct start $VMID 2>/dev/null; then
            echo "   ✅ Container started"
            echo "   💡 Wait a moment, then run this script again to start the service"
        else
            echo "   ❌ Failed to start container"
        fi
    else
        echo "   ❌ Container status: $CONTAINER_STATUS"
        echo "   💡 Container may not exist or be accessible"
    fi
else
    echo "⚠️  pct command not available (not on Proxmox host)"
    echo ""
    echo "To start Blockscout manually on pve2:"
    echo "  ssh root@pve2 'pct exec $VMID -- systemctl start blockscout'"
    echo ""
    echo "Or if you need to start the container first:"
    echo "  ssh root@pve2 'pct start $VMID'"
    echo "  ssh root@pve2 'pct exec $VMID -- systemctl start blockscout'"
fi

echo ""
echo "========================================="
echo "Summary"
echo "========================================="
echo "VMID: $VMID"
echo "Host: $PROXMOX_HOST"
echo "API: https://explorer.d-bis.org/api"
echo ""
echo "To check service status:"
echo "  pct exec $VMID -- systemctl status blockscout"
echo ""
echo "To view logs:"
echo "  pct exec $VMID -- journalctl -u blockscout -n 50 -f"
