#!/usr/bin/env bash
set -u
LINES="${1:-20}"
BRIDGE_DATE="$(date +%F)"
DEV_LOG_DIR="/var/www/html/serversoap/whatsapp/logs/dev"
PROD_LOG_DIR="/var/www/html/serversoap/whatsapp/logs/prod"
case "$LINES" in
  ''|*[!0-9]*) echo "Uso: $0 [cantidad_lineas]" >&2; exit 2 ;;
esac

echo '=== Estado de Apache ==='
systemctl is-active apache2
apache2ctl configtest

echo
echo '=== Puertos del puente ==='
ss -ltnp | grep -E ':(8090|8094)\b' || true

echo
echo '=== Conectividad a backends (GET: 405 es esperado) ==='
curl -sS -o /dev/null -w 'Pruebas    10.231.0.136:8094 -> HTTP %{http_code}, %{time_total}s\n' --connect-timeout 5 --max-time 12 http://10.231.0.136:8094/webhook || true
curl -sS -o /dev/null -w 'Produccion 10.1.1.4:8094     -> HTTP %{http_code}, %{time_total}s\n' --connect-timeout 5 --max-time 12 http://10.1.1.4:8094/webhook || true

echo
echo "=== Ultimas $LINES solicitudes de pruebas ==="
tail -n "$LINES" "$DEV_LOG_DIR/$BRIDGE_DATE-access.log" 2>/dev/null || true

echo
echo "=== Ultimas $LINES solicitudes de produccion ==="
tail -n "$LINES" "$PROD_LOG_DIR/$BRIDGE_DATE-access.log" 2>/dev/null || true

echo
echo '=== Errores recientes ==='
echo '-- Pruebas --'
tail -n "$LINES" "$DEV_LOG_DIR/$BRIDGE_DATE-error.log" 2>/dev/null || true
echo '-- Produccion --'
tail -n "$LINES" "$PROD_LOG_DIR/$BRIDGE_DATE-error.log" 2>/dev/null || true
