"""Test Milestone 3: Signal Extractor, Classifier, and Context Detector working."""
import asyncio, os, sys
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))

DEV_CHAT = """[Alex]: Hey team, I think we should go with PostgreSQL for the main DB. MongoDB is overkill for our relational data.
[Priya]: Agreed on Postgres. I'll set up the schema today.
[Raj]: Payment module webhook integration is looking tricky. I'll handle it myself since I know the Stripe API best.
[Alex]: I'm just gonna hardcode the API URL for now, we'll add env vars when we dockerize.
[Sam]: The timeout error on the checkout endpoint is happening again. Third time this week.
[Alex]: Just restart the pod for now, I'll look at it after the sprint."""

PRODUCT_CHAT = """[Lisa]: Users keep asking about dark mode, it comes up in every demo.
[Mike]: I think we should prioritize the mobile app over the API this sprint.
[Sarah]: No way, API stability is way more important. Two enterprise clients complained last week.
[Lisa]: Sarah from Acme literally said 'I would pay double if you had SSO integration.'
[Mike]: Competitor X just launched a mobile-first version. We're falling behind."""

async def test_signal_extractor():
    from backend.agents.signal_extractor import extract_signals

    print("Testing Signal Extractor (DevLens)...")
    signals = await extract_signals(DEV_CHAT, "test-dev", lens="dev")
    print(f"  Extracted {len(signals)} signals:")
    for s in signals:
        print(f"    - [{s.type}] {s.summary[:70]}...")
    assert len(signals) >= 2, f"Expected >=2 signals, got {len(signals)}"
    print(f"  ✅ DevLens extraction working ({len(signals)} signals)")

    print("\nTesting Signal Extractor (ProductLens)...")
    signals2 = await extract_signals(PRODUCT_CHAT, "test-product", lens="product")
    print(f"  Extracted {len(signals2)} signals:")
    for s in signals2:
        print(f"    - [{s.type}] {s.summary[:70]}...")
    assert len(signals2) >= 2, f"Expected >=2 signals, got {len(signals2)}"
    print(f"  ✅ ProductLens extraction working ({len(signals2)} signals)")

async def test_classifier():
    from backend.agents.signal_extractor import extract_signals
    from backend.agents.classifier import classify_signal

    print("\nTesting Classifier Agent...")
    signals = await extract_signals(DEV_CHAT, "test-classify", lens="dev")
    if signals:
        classified = await classify_signal(signals[0])
        print(f"  Signal: {classified.summary[:60]}")
        print(f"  Sentiment: {classified.sentiment}, Urgency: {classified.urgency}")
        print(f"  Keywords: {classified.keywords}")
        print(f"  ✅ Classifier working")
    else:
        print(f"  ⚠️ No signals to classify (extractor returned empty)")

async def test_context_detector():
    from backend.agents.context_detector import detect_context

    print("\nTesting Context Detector...")
    result = await detect_context(DEV_CHAT)
    print(f"  Detected: {result['detected_lens']} (confidence: {result['confidence']})")
    print(f"  Evidence: {result['evidence']}")
    assert result["detected_lens"] == "dev", f"Expected 'dev', got '{result['detected_lens']}'"
    print(f"  ✅ Correctly detected as 'dev'")

    result2 = await detect_context(PRODUCT_CHAT)
    print(f"  Detected: {result2['detected_lens']} (confidence: {result2['confidence']})")
    assert result2["detected_lens"] == "product", f"Expected 'product', got '{result2['detected_lens']}'"
    print(f"  ✅ Correctly detected as 'product'")

async def main():
    await test_signal_extractor()
    await test_classifier()
    await test_context_detector()
    print("\n🎉 MILESTONE 3 PASSED — Core agents working")

asyncio.run(main())
