#!/usr/bin/env python3
"""
Instagram DM Quick Analysis - Local analysis without API
Analyzes conversations based on metadata patterns.
"""

import json
import re
from pathlib import Path
from datetime import datetime

DM_FILE = Path.home() / "Documents" / "instagram_dm_analysis.json"
OUTPUT_FILE = Path.home() / "Documents" / "instagram_dm_leads_quick.json"

# Lead indicators in preview text (Portuguese/English)
HIGH_INTENT_PATTERNS = [
    r'quanto\s*custa', r'preco', r'preço', r'valor', r'orcamento', r'orçamento',
    r'contratar', r'serviço', r'trabalho', r'projeto', r'freelance',
    r'how\s*much', r'price', r'quote', r'budget', r'hire', r'work\s*with',
    r'parceria', r'partnership', r'collab', r'colaboracao', r'colaboração',
    r'interessado', r'interested', r'quero', r'preciso', r'need'
]

MEDIUM_INTENT_PATTERNS = [
    r'testflight', r'app', r'software', r'dev', r'codigo', r'código',
    r'instagram', r'automacao', r'automação', r'bot', r'api',
    r'perfeito', r'incrivel', r'incrível', r'amazing', r'great', r'awesome',
    r'\?$'  # Questions often indicate interest
]

PERSONAL_PATTERNS = [
    r'kkk+', r'haha', r'rsrs', r'lol', r'😂', r'❤️', r'👍',
    r'bom\s*dia', r'boa\s*noite', r'ola', r'olá', r'oi', r'hey', r'hi',
    r'tudo\s*bem', r'e\s*ai', r'fala'
]

SPAM_PATTERNS = [
    r'ganhe', r'gratis', r'grátis', r'free\s*money', r'click\s*here',
    r'promo', r'desconto\s*exclusivo', r'dm\s*me'
]

def analyze_preview(preview: str, is_verified: bool, time_ago: str) -> dict:
    """Analyze a conversation preview and return lead score."""
    preview_lower = preview.lower()

    # Base score
    score = 30
    intent = "personal"
    urgency = "low"

    # Check patterns
    for pattern in HIGH_INTENT_PATTERNS:
        if re.search(pattern, preview_lower):
            score += 30
            intent = "lead"
            break

    for pattern in MEDIUM_INTENT_PATTERNS:
        if re.search(pattern, preview_lower):
            score += 15
            if intent == "personal":
                intent = "business"
            break

    for pattern in SPAM_PATTERNS:
        if re.search(pattern, preview_lower):
            score = max(5, score - 40)
            intent = "spam"
            break

    # Verified account bonus
    if is_verified:
        score += 15

    # Recency bonus
    if 'h' in time_ago or 'm' in time_ago:  # Hours or minutes
        score += 10
        urgency = "medium"
    elif '1d' in time_ago or '2d' in time_ago:
        score += 5

    # If they sent me something (not "You:")
    if preview.startswith("You:") or preview.startswith("You sent"):
        score -= 10  # I was last to respond
    else:
        score += 5  # They were last to respond
        if score >= 60:
            urgency = "high"

    # Cap score
    score = min(100, max(0, score))

    # Determine sentiment from emojis/patterns
    sentiment = "neutral"
    if any(e in preview for e in ['❤️', '😊', '👍', '🔥', 'incrível', 'perfeito', 'amazing']):
        sentiment = "positive"
    elif any(e in preview for e in ['😂', 'kkk', 'haha']):
        sentiment = "positive"
    elif any(e in preview for e in ['😢', '😔', 'triste', 'ruim']):
        sentiment = "negative"

    return {
        "leadScore": score,
        "intent": intent,
        "sentiment": sentiment,
        "urgency": urgency,
        "priority": "high" if score >= 60 else ("medium" if score >= 40 else "low")
    }

def main():
    print("=" * 60)
    print("Instagram DM Quick Analysis (Local)")
    print("=" * 60)

    # Load conversations
    if not DM_FILE.exists():
        print(f"ERRO: Arquivo não encontrado: {DM_FILE}")
        return

    with open(DM_FILE) as f:
        data = json.load(f)

    conversations = data.get("conversations", [])
    print(f"Conversas carregadas: {len(conversations)}")

    # Analyze each conversation
    for conv in conversations:
        preview = conv.get("lastMessagePreview", "")
        is_verified = conv.get("isVerified", False)
        time_ago = conv.get("lastMessageTime", "")

        analysis = analyze_preview(preview, is_verified, time_ago)
        conv["analysis"] = analysis

    # Sort by lead score
    conversations.sort(key=lambda c: c.get("analysis", {}).get("leadScore", 0), reverse=True)

    # Stats
    verified_count = sum(1 for c in conversations if c.get("isVerified"))
    high_leads = [c for c in conversations if c.get("analysis", {}).get("leadScore", 0) >= 60]
    medium_leads = [c for c in conversations if 40 <= c.get("analysis", {}).get("leadScore", 0) < 60]

    print(f"\nContas verificadas: {verified_count}")
    print(f"Leads quentes (>=60): {len(high_leads)}")
    print(f"Leads mornos (40-59): {len(medium_leads)}")

    # Display results
    print("\n" + "=" * 60)
    print("TOP LEADS - PRIORIDADE ALTA")
    print("=" * 60)

    for i, c in enumerate(high_leads[:20], 1):
        a = c.get("analysis", {})
        verified = " ✓" if c.get("isVerified") else ""
        urgency_icon = "🔴" if a.get("urgency") == "high" else ("🟡" if a.get("urgency") == "medium" else "⚪")
        print(f"\n{i}. [{a.get('leadScore'):3d}] @{c['participantUsername']}{verified} {urgency_icon}")
        print(f"   Preview: {c.get('lastMessagePreview', '')[:60]}...")
        print(f"   Intent: {a.get('intent')} | Tempo: {c.get('lastMessageTime')}")

    print("\n" + "=" * 60)
    print("LEADS MORNOS - POTENCIAL")
    print("=" * 60)

    for i, c in enumerate(medium_leads[:15], 1):
        a = c.get("analysis", {})
        verified = " ✓" if c.get("isVerified") else ""
        print(f"{i}. [{a.get('leadScore'):3d}] @{c['participantUsername']}{verified} - {c.get('lastMessagePreview', '')[:40]}...")

    print("\n" + "=" * 60)
    print("CONTAS VERIFICADAS (Maior Influência)")
    print("=" * 60)

    verified_convs = [c for c in conversations if c.get("isVerified")]
    for c in verified_convs:
        a = c.get("analysis", {})
        print(f"  [{a.get('leadScore'):3d}] @{c['participantUsername']} ✓ - {c.get('lastMessagePreview', '')[:40]}...")

    # Save results
    output = {
        "analyzedAt": datetime.now().isoformat(),
        "analysisType": "local_heuristic",
        "totalConversations": len(conversations),
        "highLeads": len(high_leads),
        "mediumLeads": len(medium_leads),
        "verifiedAccounts": verified_count,
        "conversations": conversations
    }

    with open(OUTPUT_FILE, "w") as f:
        json.dump(output, f, indent=2, ensure_ascii=False)
    print(f"\nResultados salvos: {OUTPUT_FILE}")

    # CSV export
    csv_file = Path.home() / "Documents" / "instagram_dm_leads.csv"
    with open(csv_file, "w") as f:
        f.write("rank,username,verified,lead_score,intent,urgency,priority,last_message,time\n")
        for i, c in enumerate(conversations, 1):
            a = c.get("analysis", {})
            preview = c.get("lastMessagePreview", "").replace('"', '""')
            row = [
                str(i),
                c.get("participantUsername", ""),
                "yes" if c.get("isVerified") else "no",
                str(a.get("leadScore", 0)),
                a.get("intent", ""),
                a.get("urgency", ""),
                a.get("priority", ""),
                f'"{preview}"',
                c.get("lastMessageTime", "")
            ]
            f.write(",".join(row) + "\n")
    print(f"CSV exportado: {csv_file}")

    print("\n" + "=" * 60)
    print("RESUMO FINAL")
    print("=" * 60)
    print(f"Total de conversas: {len(conversations)}")
    print(f"Leads quentes (>=60): {len(high_leads)}")
    print(f"Leads mornos (40-59): {len(medium_leads)}")
    print(f"Contas verificadas: {verified_count}")
    print(f"\nArquivos gerados:")
    print(f"  - {OUTPUT_FILE}")
    print(f"  - {csv_file}")

if __name__ == "__main__":
    main()
