#!/bin/bash
# cf-mouser-backfill.sh <MPN>
# Universal fallback: queries Mouser API for the part, finds the canonical product URL
# (or any variant with a datasheet_url), navigates pup to the Mouser product page,
# scrapes ALL PDF links (Mouser hosts vendor docs on its own CDN — bypasses Cloudflare),
# and curls each into library/<MPN>/.
#
# Mouser CDN PDFs are at https://www.mouser.com/datasheet/<…>.pdf and serve plainly.

set -u
MPN="${1:?usage: cf-mouser-backfill.sh <MPN>}"
LIB_DIR="/home/adom/project/chip-fetcher/library/$MPN"
mkdir -p "$LIB_DIR"

MOUSER_API="${MOUSER_API:-https://mouser-xr8t4f5d01bt.adom.cloud}"

echo "=== Mouser backfill: $MPN ==="

# Find a Mouser product URL for this MPN (prefer the variant with a datasheet_url)
PRODUCT_URL=$(adom-mouser search "$MPN" --api "$MOUSER_API" 2>/dev/null \
  | python3 -c "
import sys, json
raw = sys.stdin.read()
try:
    js = raw[raw.index('{'):]
    d = json.loads(js)
except Exception:
    print(''); sys.exit(0)
comps = d.get('components', [])
# Prefer one with a datasheet_url
with_ds = [c for c in comps if c.get('datasheet_url')]
if with_ds:
    print(with_ds[0].get('product_url',''))
elif comps:
    print(comps[0].get('product_url',''))
")

if [ -z "$PRODUCT_URL" ]; then
  echo "  SKIP: no Mouser hit for $MPN"
  exit 0
fi
echo "  product: $PRODUCT_URL"

# Navigate pup to the Mouser product page
adom-desktop browser_navigate "{\"sessionId\":\"chip-fetcher\",\"url\":\"$PRODUCT_URL\"}" >/dev/null 2>&1
sleep 6

# Scrape PDF links — Mouser hosts datasheets at /datasheet/N/... and many vendor docs are linked from the page
PDF_JSON=$(adom-desktop browser_eval '{"sessionId":"chip-fetcher","expr":"(()=>{const links=Array.from(document.querySelectorAll(\"a\")).filter(a=>/\\.pdf(\\?|$|#)/i.test(a.href||\"\"));const seen=new Set();const out=[];for(const a of links){const h=a.href;if(seen.has(h))continue;seen.add(h);const rowText=(a.closest(\"tr,li,article,section,div\")?.textContent||a.textContent||\"\").trim().replace(/\\s+/g,\" \").slice(0,80);out.push(h+\"|\"+rowText);}return JSON.stringify(out);})()"}' 2>/dev/null \
  | python3 -c "import sys,json; print(json.load(sys.stdin).get('result',''))" 2>/dev/null)

if [ -z "$PDF_JSON" ] || [ "$PDF_JSON" = "[]" ]; then
  echo "  SKIP: no PDF links on Mouser page"
  exit 0
fi

echo "$PDF_JSON" | python3 -c "
import sys, json
items = json.load(sys.stdin)
for entry in items:
    parts = entry.split('|', 1)
    if len(parts) == 2:
        print(parts[0] + '\t' + parts[1])
    else:
        print(parts[0] + '\t')
" > /tmp/cf-mouser-pdfs.txt

DOC_COUNT=$(wc -l < /tmp/cf-mouser-pdfs.txt)
echo "  found $DOC_COUNT unique PDF links"

COUNT=0
SKIPPED=0
FAILED=0
while IFS=$'\t' read -r PDF_URL TITLE; do
  [ -z "$PDF_URL" ] && continue

  if [ -n "$TITLE" ]; then
    SLUG=$(echo "$TITLE" | sed -E 's/[^a-zA-Z0-9-]+/-/g; s/^-+|-+$//g; s/--+/-/g' | cut -c1-40 | tr 'A-Z' 'a-z')
  fi
  if [ -z "${SLUG:-}" ]; then
    SLUG=$(basename "$PDF_URL" .pdf | tr 'A-Z' 'a-z' | cut -c1-40)
  fi
  OUT="$LIB_DIR/$MPN-$SLUG.pdf"
  if [ -f "$OUT" ]; then
    SKIPPED=$((SKIPPED + 1)); continue
  fi

  HEAD=$(curl -sLI -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/130.0" "$PDF_URL" 2>/dev/null)
  CT=$(echo "$HEAD" | grep -i "^content-type:" | tail -1 | tr -d '\r' | awk '{print $2}')
  CL=$(echo "$HEAD" | grep -i "^content-length:" | tail -1 | tr -d '\r' | awk '{print $2}')

  if echo "$CT" | grep -qi "pdf"; then
    if [ -n "$CL" ] && [ "$CL" -gt 26214400 ]; then
      echo "  skip large: $SLUG"; SKIPPED=$((SKIPPED + 1)); continue
    fi
    if curl -sL -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/130.0" -o "$OUT" "$PDF_URL" 2>/dev/null; then
      SIZE=$(stat -c%s "$OUT" 2>/dev/null || echo 0)
      if [ "$SIZE" -gt 1000 ] && head -c 4 "$OUT" | grep -q "%PDF"; then
        echo "  + $MPN-$SLUG.pdf ($((SIZE/1024))KB)"
        COUNT=$((COUNT + 1))
      else
        rm -f "$OUT"; FAILED=$((FAILED + 1))
      fi
    else
      FAILED=$((FAILED + 1))
    fi
  else
    SKIPPED=$((SKIPPED + 1))
  fi
done < /tmp/cf-mouser-pdfs.txt

echo "  pulled=$COUNT skipped=$SKIPPED failed=$FAILED"
