#!/usr/bin/env bash
set -euo pipefail

ROOT_DIR="$(cd "$(dirname "$0")/.." && pwd)"
cd "$ROOT_DIR"

echo "[1/5] Garantindo rede Docker compartilhada..."
docker network create shared_network >/dev/null 2>&1 || true

echo "[2/5] Subindo oracle18c e backend..."
docker compose up -d oracle18c backend

echo "[3/5] Aguardando backend responder /health..."
for i in {1..30}; do
  if docker compose exec -T backend python - <<'PY' >/dev/null 2>&1; then
import httpx, sys
try:
    r = httpx.get("http://localhost:8000/api/v1/health", timeout=15)
    if r.status_code == 200:
        sys.exit(0)
except Exception:
    pass
sys.exit(1)
PY
    echo "  Backend OK."
    break
  fi
  echo "  Tentativa ${i}/30... aguardando 5s"
  sleep 5
  if [ "$i" -eq 30 ]; then
    echo "ERRO: backend não respondeu /health."
    exit 1
  fi
done

echo "[4/5] Disparando job do ranking (limpar_antes=true)..."
docker compose exec -T backend python - <<'PY'
import httpx
client = httpx.Client(timeout=120)
resp = client.post("http://localhost:8000/api/v1/ranking/processar", json={"limpar_antes": True})
print("POST /api/v1/ranking/processar ->", resp.status_code, resp.text)
PY

echo "[5/5] Acompanhando status até finalizar..."
docker compose exec -T backend python - <<'PY'
import httpx, time
client = httpx.Client(timeout=120)
while True:
    r = client.get("http://localhost:8000/api/v1/ranking/status")
    data = r.json()
    print(f"{time.strftime('%H:%M:%S')} | running={data.get('running')} | {data.get('processados')}/{data.get('total')} ({data.get('progress')}%) | msg={data.get('mensagem')}")
    if not data.get("running"):
        break
    time.sleep(15)

print("Coletando contagens finais...")
dsn = None
try:
    import os, cx_Oracle
    dsn = os.getenv("ORACLE_LOCAL_DSN", "oracle18c:1521/XEPDB1")
    user = os.getenv("ORACLE_LOCAL_USER", "local123")
    pwd = os.getenv("ORACLE_LOCAL_PASSWORD", "local123")
    conn = cx_Oracle.connect(user, pwd, dsn)
    cur = conn.cursor()
    for sql in [
        "select count(*) from tb_ranking_consultor",
        "select count(*) from tb_ranking_consultor where componente_a>0",
        "select count(*) from tb_ranking_consultor where componente_c>0",
        "select count(*) from tb_ranking_consultor where componente_d>0",
        "select max(pontuacao_total) from tb_ranking_consultor"
    ]:
        cur.execute(sql)
        print(sql, cur.fetchone()[0])
    cur.close(); conn.close()
except Exception as e:
    print(f"AVISO: não foi possível coletar contagens finais (dsn={dsn}): {e}")
PY

echo "Pronto."
