"""Seed demo data directly into ChromaDB (bypasses Telegram for speed)."""
import asyncio, os, sys
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))

from backend.pipeline import process_message_batch, set_lens

# ====== ACME DEV TEAM ======
DEV_MESSAGES = [
    {"sender": "Alex", "text": "Team, I'm proposing we use PostgreSQL for the main database. Our data is highly relational.", "timestamp": "2026-03-10T10:00:00Z"},
    {"sender": "Priya", "text": "Agreed on Postgres. I'll set up the initial schema and migrations.", "timestamp": "2026-03-10T10:15:00Z"},
    {"sender": "Raj", "text": "I'll take ownership of the payment module. I have experience with Stripe webhooks.", "timestamp": "2026-03-10T14:00:00Z"},
    {"sender": "Alex", "text": "For the auth service, I'm hardcoding the JWT secret for now. We'll move to vault later.", "timestamp": "2026-03-11T09:00:00Z"},
    {"sender": "Sam", "text": "Getting a timeout error on the checkout endpoint. Seems intermittent.", "timestamp": "2026-03-12T10:00:00Z"},
    {"sender": "Raj", "text": "Payment webhook is fully integrated now. Only I know how the retry logic works though.", "timestamp": "2026-03-13T11:00:00Z"},
    {"sender": "Sam", "text": "Timeout error on checkout is back again. Second time this week.", "timestamp": "2026-03-14T09:00:00Z"},
    {"sender": "Alex", "text": "Just restart the pod when it happens. I'll investigate after the sprint.", "timestamp": "2026-03-14T09:30:00Z"},
    {"sender": "Sam", "text": "Has anyone heard back from design about the dashboard specs? We need them to start.", "timestamp": "2026-03-15T10:00:00Z"},
    {"sender": "Alex", "text": "Still no dashboard specs from design. This is blocking my entire sprint work.", "timestamp": "2026-03-17T10:00:00Z"},
    {"sender": "Sam", "text": "Timeout error AGAIN. That's the third time. We have a systemic issue here.", "timestamp": "2026-03-18T09:00:00Z"},
    {"sender": "Priya", "text": "I'm pushing this config change directly to main. It's a small fix, should be fine.", "timestamp": "2026-03-19T14:00:00Z"},
    {"sender": "Sam", "text": "Dashboard is completely blocked without those design specs. Week 2 of waiting.", "timestamp": "2026-03-20T10:00:00Z"},
]

# ====== ACME PRODUCT TEAM ======
PRODUCT_MESSAGES = [
    {"sender": "Lisa", "text": "Users keep asking about dark mode. It comes up in literally every demo.", "timestamp": "2026-03-10T10:00:00Z"},
    {"sender": "Mike", "text": "I think we should prioritize the mobile app this sprint. Mobile traffic is 60%.", "timestamp": "2026-03-11T10:00:00Z"},
    {"sender": "Sarah", "text": "No, API stability is way more important. Two enterprise clients complained last week about downtime.", "timestamp": "2026-03-11T10:30:00Z"},
    {"sender": "Lisa", "text": "Sarah from ClientCo literally said 'I would pay double if you had SSO integration.'", "timestamp": "2026-03-12T14:00:00Z"},
    {"sender": "Mike", "text": "Competitor X just launched a mobile-first version. We're falling behind on mobile.", "timestamp": "2026-03-13T10:00:00Z"},
    {"sender": "Lisa", "text": "I told ClientCo we'd have the dashboard demo ready by Friday March 21st.", "timestamp": "2026-03-14T10:00:00Z"},
    {"sender": "Sarah", "text": "Our conversion rate dropped to 2.3% after the last release. That's concerning.", "timestamp": "2026-03-15T11:00:00Z"},
    {"sender": "Mike", "text": "Let's commit to API-first approach for the rest of Q1.", "timestamp": "2026-03-17T10:00:00Z"},
    {"sender": "Lisa", "text": "Dark mode was mentioned again by three different users at the conference.", "timestamp": "2026-03-19T10:00:00Z"},
    {"sender": "Sarah", "text": "We really need to decide: mobile or API stability? This conflict is slowing us down.", "timestamp": "2026-03-20T10:00:00Z"},
]

# ====== ACME ↔ CLIENT CHANNEL ======
CLIENT_MESSAGES = [
    {"sender": "Lisa", "text": "Hi ClientCo team! Just confirming we'll have the dashboard mockups ready by Friday March 21st.", "timestamp": "2026-03-10T10:00:00Z"},
    {"sender": "Client_CEO", "text": "Great, looking forward to seeing them. This is a key deliverable for our board meeting.", "timestamp": "2026-03-10T10:30:00Z"},
    {"sender": "Lisa", "text": "We'll also share the API documentation by Wednesday March 19th.", "timestamp": "2026-03-11T10:00:00Z"},
    {"sender": "Client_CEO", "text": "Perfect. Oh, could you also add an export-to-PDF feature for the reports? That would be really helpful.", "timestamp": "2026-03-12T10:00:00Z"},
    {"sender": "Lisa", "text": "Sure, we'll look into the PDF export!", "timestamp": "2026-03-12T10:15:00Z"},
    {"sender": "Client_CEO", "text": "Any update on the dashboard mockups?", "timestamp": "2026-03-17T10:00:00Z"},
    {"sender": "Client_CEO", "text": "Also, would it be possible to add a dark mode option?", "timestamp": "2026-03-18T10:00:00Z"},
    {"sender": "Client_CEO", "text": "We really need those mockups before the board meeting on Monday.", "timestamp": "2026-03-19T10:00:00Z"},
    {"sender": "Client_CEO", "text": "I might need to loop in our VP if we can't get the timeline confirmed.", "timestamp": "2026-03-20T10:00:00Z"},
]


async def seed():
    print("🌱 Seeding demo data...\n")

    # Set lenses explicitly
    set_lens("acme_dev", "dev")
    set_lens("acme_product", "product")
    set_lens("acme_client", "client")

    # Process dev team (2 batches)
    print("Processing Acme Dev Team...")
    s1 = await process_message_batch("acme_dev", DEV_MESSAGES[:7])
    s2 = await process_message_batch("acme_dev", DEV_MESSAGES[7:])
    print(f"  ✅ Dev team: {len(s1) + len(s2)} signals stored\n")

    # Process product team
    print("Processing Acme Product Team...")
    s3 = await process_message_batch("acme_product", PRODUCT_MESSAGES[:5])
    s4 = await process_message_batch("acme_product", PRODUCT_MESSAGES[5:])
    print(f"  ✅ Product team: {len(s3) + len(s4)} signals stored\n")

    # Process client channel
    print("Processing Acme ↔ ClientCo Channel...")
    s5 = await process_message_batch("acme_client", CLIENT_MESSAGES[:5])
    s6 = await process_message_batch("acme_client", CLIENT_MESSAGES[5:])
    print(f"  ✅ Client channel: {len(s5) + len(s6)} signals stored\n")

    # Run pattern detection
    print("Running pattern detection on dev team...")
    from backend.agents.pattern_detector import detect_patterns
    patterns = await detect_patterns("acme_dev")
    print(f"  Found {len(patterns)} patterns")
    for p in patterns:
        print(f"    [{p.severity}] {p.type}: {p.description[:80]}")

    # Run cross-group analysis
    print("\n🔥 Running CROSS-GROUP ANALYSIS...")
    from backend.agents.cross_group_analyst import analyze_cross_group
    from backend.db.chroma import get_all_signals

    summaries = {
        "Acme Dev Team": get_all_signals("acme_dev"),
        "Acme Product": get_all_signals("acme_product"),
        "Acme ↔ ClientCo": get_all_signals("acme_client"),
    }
    insights = await analyze_cross_group(summaries)
    print(f"\n  Found {len(insights)} CROSS-GROUP INSIGHTS:")
    for i in insights:
        print(f"    🚨 [{i.severity}] {i.type}")
        print(f"       {i.description[:120]}")
        print(f"       Recommendation: {i.recommendation[:100]}")
        print()

    print("🎉 Demo data seeded successfully!")
    print("   Start the API with: python run_api.py")
    print("   Then visit: http://localhost:8000/api/groups")
    print("   And: http://localhost:8000/api/cross-group/insights")

asyncio.run(seed())