#!/bin/bash
# cf-mouser-pdf-pull.sh <MPN>
# Use pup's in-page fetch() to download Mouser-hosted PDFs (Akamai blocks naked curl).
# Walks each variant returned by the Mouser API; for each datasheet_url, fetches
# binary via browser_eval and decodes base64 into the library.
set -u
MPN="${1:?usage: cf-mouser-pdf-pull.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-PDF-pull: $MPN ==="

# Get all unique PDF URLs from Mouser API for this MPN's variants
URLS=$(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:
    sys.exit(0)
seen=set()
for c in d.get('components', []):
    u=c.get('datasheet_url')
    if u and '.pdf' in u and u not in seen:
        seen.add(u); print(u)
")

if [ -z "$URLS" ]; then
  echo "  no datasheet URLs from API"
  # Fallback: also navigate to product page + scrape
  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:
    sys.exit(0)
comps=d.get('components',[])
print(comps[0].get('product_url','') if comps else '')")
  if [ -z "$PRODUCT_URL" ]; then echo "  SKIP: no Mouser hit"; exit 0; fi
  adom-desktop browser_navigate "{\"sessionId\":\"chip-fetcher\",\"url\":\"$PRODUCT_URL\"}" >/dev/null 2>&1
  sleep 6
  URLS=$(adom-desktop browser_eval '{"sessionId":"chip-fetcher","expr":"JSON.stringify(Array.from(new Set(Array.from(document.querySelectorAll(\"a\")).map(a=>a.href).filter(h=>/\\.pdf(\\?|$|#)/i.test(h)))))"}' 2>/dev/null \
    | python3 -c "import sys,json; r=json.load(sys.stdin).get('result',''); print('\n'.join(json.loads(r)) if r and r.startswith('[') else '')" 2>/dev/null)
fi

if [ -z "$URLS" ]; then
  echo "  SKIP: no PDFs found via API or page scrape"
  exit 0
fi

# First, navigate pup to mouser.com to establish Akamai session cookies
adom-desktop browser_navigate '{"sessionId":"chip-fetcher","url":"https://www.mouser.com/"}' >/dev/null 2>&1
sleep 3

COUNT=0
FAILED=0
echo "$URLS" | while read URL; do
  [ -z "$URL" ] && continue
  SLUG=$(basename "$URL" .pdf | tr 'A-Z' 'a-z' | sed -E 's/[^a-z0-9-]+/-/g; s/^-+|-+$//g; s/--+/-/g' | cut -c1-40)
  OUT="$LIB_DIR/$MPN-$SLUG.pdf"
  if [ -f "$OUT" ]; then echo "  exists: $SLUG"; continue; fi

  # Fetch PDF via in-page XHR, base64-encode, return through browser_eval
  EXPR=$(python3 -c "
import json
url='$URL'
print(json.dumps(f'(async()=>{{const r=await fetch({json.dumps(url)},{{credentials:\"include\"}});if(!r.ok)return\"FAIL_\"+r.status;const ab=await r.arrayBuffer();const b=new Uint8Array(ab);let s=\"\";for(let i=0;i<b.length;i++)s+=String.fromCharCode(b[i]);return\"OK_\"+btoa(s);}})()'))
")

  # Use browser_eval with awaitPromise:true if supported; else await in expr
  RESULT=$(adom-desktop browser_eval "{\"sessionId\":\"chip-fetcher\",\"expr\":$EXPR,\"awaitPromise\":true}" 2>/dev/null \
    | python3 -c "import sys,json; r=json.load(sys.stdin).get('result',''); print(r if isinstance(r,str) else '')" 2>/dev/null)

  if [[ "$RESULT" == OK_* ]]; then
    B64="${RESULT#OK_}"
    echo "$B64" | base64 -d > "$OUT" 2>/dev/null
    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"; echo "  fail decode: $SLUG (size=$SIZE)"; FAILED=$((FAILED + 1))
    fi
  else
    echo "  fail fetch: $SLUG ($RESULT)"; FAILED=$((FAILED + 1))
  fi
done

echo "  done: pulled=$COUNT failed=$FAILED"