#!/usr/bin/env python3
"""
Migrate MyMind entries to taste board candidates.
- Screenshots each URL
- Auto-tags with vision if missing tags
- Adds to state.json as candidates
"""

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

MYMIND_FILE = Path("/home/clawd/workspace/public/taste/mymind-entries.json")
STATE_FILE = Path("/home/clawd/workspace/public/taste/state.json")
IMAGES_DIR = Path("/home/clawd/workspace/public/visual-research/taste-dna")
TASTE_HTML = Path("/home/clawd/workspace/public/taste/index.html")

# Vision API for auto-tagging
GEMINI_KEY = "AIzaSyDXqYZInk83iVV4mD29pSuHQKbgkiI1x9Q"

VALID_TAGS = ["color", "type", "illustration", "photo", "brand", "3d", "motion", 
              "website", "minimal", "bold", "editorial", "food", "product", 
              "texture", "geometric", "layout"]

def screenshot_url(url, filename):
    """Take screenshot using Playwright"""
    filepath = IMAGES_DIR / filename
    if filepath.exists():
        print(f"  Screenshot exists: {filename}")
        return filename
    
    try:
        result = subprocess.run(
            ["playwright", "screenshot", "--browser", "chromium", 
             "--viewport-size=1200,900", "--timeout=20000", url, str(filepath)],
            capture_output=True,
            text=True,
            timeout=30
        )
        if filepath.exists() and filepath.stat().st_size > 1000:
            print(f"  ✓ Screenshot: {filename}")
            return filename
        else:
            print(f"  ✗ Screenshot failed: {url}")
            return None
    except Exception as e:
        print(f"  ✗ Screenshot error: {e}")
        return None

def auto_tag_image(filepath):
    """Use Gemini vision to auto-tag an image"""
    try:
        with open(filepath, "rb") as f:
            img_data = base64.b64encode(f.read()).decode()
        
        prompt = f"""Analyze this design/visual reference image and return relevant tags.
        
Choose ONLY from these tags: {', '.join(VALID_TAGS)}

Return a JSON array of 2-5 relevant tags. Example: ["brand", "minimal", "type"]

Only return the JSON array, nothing else."""
        
        resp = requests.post(
            f"https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key={GEMINI_KEY}",
            headers={"Content-Type": "application/json"},
            json={
                "contents": [{
                    "parts": [
                        {"text": prompt},
                        {"inline_data": {"mime_type": "image/png", "data": img_data}}
                    ]
                }]
            },
            timeout=30
        )
        
        if resp.status_code == 200:
            text = resp.json()["candidates"][0]["content"]["parts"][0]["text"]
            # Extract JSON array
            text = text.strip()
            if text.startswith("```"):
                text = text.split("\n", 1)[1].rsplit("```", 1)[0]
            tags = json.loads(text)
            # Filter to valid tags
            tags = [t.lower() for t in tags if t.lower() in VALID_TAGS]
            return tags[:5]
    except Exception as e:
        print(f"    Auto-tag error: {e}")
    return []

def load_state():
    if STATE_FILE.exists():
        return json.loads(STATE_FILE.read_text())
    return {"items": {}}

def save_state(state):
    STATE_FILE.write_text(json.dumps(state, indent=2))

def migrate_entries(limit=None, skip_screenshots=False):
    """Migrate MyMind entries to candidates"""
    
    # Load MyMind entries
    mymind = json.loads(MYMIND_FILE.read_text())
    print(f"Loaded {len(mymind)} MyMind entries")
    
    # Load current state
    state = load_state()
    existing_urls = {item.get("url") for item in state.get("items", {}).values()}
    
    # Filter to visual entries (skip LinkedIn, YouTube, etc)
    skip_domains = ["linkedin.com", "youtube.com", "spotify.com", "twitter.com", "x.com"]
    visual_entries = [
        e for e in mymind 
        if e.get("url") and not any(d in e["url"] for d in skip_domains)
    ]
    print(f"Visual entries to process: {len(visual_entries)}")
    
    if limit:
        visual_entries = visual_entries[:limit]
        print(f"Processing first {limit}")
    
    migrated = 0
    failed = 0
    skipped = 0
    
    for i, entry in enumerate(visual_entries):
        url = entry["url"]
        title = entry.get("title", "Untitled")
        existing_tags = entry.get("tags", [])
        
        print(f"\n[{i+1}/{len(visual_entries)}] {title[:50]}...")
        
        # Skip if already in candidates
        if url in existing_urls:
            print(f"  Already exists, skipping")
            skipped += 1
            continue
        
        # Generate filename from URL
        safe_name = url.replace("https://", "").replace("http://", "")
        safe_name = "".join(c if c.isalnum() else "_" for c in safe_name)[:60]
        filename = f"mymind-{safe_name}.png"
        
        # Screenshot
        if not skip_screenshots:
            img_file = screenshot_url(url, filename)
            if not img_file:
                failed += 1
                continue
        else:
            img_file = filename
            if not (IMAGES_DIR / img_file).exists():
                print(f"  No screenshot, skipping")
                failed += 1
                continue
        
        # Auto-tag if no tags
        tags = [t.lower() for t in existing_tags if t.lower() in VALID_TAGS]
        if not tags and (IMAGES_DIR / img_file).exists():
            print(f"  Auto-tagging...")
            tags = auto_tag_image(IMAGES_DIR / img_file)
            print(f"    Tags: {tags}")
        
        # Create candidate entry
        candidate_id = f"mymind-{i+1}"
        state["items"][candidate_id] = {
            "id": candidate_id,
            "url": url,
            "title": title,
            "image": f"/visual-research/taste-dna/{img_file}",
            "tags": tags,
            "status": "candidate",
            "source": "mymind",
            "imported": entry.get("created", "")
        }
        
        existing_urls.add(url)
        migrated += 1
        
        # Save periodically
        if migrated % 10 == 0:
            save_state(state)
            print(f"  [Saved {migrated} so far]")
        
        # Rate limit
        time.sleep(0.5)
    
    # Final save
    save_state(state)
    
    print(f"\n=== Migration Complete ===")
    print(f"Migrated: {migrated}")
    print(f"Failed: {failed}")
    print(f"Skipped (existing): {skipped}")
    
    return migrated

if __name__ == "__main__":
    limit = int(sys.argv[1]) if len(sys.argv) > 1 else None
    skip_ss = "--skip-screenshots" in sys.argv
    migrate_entries(limit=limit, skip_screenshots=skip_ss)
