#!/usr/bin/env python3

import asyncio
import json
import sys
import re
from datetime import datetime, timedelta
from playwright.async_api import async_playwright
import argparse

# Define all 5 design sources
SOURCES = [
    {
        'name': 'Brand New',
        'url': 'https://underconsideration.com/brandnew/',
        'selectors': {
            'articles': 'article',
            'title': 'h2 a, h1 a',
            'link': 'h2 a, h1 a',
            'date': 'time',
            'excerpt': 'p'
        },
        'method': 'playwright'  # Use Playwright for consistency
    },
    {
        'name': 'Creative Boom',
        'url': 'https://creativeboom.com/inspiration/',
        'selectors': {
            'articles': 'article.post, .post-item',
            'title': 'h2 a, .entry-title a, h3 a',
            'link': 'h2 a, .entry-title a, h3 a',
            'date': 'time, .entry-date',
            'excerpt': '.entry-excerpt, .excerpt, .entry-content p'
        },
        'method': 'playwright'
    },
    {
        'name': "It's Nice That",
        'url': 'https://www.itsnicethat.com/',
        'selectors': {
            'articles': 'article, .featured-article, .post',
            'title': 'h1 a, h2 a, h3 a, .title a',
            'link': 'h1 a, h2 a, h3 a, .title a',
            'date': 'time, .date, .meta-date',
            'excerpt': '.excerpt, .description, .summary'
        },
        'method': 'playwright'
    },
    {
        'name': 'The Brand Identity',
        'url': 'https://the-brandidentity.com/',
        'selectors': {
            'articles': 'article, .post, .entry',
            'title': 'h2 a, .title a, h1 a',
            'link': 'h2 a, .title a, h1 a',
            'date': 'time, .date, .meta-date',
            'excerpt': '.excerpt, .summary, .entry-content p'
        },
        'method': 'playwright'
    },
    {
        'name': 'Behance',
        'url': 'https://www.behance.net/galleries/graphic-design',
        'selectors': {
            'articles': '.ProjectCover, .project-cover, [data-project-id]',
            'title': '.Title, .project-title, h2 a, .cover-title',
            'link': 'a.Cover, a.project-cover, .project-link',
            'date': '.PublishDate, .publish-date, time',
            'excerpt': '.ProjectDescription, .project-description, .excerpt'
        },
        'method': 'playwright'
    }
]

# Trend patterns for analysis
TREND_PATTERNS = {
    'visual-minimalism': {
        'keywords': ['minimalist', 'clean', 'simple', 'negative space', 'typography', 'sans serif', 'geometric'],
        'confidence_threshold': 3
    },
    'brand-authenticity': {
        'keywords': ['authentic', 'human', 'personal', 'story', 'craft', 'handmade', 'illustration'],
        'confidence_threshold': 3
    },
    'color-evolution': {
        'keywords': ['color', 'palette', 'gradient', 'vibrant', 'bold', 'neon', 'pastel', 'monochrome'],
        'confidence_threshold': 2
    },
    'digital-physical-blend': {
        'keywords': ['digital', 'physical', 'experiential', 'interactive', 'immersive', 'AR', 'VR'],
        'confidence_threshold': 3
    },
    'sustainability-design': {
        'keywords': ['sustainable', 'eco', 'green', 'circular', 'renewable', 'carbon', 'climate'],
        'confidence_threshold': 2
    }
}

async def scrape_source(browser, source, max_articles=8):
    """Scrape a single source using Playwright"""
    print(f"📡 Scanning {source['name']}...", file=sys.stderr)
    
    page = await browser.new_page()
    articles = []
    
    try:
        # Navigate to the source
        await page.goto(source['url'], wait_until='networkidle', timeout=15000)
        
        # Wait for content to load
        await page.wait_for_timeout(3000)
        
        # Find all article containers
        article_elements = await page.query_selector_all(source['selectors']['articles'])
        
        for i, article in enumerate(article_elements[:max_articles]):
            try:
                # Extract title and link
                title_element = await article.query_selector(source['selectors']['title'])
                if not title_element:
                    continue
                    
                title = await title_element.inner_text()
                href = await title_element.get_attribute('href')
                
                if not href:
                    # Try to find link in parent or article
                    link_element = await article.query_selector('a')
                    if link_element:
                        href = await link_element.get_attribute('href')
                
                # Normalize URL
                if href and not href.startswith('http'):
                    base_url = f"{source['url'].split('/')[0]}//{source['url'].split('/')[2]}"
                    href = base_url + (href if href.startswith('/') else '/' + href)
                
                # Extract date
                date = None
                try:
                    date_element = await article.query_selector(source['selectors']['date'])
                    if date_element:
                        date_text = await date_element.get_attribute('datetime') or await date_element.inner_text()
                        if date_text:
                            date = date_text.strip()
                except:
                    pass
                
                # Extract excerpt
                excerpt = ""
                try:
                    excerpt_element = await article.query_selector(source['selectors']['excerpt'])
                    if excerpt_element:
                        excerpt_text = await excerpt_element.inner_text()
                        if excerpt_text:
                            excerpt = excerpt_text.strip()[:300]
                except:
                    pass
                
                if title and href:
                    articles.append({
                        'source': source['name'],
                        'title': title.strip(),
                        'url': href,
                        'date': date,
                        'excerpt': excerpt,
                        'scraped_at': datetime.now().isoformat()
                    })
                    
            except Exception as e:
                print(f"⚠️ Error parsing article {i+1} from {source['name']}: {str(e)}", file=sys.stderr)
                continue
        
        print(f"✅ {source['name']}: Found {len(articles)} articles", file=sys.stderr)
        
    except Exception as e:
        print(f"❌ Error scraping {source['name']}: {str(e)}", file=sys.stderr)
    
    finally:
        await page.close()
    
    return articles

def analyze_trend_signals(articles):
    """Analyze articles for trend signals"""
    signals = []
    today = datetime.now().strftime('%Y-%m-%d')
    
    for article in articles:
        content = f"{article['title']} {article.get('excerpt', '')}".lower()
        
        # Check against trend patterns
        for pattern_name, pattern in TREND_PATTERNS.items():
            score = 0
            matched_keywords = []
            
            for keyword in pattern['keywords']:
                if keyword.lower() in content:
                    score += 1
                    matched_keywords.append(keyword)
            
            if score >= pattern['confidence_threshold']:
                confidence = 'signal' if score >= 4 else 'emerging' if score >= 2 else 'noise'
                
                signal = {
                    'title': article['title'],
                    'source': article['source'],
                    'url': article['url'],
                    'pattern': pattern_name,
                    'confidence': confidence,
                    'matched_keywords': matched_keywords,
                    'why_interesting': f"Shows {pattern_name.replace('-', ' ')} trend with {score} indicators: {', '.join(matched_keywords[:3])}",
                    'date': today,
                    'excerpt': article.get('excerpt', '')[:200]
                }
                signals.append(signal)
    
    # Sort by confidence and score
    confidence_order = {'signal': 3, 'emerging': 2, 'noise': 1}
    signals.sort(key=lambda x: (confidence_order.get(x['confidence'], 0), len(x['matched_keywords'])), reverse=True)
    
    return signals[:10]  # Return top 10 signals

async def run_radar_scan():
    """Main function to run the radar scan"""
    print("🎯 Starting design radar scan...", file=sys.stderr)
    print(f"📅 {datetime.now().strftime('%Y-%m-%d %H:%M UTC')}", file=sys.stderr)
    print("="*50, file=sys.stderr)
    
    all_articles = []
    failed_sources = []
    
    async with async_playwright() as p:
        browser = await p.chromium.launch(headless=True)
        
        try:
            # Test each source
            for source in SOURCES:
                try:
                    articles = await scrape_source(browser, source)
                    all_articles.extend(articles)
                    
                    if not articles:
                        failed_sources.append(source['name'])
                        
                except Exception as e:
                    print(f"❌ Failed to scrape {source['name']}: {str(e)}", file=sys.stderr)
                    failed_sources.append(source['name'])
        
        finally:
            await browser.close()
    
    print(f"\n📊 Scan Summary:", file=sys.stderr)
    print(f"   Total articles: {len(all_articles)}", file=sys.stderr)
    print(f"   Sources tested: {len(SOURCES)}", file=sys.stderr)
    print(f"   Failed sources: {len(failed_sources)}", file=sys.stderr)
    
    if failed_sources:
        print(f"   ⚠️ Failed: {', '.join(failed_sources)}", file=sys.stderr)
    
    # Analyze for trends
    signals = analyze_trend_signals(all_articles)
    print(f"   🎯 Trend signals: {len(signals)}", file=sys.stderr)
    
    # Prepare output
    output = {
        'scan_date': datetime.now().isoformat(),
        'sources_tested': len(SOURCES),
        'sources_successful': len(SOURCES) - len(failed_sources),
        'total_articles': len(all_articles),
        'trend_signals': signals,
        'raw_articles': all_articles[:20],  # Include sample of raw articles
        'infrastructure_status': 'operational' if len(failed_sources) < 3 else 'degraded'
    }
    
    return output

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Run design radar scan with fixed infrastructure')
    parser.add_argument('--output', '-o', help='Output file (default: stdout)')
    parser.add_argument('--verbose', '-v', action='store_true', help='Verbose output')
    
    args = parser.parse_args()
    
    # Run the scan
    try:
        output = asyncio.run(run_radar_scan())
        
        if args.output:
            with open(args.output, 'w') as f:
                json.dump(output, f, indent=2)
            print(f"✅ Results written to {args.output}", file=sys.stderr)
        else:
            print(json.dumps(output, indent=2))
            
    except KeyboardInterrupt:
        print("\n🛑 Scan interrupted by user", file=sys.stderr)
        sys.exit(1)
    except Exception as e:
        print(f"💥 Scan failed: {str(e)}", file=sys.stderr)
        sys.exit(1)