#!/usr/bin/env python3
"""Download cover images from Notion entries and update taste JSON."""

import json, urllib.request, os, hashlib, time, ssl
from pathlib import Path

NOTION_KEY = open("/home/clawd/secrets/notion/api_key").read().strip()
DB_ID = "2ff330c2-8646-81f0-bbd9-ec474393d7a5"
IMG_DIR = Path("/root/.openclaw/workspace/public/taste/screenshots")
JSON_PATH = Path("/root/.openclaw/workspace/public/taste/mymind-entries.json")

headers = {
    "Authorization": f"Bearer {NOTION_KEY}",
    "Notion-Version": "2022-06-28",
    "Content-Type": "application/json"
}

# Create SSL context that doesn't verify (for LinkedIn CDN etc)
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE

# Query all Notion entries
all_results = []
has_more = True
start_cursor = None
while has_more:
    body = {"page_size": 100}
    if start_cursor:
        body["start_cursor"] = start_cursor
    req = urllib.request.Request(
        f"https://api.notion.com/v1/databases/{DB_ID}/query",
        data=json.dumps(body).encode(), headers=headers, method="POST"
    )
    resp = urllib.request.urlopen(req)
    data = json.loads(resp.read())
    all_results.extend(data.get("results", []))
    has_more = data.get("has_more", False)
    start_cursor = data.get("next_cursor")

# Build map: URL -> cover image URL
url_to_cover = {}
nourl_covers = []
for r in all_results:
    cover = r.get("cover")
    if not cover:
        continue
    
    ctype = cover.get("type")
    if ctype == "external":
        cover_url = cover["external"]["url"]
    elif ctype == "file":
        cover_url = cover["file"]["url"]
    else:
        continue
    
    props = r["properties"]
    link = props.get("Link", {}).get("url", "")
    name_arr = props.get("Name", {}).get("title", [])
    name = name_arr[0]["plain_text"] if name_arr else ""
    tags_arr = props.get("Tags", {}).get("multi_select", [])
    tags = [t["name"] for t in tags_arr]
    
    if link:
        url_to_cover[link.strip().rstrip("/")] = cover_url
    else:
        # No URL entry — add directly with cover as the image
        nourl_covers.append({
            "notion_id": r["id"][:20],
            "title": name,
            "cover_url": cover_url,
            "tags": tags,
            "created": r.get("created_time", "")
        })

print(f"Entries with URL + cover: {len(url_to_cover)}")
print(f"Entries without URL but with cover: {len(nourl_covers)}")

# Load existing JSON
with open(JSON_PATH) as f:
    entries = json.load(f)

# Update existing entries that have matching URLs but no image
updated = 0
for e in entries:
    if e.get("image"):
        continue  # already has image
    url = (e.get("url") or "").strip().rstrip("/")
    if url and url in url_to_cover:
        cover_url = url_to_cover[url]
        # Download cover
        fname = hashlib.md5(cover_url.encode()).hexdigest()[:12] + ".jpg"
        fpath = IMG_DIR / fname
        if not fpath.exists():
            try:
                req = urllib.request.Request(cover_url, headers={"User-Agent": "Mozilla/5.0"})
                resp = urllib.request.urlopen(req, context=ctx, timeout=10)
                with open(fpath, "wb") as f:
                    f.write(resp.read())
            except Exception as ex:
                continue
        if fpath.exists() and fpath.stat().st_size > 1000:
            e["image"] = f"screenshots/{fname}"
            updated += 1

# Add no-URL entries with their cover images
existing_titles = set(e.get("title", "") for e in entries)
added = 0
for nc in nourl_covers:
    if nc["title"] in existing_titles:
        continue
    fname = hashlib.md5(nc["cover_url"].encode()).hexdigest()[:12] + ".jpg"
    fpath = IMG_DIR / fname
    if not fpath.exists():
        try:
            req = urllib.request.Request(nc["cover_url"], headers={"User-Agent": "Mozilla/5.0"})
            resp = urllib.request.urlopen(req, context=ctx, timeout=10)
            with open(fpath, "wb") as f:
                f.write(resp.read())
        except:
            continue
    if fpath.exists() and fpath.stat().st_size > 1000:
        entries.append({
            "id": nc["notion_id"],
            "title": nc["title"],
            "url": "",
            "desc": "",
            "note": "",
            "tags": nc["tags"],
            "type": "Image",
            "created": nc["created"],
            "image": f"screenshots/{fname}",
            "source": "notion"
        })
        added += 1

with open(JSON_PATH, "w") as f:
    json.dump(entries, f, indent=2, ensure_ascii=False)

print(f"Updated {updated} existing entries with Notion covers")
print(f"Added {added} new image-only entries")
print(f"Total entries now: {len(entries)}")
