#!/bin/bash
# ROA2WEB Ultrathin Monolith - Services Status Overview
# Shows status of unified backend and frontend

# Script directory
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# Source helper functions
source "$SCRIPT_DIR/scripts/service-helpers.sh"

print_header "ROA2WEB Ultrathin Monolith - Services Status"

# SSH Tunnel check
echo -e "${BLUE}━━━ Infrastructure ━━━${NC}"
if check_service_status 1521 "SSH Tunnel (Oracle)"; then
  :
else
  print_warning "SSH Tunnel not running - Oracle DB connection will fail"
  print_info "Start with: ./ssh-tunnel-prod.sh start (or ./ssh-tunnel-test.sh start for TEST)"
fi
echo ""

# Unified Backend
echo -e "${BLUE}━━━ Unified Backend (port 8000) ━━━${NC}"
if check_service_status 8000 "Unified Backend"; then
  # Try health check
  echo -e "${CYAN}Health check:${NC}"
  health=$(curl -s http://localhost:8000/health 2>&1)
  if echo "$health" | grep -q "api"; then
    echo "$health" | python3 -m json.tool 2>/dev/null || echo "$health"
    print_success "Backend is healthy"
  else
    print_warning "Health check: Not responding properly"
    echo "$health"
  fi

  # Show module status
  echo ""
  echo -e "${CYAN}Modules:${NC}"
  echo "  • Reports API:     http://localhost:8000/api/reports/*"
  echo "  • Data Entry API:  http://localhost:8000/api/data-entry/*"
  echo "  • Telegram API:    http://localhost:8000/api/telegram/*"
  echo "  • Telegram Bot:    Running as background task"
else
  print_info "Start with: ./start-prod.sh (or ./start-test.sh for TEST)"
fi
echo ""

# Frontend
echo -e "${BLUE}━━━ Frontend Unified ━━━${NC}"
if check_service_status 3000 "Frontend Unified"; then
  print_info "Access at: http://localhost:3000"
else
  print_info "Start with: ./start-prod.sh (or ./start-test.sh for TEST)"
fi
echo ""

# Log files summary
print_header "Log Files"
echo -e "${BLUE}Recent logs (last modified):${NC}"
ls -lht /tmp/unified_backend*.log /tmp/unified_frontend*.log 2>/dev/null | head -10 || print_warning "No log files found"
echo ""

# Quick stats
print_header "Quick Stats"
running_count=0
for port in 1521 8000 3000; do
  if netstat -tuln 2>/dev/null | grep -q ":${port} " || ss -tuln 2>/dev/null | grep -q ":${port} "; then
    running_count=$((running_count + 1))
  fi
done

echo -e "${BLUE}Services running: ${GREEN}${running_count}/3${NC}"
if [ $running_count -eq 3 ]; then
  print_success "All services are running!"
elif [ $running_count -ge 2 ]; then
  print_warning "Some services are not running"
else
  print_error "Most services are offline"
fi
echo ""

# Helpful commands
echo -e "${BLUE}━━━ Helpful Commands ━━━${NC}"
echo "  ./status.sh                 # Show this status"
echo "  ./start-prod.sh             # Start all services (PROD)"
echo "  ./start-test.sh             # Start all services (TEST)"
echo "  ./start-prod.sh stop        # Stop all services"
echo "  ./start-backend.sh status   # Detailed backend status"
echo "  ./start-backend.sh restart  # Restart backend only"
echo "  ./start-frontend.sh restart # Restart frontend (quick!)"
echo "  tail -f /tmp/unified_backend_prod.log    # Watch backend logs (PROD)"
echo "  tail -f /tmp/unified_backend_test.log    # Watch backend logs (TEST)"
echo "  tail -f /tmp/unified_frontend_prod.log   # Watch frontend logs (PROD)"
echo ""

# API Documentation links
echo -e "${BLUE}━━━ API Documentation ━━━${NC}"
if check_service_status 8000 ""; then
  echo "  • Unified API Docs:  http://localhost:8000/docs"
  echo "  • Health Endpoint:   http://localhost:8000/health"
  echo "  • Root Endpoint:     http://localhost:8000/"
else
  echo "  (Backend not running)"
fi
echo ""
