#!/bin/bash
# cayce-daily-report (r20) — DM diario al supervisor:
#   1) publicado por canal con enlaces  2) rechazos del guard y por qué
#   3) PRs auto-mergeados + deploys      4) métricas Umami (ambos sitios)
#   5) costo estimado de Gemini del día
set -uo pipefail
OUT=$(mktemp)
PSQL="sudo -n -u postgres psql -d cayce -tA -f-"
DAY=$(date -u +%F)

{
echo "CAYCE — reporte diario $DAY (UTC)"

echo ""
echo "== 1. Publicado (24h) =="
$PSQL <<'EOF'
SELECT 'nostr/' || type || ' — ' || left(regexp_replace(content, E'[\\n\\r]+', ' ', 'g'), 80)
  || ' — ev:' || coalesce(meta->>'event_id', '(sin id)')
FROM outbound_drafts
WHERE status='published' AND channel='nostr'
  AND type NOT IN ('profile','contact_list','inbox_relays','deletion')
  AND ts > now() - interval '24 hours'
ORDER BY ts DESC LIMIT 6;
SELECT 'hardcap/' || type || ' — ' || left(regexp_replace(content, E'[\\n\\r]+', ' ', 'g'), 80)
FROM outbound_drafts
WHERE status='published' AND channel='hardcap' AND ts > now() - interval '24 hours'
ORDER BY ts DESC LIMIT 4;
EOF
sudo -n -u postgres psql -d cayce -tA -f- <<'EOF'
SELECT platform || ' — ' || theme || ' — ' || coalesce(post_url, '(sin url)')
FROM posts_log WHERE ts > now() - interval '24 hours' AND status='ok' ORDER BY ts DESC LIMIT 6;
EOF

echo ""
echo "== 2. Guard: rechazos (24h) =="
sudo -n -u postgres psql -d cayce -tA -f- <<'EOF'
SELECT count(*) || 'x  ' || left(reasons, 100) FROM guard_rejections
WHERE ts > now() - interval '24 hours'
GROUP BY reasons ORDER BY count(*) DESC LIMIT 6;
EOF

echo ""
echo "== 2b. DMs urgentes (politica r33) =="
NDM=$(journalctl -u cayce-nostr --since "-24 hours" --no-pager 2>/dev/null | grep -c "\[dm\] envio a")
UCOUNT=$(python3 -c "import json; print(json.load(open('/var/lib/cayce/dm/urgent-count.json')).get('$(date -u +%F)', 0))" 2>/dev/null || echo 0)
echo "  DMs NIP-04 enviados (24h, todas las causas): $NDM · urgentes de hoy (tope 3): $UCOUNT"
if [ -s /var/lib/cayce/dm/deferred.log ]; then
  echo "  diferidos por tope (van aqui en vez de DM):"
  tail -5 /var/lib/cayce/dm/deferred.log | sed "s/^/    /"
fi
if [ -f /srv/cayce-core/PUBLISH_OFF ]; then
  echo "  kill switch: ACTIVO ($(head -c 20 /srv/cayce-core/PUBLISH_OFF 2>/dev/null))"
fi

echo ""
echo "== 3. PRs y deploys =="
BTOK=$(cat /etc/cayce/ci-bot.token 2>/dev/null)
if [ -n "$BTOK" ]; then
  for REPO in cayce-ops entity bitcoinhotels; do
    curl -s -m 10 -H "Authorization: token $BTOK" \
      "http://127.0.0.1:3100/api/v1/repos/cayce/$REPO/pulls?state=closed&limit=20" \
    | python3 -c "
import json, sys, datetime
cutoff = (datetime.datetime.utcnow() - datetime.timedelta(days=1)).strftime('%Y-%m-%dT%H:%M')
for p in json.load(sys.stdin):
    m = (p.get('merged_at') or '')
    if not p.get('merged') or not m or m < cutoff: continue
    print(f\"$REPO#{p['number']} — {p['title'][:60]}\")" 2>/dev/null
  done
fi
journalctl -u cayce-autodeploy --since "-24 hours" -o cat --no-pager 2>/dev/null | grep -E "^\[autodeploy\].*(→|ROLLBACK|ABORT|FATAL)" | tail -6

echo ""
echo "== 3b. PRs que esperan tu merge =="
PROTECTED="SPEC.md|MANDATE.md|prompts/cayce-identity.md|guard[^/]*\.|relay-watcher|outbox\.mjs|guard-blocklist|check-single-outbox|\.gitea/workflows|\.github/workflows|cayce-auto-merge|act_runner|docker"
if [ -n "${BTOK:-}" ]; then
  for REPO in cayce-ops entity bitcoinhotels; do
    curl -s -m 10 -H "Authorization: token $BTOK" \
      "http://127.0.0.1:3100/api/v1/repos/cayce/$REPO/pulls?state=open&limit=20" \
    | python3 -c "
import json, sys, urllib.request, os
tok = os.environ.get('BTOK')
for p in json.load(sys.stdin):
    req = urllib.request.Request(f'http://127.0.0.1:3100/api/v1/repos/cayce/$REPO/pulls/{p["number"]}/files?limit=100', headers={'Authorization': 'token ' + tok})
    files = [f.get('filename','') for f in json.load(urllib.request.urlopen(req, timeout=10))]
    import re
    prot = [f for f in files if re.search(r'$PROTECTED', f)]
    motivo = 'toca paths protegidos: ' + ', '.join(prot[:3]) if prot else 'CI pendiente o esperando revisión'
    print(f"  $REPO#{p['number']} — {p['title'][:56]} — {motivo}")" 2>/dev/null
  done
fi
echo "  GitHub patternn/hardcap-news: sin token en VPS — revisar https://github.com/patternn/hardcap-news/pulls"

echo ""
echo "== 4. Umami (24h) =="
sudo -n -u postgres psql -d umami -tA -f- <<'EOF'
SELECT w.name || ': ' || count(DISTINCT e.session_id) || ' visitas · ' || count(*) || ' páginas'
FROM website_event e JOIN website w ON w.website_id = e.website_id
WHERE e.created_at > now() - interval '24 hours' AND e.event_type = 1
GROUP BY w.name ORDER BY count(*) DESC;
SELECT 'top: ' || url_path || ' (' || count(*) || ')' FROM website_event
WHERE created_at > now() - interval '24 hours' AND event_type = 1
GROUP BY url_path ORDER BY count(*) DESC LIMIT 4;
SELECT 'refs: ' || coalesce(nullif(referrer_domain, ''), '(direct)') || ' (' || count(DISTINCT session_id) || ')'
FROM website_event
WHERE created_at > now() - interval '24 hours' AND event_type = 1
GROUP BY referrer_domain ORDER BY count(DISTINCT session_id) DESC LIMIT 4;
EOF

echo ""
echo "== Contenido nuevo publicado hoy por sitio =="
# bitcoinhotels: guias/fichas nuevas o actualizadas en el arbol DESPLEGADO (link vivo)
cd /srv/bitcoinhotels 2>/dev/null && git log --since="24 hours" --format="%H" -- src/data/guides.json | while read -r C; do
  git show "$C" -- src/data/guides.json 2>/dev/null | grep -oE '^\+\s+"slug": "[a-z0-9-]+"' | sed 's/.*: "//;s/"//' \
    | while read -r SL; do echo "  bitcoinhotels.org/guides/$SL"; done
done | sort -u
# hardcap: notas publicadas hoy (link por article_id del meta)
sudo -n -u postgres psql -d cayce -tA -f- <<'EOF2' | while read -r L; do [ -n "$L" ] && echo "  hardcap.news$L"; done
SELECT '/news/' || (meta->>'article_id') || '/summary — ' || left(regexp_replace(content, E'[\n\r]+', ' ', 'g'), 60)
FROM outbound_drafts
WHERE channel='hardcap' AND status='published' AND ts > now() - interval '24 hours'
  AND meta->>'article_id' IS NOT NULL
ORDER BY ts DESC LIMIT 6;
EOF2

echo ""
echo "== Ciclos: piezas producidas (24h) =="
sudo -n -u postgres psql -d cayce -tA -f- <<'EOF'
SELECT 'posts: ' || count(*) || ' piezas nostr (opinion/reflection/events/iniciativa)'
FROM outbound_drafts WHERE channel='nostr'
  AND type IN ('opinion','reflection','events_post','initiative_note')
  AND status='published' AND ts > now() - interval '24 hours';
SELECT 'editoriales: ' || count(*) || ' opiniones publicadas · ' || (SELECT count(*) FROM actions_log WHERE action='editorials_cycle' AND ts > now() - interval '24 hours') || ' corridas'
FROM outbound_drafts WHERE channel='nostr' AND type='opinion' AND status='published' AND ts > now() - interval '24 hours';
SELECT 'notas hardcap: ' || count(*) || ' publicadas'
FROM outbound_drafts WHERE channel='hardcap' AND status='published' AND ts > now() - interval '24 hours';
SELECT 'curacion hardcap: ' || count(*) || ' corridas del ciclo core · ' || (SELECT count(*) FROM digests WHERE ts > now() - interval '24 hours') || ' digests'
FROM actions_log WHERE action='cycle_complete' AND ts > now() - interval '24 hours';
SELECT 'promo: ' || count(*) FILTER (WHERE status='ok') || ' publicadas · ' || count(*) FILTER (WHERE status LIKE 'rejected%') || ' rechazadas · ' || count(*) FILTER (WHERE status LIKE 'error%') || ' con error'
FROM posts_log WHERE ts > now() - interval '24 hours';
SELECT 'iniciativa: ' || count(*) || ' drafts creados'
FROM actions_log WHERE action='draft_created' AND ts > now() - interval '24 hours';
EOF

echo "== Checklist de postulacion (verificable en vivo) =="
chk() { # $1 nombre, $2 condicion (0=verde)
  if [ "$2" = "0" ]; then echo "  $1: verde"; else echo "  $1: pendiente"; fi
}
for SITE in bh hc; do
  [ "$SITE" = "bh" ] && P=3002 && D=bitcoinhotels.org || { P=3003; D=hardcap.news; }
  N=$(curl -s -m 6 http://127.0.0.1:$P/sitemap.xml | grep -c "<loc>")
  TR=0; for PG in about contact privacy terms; do C=$(curl -s -o /dev/null -w "%{http_code}" -m 5 http://127.0.0.1:$P/$PG); [ "$C" = "200" ] && TR=$((TR+1)); done
  HWW=$(curl -s -o /dev/null -w "%{http_code}" -m 5 http://127.0.0.1:$P/how-we-work)
  ADS=$(curl -s -o /dev/null -w "%{http_code}" -m 5 http://127.0.0.1:$P/ads.txt)
  PC=$(curl -s -o /dev/null -w "%{http_code}" -m 5 http://127.0.0.1:$P/privacy)
  PRIV=$(curl -s -m 5 http://127.0.0.1:$P/privacy | grep -c -i "adsense"); [ "$PC" != "200" ] && PRIV=0
  echo "$D: sitemap=$N URLs · confianza $TR/4 · how-we-work $([ "$HWW" = "200" ] && echo SI || echo NO) · ads.txt $([ "$ADS" = "200" ] && echo SI || echo NO) · privacy-AdSense $([ "$PRIV" -ge 1 ] && echo SI || echo NO)"
done

if [ "$(date -u +%u)" = "1" ]; then
  echo "== Semanal (lunes): checklist AdSense =="
  for SITE in bh hc; do
    [ "$SITE" = "bh" ] && P=3002 && N=bitcoinhotels.org || { P=3003; N=hardcap.news; }
    OK=0; TOTAL=5
    for PG in sitemap.xml ads.txt privacy terms how-we-work; do
      C=$(curl -s -o /dev/null -w "%{http_code}" -m 5 http://127.0.0.1:$P/$PG); [ "$C" = "200" ] && OK=$((OK+1))
    done
    echo "  $N: $OK/$TOTAL (sitemap · ads.txt · privacy · terms · how-we-work)"
  done
fi

echo "== Pushes (24h) por autor =="
for R in cayce-ops entity bitcoinhotels; do
  A=$(cd /srv/$([ "$R" = "cayce-ops" ] && echo home/ubuntu/cayce-ops || { [ "$R" = "entity" ] && echo cayce-core || echo bitcoinhotels; }) 2>/dev/null && git log --since="24 hours" --format="%an" | sort | uniq -c | tr "\n" ";" )
  echo "  $R: ${A:-sin commits}"
done

echo "== Avance semanal FASE B (piezas vs objetivo) =="
# convencion: los commits de piezas FASE B llevan prefijo "pieza:"
SEM=$(( ($(date -u +%s) / 86400 + 3) / 7 ))  # semana ISO aprox
for R in cayce-ops entity bitcoinhotels; do
  D=/srv/$([ "$R" = "cayce-ops" ] && echo home/ubuntu/cayce-ops || { [ "$R" = "entity" ] && echo cayce-core || echo bitcoinhotels; })
  N=$(cd "$D" 2>/dev/null && git log --since="7 days" --format="%s" | grep -c "^pieza:")
  echo "  $R: $N piezas-commits esta semana"
done
echo "  objetivos: bitcoinhotels 2/sem · hardcap ~2,5/sem (hub+watch+guia alterna)"

echo "== Persistencias (caidos ahora mismo) =="
for U in cayce-nostr.service hardcap-backend.service hardcap-frontend.service cayce-bitcoinhotels.service postgresql@18-main.service caddy.service; do
  A=$(systemctl is-active $U 2>/dev/null)
  [ "$A" != "active" ] && echo "  $U: $A"
done
true

echo "== 5. Gemini del día =="
python3 - /srv/cayce-core/.llm-usage.jsonl <<'PY' 2>/dev/null || echo "(sin uso registrado aún)"
import json, sys, os
IN = OUT_ = CACHED = CALLS = 0
if os.path.exists(sys.argv[1]):
    for line in open(sys.argv[1]):
        try:
            d = json.loads(line)
            if d.get("ts", "")[:10] == __import__("datetime").datetime.utcnow().date().isoformat():
                CALLS += 1; IN += d.get("in", 0); OUT_ += d.get("out", 0); CACHED += d.get("cached", 0)
        except Exception:
            pass
rin  = float(os.environ.get("GEMINI_USD_PER_MTOK_IN",  "0.15"))
rout = float(os.environ.get("GEMINI_USD_PER_MTOK_OUT", "0.60"))
cost = IN/1e6*rin + OUT_/1e6*rout
print(f"{CALLS} llamadas · in {IN:,} tok · out {OUT_:,} tok · cached {CACHED:,} · costo estimado ${cost:.3f} (rates {rin}/{rout} USD/Mtok)")
PY
} > "$OUT" 2>&1

# DM al supervisor + copia de auditoría
SUP=$(grep -aoE 'SUPERVISOR_NPUB_HEX=[0-9a-f]+' /srv/cayce-nostr/.env 2>/dev/null | cut -d= -f2)
BODY=$(head -c 6000 "$OUT")
if [ -n "$SUP" ]; then
  curl -s -m 20 -X POST http://127.0.0.1:3011/dm -H "Content-Type: application/json" \
    -d "$(python3 -c "import json,sys; print(json.dumps({'to_hex': '$SUP', 'content': open('$OUT').read()[:6000]}))")" \
    >/dev/null && logger -t cayce-daily-report "DM enviado ($(wc -c < "$OUT") bytes)"
else
  logger -t cayce-daily-report "SIN supervisor — reporte solo en log"
fi
cat "$OUT"
cp "$OUT" /var/log/cayce-daily-report.log 2>/dev/null || true
rm -f "$OUT"
