#!/usr/bin/env bash
#
# 99-finalize-resume.sh — Resume pipeline from Phase 6 (deep enrichment) + Phase 9 (lint).
# Used after killing the over-broad shallow enrichment.
#
# Phases 3-5, 7, 8 already completed in previous run.

set -uo pipefail

ROOT="/Users/guto/ufo"
LOG="/tmp/ufo-finalize.log"
PY="python3"

cd "$ROOT" || exit 1

phase() {
  local name="$1"; shift
  echo "" | tee -a "$LOG"
  echo "================================================================" | tee -a "$LOG"
  echo "=== $(date -u +%Y-%m-%dT%H:%M:%SZ) — $name" | tee -a "$LOG"
  echo "================================================================" | tee -a "$LOG"
  "$@" 2>&1 | tee -a "$LOG"
  local rc=${PIPESTATUS[0]}
  echo "=== rc=$rc" | tee -a "$LOG"
  return $rc
}

echo "" >> "$LOG"
echo "================================================================" >> "$LOG"
echo "==== RESUME PIPELINE (deep enrichment + lint) $(date -u +%Y-%m-%dT%H:%M:%SZ) ====" >> "$LOG"
echo "================================================================" >> "$LOG"

# --- Phase 6 (deep tier only) — 1,107 entities, ~3h, ~$55 ---
phase "Phase 6 — enrichment (deep tier only, 1107 entities, 3 workers)" \
  $PY scripts/17-enrich-entities.py --all --tier deep --workers 3 || true

# --- Phase 9 — Lint (rebuilds mentioned_in[]) ---
phase "Phase 9 — lint + backlink rebuild" \
  $PY scripts/04-lint.py || true

echo "" | tee -a "$LOG"
echo "================================================================" | tee -a "$LOG"
echo "==== RESUME PIPELINE FINISHED $(date -u +%Y-%m-%dT%H:%M:%SZ) ====" | tee -a "$LOG"
echo "================================================================" | tee -a "$LOG"

PAGES=$(find "$ROOT/wiki/pages" -name "p*.md" 2>/dev/null | wc -l | tr -d ' ')
ENTITIES=$(find "$ROOT/wiki/entities" -name "*.md" 2>/dev/null | wc -l | tr -d ' ')
ENRICHED_DEEP=$(grep -l "enrichment_status: deep" "$ROOT/wiki/entities/"*/*.md 2>/dev/null | wc -l | tr -d ' ')

echo "pages: $PAGES · entities: $ENTITIES · enriched (deep): $ENRICHED_DEEP" | tee -a "$LOG"
