#!/usr/bin/env python3
"""
Extract taste DNA from Inspiration Library.
Downloads cover images and analyzes them with vision model.
Outputs structured taste profile.
"""

import json
import os
import sys
import requests
import base64
from pathlib import Path

NOTION_KEY = open("/home/clawd/secrets/notion/api_key").read().strip()
DB_ID = "2ff330c2-8646-81f0-bbd9-ec474393d7a5"
OUTPUT_DIR = Path("/home/clawd/workspace/public/visual-research/taste-dna")
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)

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

def fetch_all_entries():
    """Fetch all entries from Inspiration Library."""
    url = f"https://api.notion.com/v1/databases/{DB_ID}/query"
    all_results = []
    has_more = True
    start_cursor = None
    
    while has_more:
        payload = {"page_size": 100}
        if start_cursor:
            payload["start_cursor"] = start_cursor
        
        resp = requests.post(url, headers=headers, json=payload)
        data = resp.json()
        all_results.extend(data.get("results", []))
        has_more = data.get("has_more", False)
        start_cursor = data.get("next_cursor")
    
    return all_results

def extract_entry_info(item):
    """Extract key info from a Notion page."""
    props = item.get("properties", {})
    
    # Title
    title = "Untitled"
    if "Name" in props and props["Name"]["title"]:
        title = props["Name"]["title"][0]["plain_text"]
    
    # URL
    url = props.get("URL", {}).get("url", "")
    
    # Tags
    tags = []
    if "Tags" in props and props["Tags"]["type"] == "multi_select":
        tags = [t["name"] for t in props["Tags"]["multi_select"]]
    
    # Cover image
    cover_url = None
    if item.get("cover"):
        cover = item["cover"]
        if cover["type"] == "external":
            cover_url = cover["external"]["url"]
        elif cover["type"] == "file":
            cover_url = cover["file"]["url"]
    
    return {
        "id": item["id"],
        "title": title,
        "url": url,
        "tags": tags,
        "cover_url": cover_url
    }

def download_image(url, filename):
    """Download image from URL."""
    try:
        resp = requests.get(url, timeout=30)
        if resp.status_code == 200:
            filepath = OUTPUT_DIR / filename
            with open(filepath, "wb") as f:
                f.write(resp.content)
            return filepath
    except Exception as e:
        print(f"  Error downloading: {e}")
    return None

def main():
    print("Fetching Inspiration Library entries...")
    entries = fetch_all_entries()
    print(f"Found {len(entries)} total entries")
    
    # Filter to entries with covers
    with_covers = []
    for item in entries:
        info = extract_entry_info(item)
        if info["cover_url"]:
            with_covers.append(info)
    
    print(f"\n{len(with_covers)} entries have cover images")
    
    # Output summary
    summary_path = OUTPUT_DIR / "entries-with-covers.json"
    with open(summary_path, "w") as f:
        json.dump(with_covers, f, indent=2)
    print(f"\nSaved entry list to: {summary_path}")
    
    # Download covers (first 20 for now)
    print("\nDownloading cover images...")
    downloaded = []
    for i, entry in enumerate(with_covers[:40]):
        safe_title = "".join(c if c.isalnum() else "_" for c in entry["title"][:40])
        ext = "jpg"  # Default
        if entry["cover_url"]:
            if ".png" in entry["cover_url"].lower():
                ext = "png"
            elif ".webp" in entry["cover_url"].lower():
                ext = "webp"
        
        filename = f"{i:02d}_{safe_title}.{ext}"
        print(f"  [{i+1}/{min(40, len(with_covers))}] {entry['title'][:50]}...")
        
        filepath = download_image(entry["cover_url"], filename)
        if filepath:
            downloaded.append({
                "entry": entry,
                "local_path": str(filepath)
            })
    
    print(f"\nDownloaded {len(downloaded)} images to {OUTPUT_DIR}")
    
    # Save download manifest
    manifest_path = OUTPUT_DIR / "download-manifest.json"
    with open(manifest_path, "w") as f:
        json.dump(downloaded, f, indent=2)
    print(f"Saved manifest to: {manifest_path}")
    
    # Print tags summary
    all_tags = {}
    for entry in with_covers:
        for tag in entry["tags"]:
            all_tags[tag] = all_tags.get(tag, 0) + 1
    
    print("\n" + "="*60)
    print("TOP TAGS (entries with covers):")
    print("="*60)
    for tag, count in sorted(all_tags.items(), key=lambda x: -x[1])[:25]:
        print(f"  {tag}: {count}")
    
    return downloaded

if __name__ == "__main__":
    main()
