#!/usr/bin/env python3
"""
Import MyMind CSV export to taste board.
Parses CSV, takes screenshots for URLs, updates state.json and index.html
"""

import csv
import json
import os
import re
import subprocess
from pathlib import Path
from urllib.parse import urlparse
import time

# Config
CSV_PATH = '/home/clawd/.openclaw/media/inbound/6455ce86-18ca-4064-b8cd-e5283c00e932.csv'
STATE_FILE = '/home/clawd/workspace/public/taste/state.json'
INDEX_FILE = '/home/clawd/workspace/public/taste/index.html'
IMAGES_DIR = '/home/clawd/workspace/public/visual-research/mymind'

# Valid taste tags (map from MyMind to our system)
TAG_MAP = {
    'branding': 'brand',
    'typography': 'type',
    'photography': 'photo',
    'illustration': 'illustration',
    'colorful': 'color',
    '3d': '3d',
    'animation': 'motion',
    'web design': 'website',
    'minimalist': 'minimal',
    'editorial': 'editorial',
    'food': 'food',
    'packaging': 'product',
    'graphic design': 'brand',
    'visual identity': 'brand',
    'motion design': 'motion',
    'ui/ux': 'website',
    'layout': 'layout',
    'bold': 'bold',
    'geometric': 'geometric',
    'texture': 'texture',
}

VALID_TAGS = {'color', 'type', 'illustration', 'photo', 'brand', '3d', 'motion', 
              'website', 'minimal', 'bold', 'editorial', 'food', 'product', 
              'texture', 'geometric', 'layout'}


def ensure_dirs():
    Path(IMAGES_DIR).mkdir(parents=True, exist_ok=True)


def load_csv():
    """Load and parse MyMind CSV."""
    entries = []
    with open(CSV_PATH, 'r', encoding='utf-8') as f:
        reader = csv.DictReader(f)
        for row in reader:
            # Only include entries with URLs
            url = row.get('url', '').strip()
            if url and url.startswith('http'):
                # Skip non-visual URLs
                skip_domains = ['linkedin.com/posts', 'open.spotify', 'youtube.com', 'youtu.be']
                if any(d in url for d in skip_domains):
                    continue
                
                entries.append({
                    'mymind_id': row.get('id', ''),
                    'type': row.get('type', ''),
                    'title': row.get('title', '').strip() or urlparse(url).netloc,
                    'url': url,
                    'note': row.get('note', ''),
                    'tags': row.get('tags', ''),
                    'created': row.get('created', '')
                })
    return entries


def map_tags(mymind_tags: str) -> list:
    """Convert MyMind tags to our tag system."""
    if not mymind_tags:
        return []
    
    tags = [t.strip().lower() for t in mymind_tags.split(',')]
    mapped = set()
    
    for tag in tags:
        if tag in VALID_TAGS:
            mapped.add(tag)
        elif tag in TAG_MAP:
            mapped.add(TAG_MAP[tag])
        # Also check partial matches
        for mymind_tag, our_tag in TAG_MAP.items():
            if mymind_tag in tag:
                mapped.add(our_tag)
    
    return list(mapped)[:5]  # Limit to 5 tags


def take_screenshot(url: str, filename: str) -> bool:
    """Take screenshot using Playwright."""
    filepath = os.path.join(IMAGES_DIR, filename)
    if os.path.exists(filepath):
        return True
    
    try:
        result = subprocess.run(
            ['playwright', 'screenshot', '--browser', 'chromium', 
             '--viewport-size=1200,900', url, filepath],
            capture_output=True, timeout=30
        )
        return os.path.exists(filepath) and os.path.getsize(filepath) > 1000
    except Exception as e:
        print(f"  Screenshot failed: {e}")
        return False


def load_state():
    """Load current state.json."""
    if os.path.exists(STATE_FILE):
        with open(STATE_FILE) as f:
            return json.load(f)
    return {'items': {}, 'seenIds': []}


def save_state(state):
    """Save state.json."""
    with open(STATE_FILE, 'w') as f:
        json.dump(state, f, indent=2)


def get_existing_urls():
    """Get URLs already in the taste board."""
    with open(INDEX_FILE) as f:
        content = f.read()
    
    urls = set()
    # Find all URLs in CANDIDATES array
    for match in re.finditer(r"url:\s*['\"]([^'\"]+)['\"]", content):
        urls.add(match.group(1))
    
    return urls


def get_next_id():
    """Get the next candidate ID."""
    with open(INDEX_FILE) as f:
        content = f.read()
    
    # Find highest ID
    ids = [int(m) for m in re.findall(r'\{\s*id:\s*(\d+)', content)]
    return max(ids) + 1 if ids else 1


def main():
    print("🔄 Importing MyMind export to taste board...")
    ensure_dirs()
    
    # Load data
    entries = load_csv()
    print(f"📋 Loaded {len(entries)} visual entries from MyMind")
    
    existing_urls = get_existing_urls()
    print(f"📁 {len(existing_urls)} URLs already in taste board")
    
    state = load_state()
    next_id = get_next_id()
    
    # Filter to new entries only
    new_entries = [e for e in entries if e['url'] not in existing_urls]
    print(f"🆕 {len(new_entries)} new entries to add")
    
    # Generate new candidate entries
    new_candidates = []
    imported = 0
    failed = 0
    
    # Batch process (limit to avoid overwhelming)
    batch_size = 50  # Process in batches
    
    for i, entry in enumerate(new_entries[:batch_size]):
        title = entry['title'][:50]
        url = entry['url']
        mymind_id = entry['mymind_id']
        
        print(f"[{i+1}/{min(len(new_entries), batch_size)}] {title[:30]}...")
        
        # Generate filename
        safe_title = re.sub(r'[^\w\-]', '_', title)[:30]
        filename = f"mm_{mymind_id}_{safe_title}.png"
        
        # Take screenshot
        if take_screenshot(url, filename):
            # Map tags
            tags = map_tags(entry['tags'])
            
            # Create candidate entry
            candidate = {
                'id': next_id,
                'img': f'../visual-research/mymind/{filename}',
                'title': entry['title'][:60],
                'desc': entry['note'][:100] if entry['note'] else '',
                'url': url,
                'source': 'MyMind'
            }
            new_candidates.append(candidate)
            
            # Update state with tags
            state['items'][str(next_id)] = {
                'status': 'pending',  # Mark for curation
                'tags': tags
            }
            
            next_id += 1
            imported += 1
            print(f"  ✅ Added with tags: {tags}")
        else:
            failed += 1
            print(f"  ❌ Screenshot failed")
        
        # Small delay to avoid overwhelming
        time.sleep(0.5)
    
    # Save state
    save_state(state)
    
    # Output summary
    print(f"\n📊 Import Summary:")
    print(f"   Imported: {imported}")
    print(f"   Failed: {failed}")
    print(f"   Remaining: {len(new_entries) - batch_size if len(new_entries) > batch_size else 0}")
    
    # Generate JS for new candidates
    if new_candidates:
        print(f"\n📝 Generated {len(new_candidates)} new candidates")
        
        # Write candidates to a temp file for manual merge
        js_entries = []
        for c in new_candidates:
            js_entries.append(
                f"      {{ id: {c['id']}, img: '{c['img']}', title: '{c['title'].replace(chr(39), '')}', "
                f"desc: '{c['desc'].replace(chr(39), '')}', url: '{c['url']}', source: '{c['source']}' }}"
            )
        
        output_file = '/home/clawd/workspace/public/taste/new-candidates.js'
        with open(output_file, 'w') as f:
            f.write("// New candidates from MyMind import\n")
            f.write("// Add these to CANDIDATES array in index.html\n\n")
            f.write(",\n".join(js_entries))
        
        print(f"   Written to: {output_file}")
        print(f"   Run: cat {output_file} >> index.html (after CANDIDATES array)")
    
    return imported, failed


if __name__ == '__main__':
    main()
