#!/usr/bin/env bash
#
# Quick API Diagnostic Script
# Checks why the API is not starting
#

set -euo pipefail

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

VPS_HOST="vps-4d8abcb7"
VPS_USER="myroadtrip"
REPO_PATH="/srv/myroadtrip/repo"
API_SERVICE="myroadtrip-api"

echo -e "${BLUE}=== MyRoadTrip API Diagnostics ===${NC}\n"

# 1. Service Status
echo -e "${YELLOW}[1/8]${NC} Service Status:"
ssh ubuntu@${VPS_HOST} "sudo systemctl status ${API_SERVICE} --no-pager -l | head -15"
echo ""

# 2. Check if process is running
echo -e "${YELLOW}[2/8]${NC} Process Check:"
ssh ubuntu@${VPS_HOST} "ps aux | grep 'node.*server.js' | grep -v grep || echo 'No Node process found'"
echo ""

# 3. Check port binding
echo -e "${YELLOW}[3/8]${NC} Port Binding:"
ssh ubuntu@${VPS_HOST} "sudo netstat -tlnp | grep -E ':(3000|3001)' || echo 'No service on port 3000 or 3001'"
echo ""

# 4. Check build output
echo -e "${YELLOW}[4/8]${NC} Build Output:"
ssh ubuntu@${VPS_HOST} "ls -lh ${REPO_PATH}/apps/api/dist/server.js 2>/dev/null || echo 'server.js not found'"
echo ""

# 5. Check Node version
echo -e "${YELLOW}[5/8]${NC} Node Version:"
ssh ubuntu@${VPS_HOST} "node --version"
echo ""

# 6. Check environment file
echo -e "${YELLOW}[6/8]${NC} Environment File:"
ssh ubuntu@${VPS_HOST} "sudo -u ${VPS_USER} bash -lc 'ls -lh ${REPO_PATH}/apps/api/.env.production 2>/dev/null || echo \".env.production not found\"'"
echo ""

# 7. Recent error logs
echo -e "${YELLOW}[7/8]${NC} Recent Error Logs (last 20 lines):"
ssh ubuntu@${VPS_HOST} "sudo journalctl -u ${API_SERVICE} -n 20 --no-pager | tail -20"
echo ""

# 8. Service configuration
echo -e "${YELLOW}[8/8]${NC} Service Configuration:"
ssh ubuntu@${VPS_HOST} "cat /etc/systemd/system/${API_SERVICE}.service"
echo ""

# Summary
echo -e "${BLUE}=== Summary ===${NC}"
echo ""

# Check health endpoint
echo -e "${YELLOW}Health Check:${NC}"
HEALTH_STATUS=$(ssh ubuntu@${VPS_HOST} "curl -s -o /dev/null -w '%{http_code}' http://localhost:3001/health 2>/dev/null || echo 'FAIL'")

if [ "$HEALTH_STATUS" = "200" ]; then
    echo -e "${GREEN}✓ API is responding (HTTP 200)${NC}"
else
    echo -e "${RED}✗ API is not responding (Status: $HEALTH_STATUS)${NC}"
fi

# Check if service is active
SERVICE_ACTIVE=$(ssh ubuntu@${VPS_HOST} "sudo systemctl is-active ${API_SERVICE} || echo 'inactive'")

if [ "$SERVICE_ACTIVE" = "active" ]; then
    echo -e "${GREEN}✓ Service is active${NC}"
else
    echo -e "${RED}✗ Service is $SERVICE_ACTIVE${NC}"
fi

echo ""
echo -e "${BLUE}=== Quick Actions ===${NC}"
echo ""
echo "View live logs:"
echo "  ssh ubuntu@${VPS_HOST} \"sudo journalctl -u ${API_SERVICE} -f\""
echo ""
echo "Restart service:"
echo "  ssh ubuntu@${VPS_HOST} \"sudo systemctl restart ${API_SERVICE}\""
echo ""
echo "Manual start (see errors):"
echo "  ssh ubuntu@${VPS_HOST}"
echo "  sudo -u ${VPS_USER} bash -lc 'cd ${REPO_PATH}/apps/api && node dist/server.js'"
echo ""
echo "Check Caddy logs:"
echo "  ssh ubuntu@${VPS_HOST} \"sudo journalctl -u caddy -n 50 --no-pager\""
