"""
Test the v2 Multi-Agent Student Simulation.

This tests the full pipeline:
1. Knowledge Agent - Builds knowledge constraints
2. Perception Agent - How student reads question
3. Answer Agent - Answers with constraints
4. Verifier Agent - Catches "cheating"
"""

import asyncio
import json
import os
import sys
sys.path.insert(0, "src")

from mirofish_simulator import (
    StudentSimulator,
    simulate_student,
    simulate_classroom,
    KnowledgeAgent,
    PerceptionAgent,
    AnswerAgent,
    VerifierAgent,
)


# Test questions
QUESTIONS = [
    {
        "name": "Electoral College (AP Gov)",
        "question": {
            "text": "How many electoral votes are needed to win the presidency?",
            "options": ["218", "270", "300", "435"],
            "subject": "AP Government",
        },
        "correct_answer": "B",
        "notes": "Requires specific knowledge most younger students wouldn't have",
    },
    {
        "name": "Amendment (US History)",
        "question": {
            "text": "Which amendment to the Constitution abolished slavery?",
            "options": ["12th Amendment", "13th Amendment", "14th Amendment", "15th Amendment"],
            "subject": "US History",
        },
        "correct_answer": "B",
        "notes": "Often confused with 14th (citizenship) and 15th (voting rights)",
    },
    {
        "name": "Simple Civics",
        "question": {
            "text": "Who makes the laws in the United States?",
            "options": ["The President", "Congress", "The judges", "The police"],
            "subject": "Civics",
        },
        "correct_answer": "B",
        "notes": "Basic civics knowledge, most grades should know",
    },
]


async def test_individual_agents():
    """Test each agent in isolation."""
    print("=" * 70)
    print("TEST 1: Individual Agents")
    print("=" * 70)

    question = QUESTIONS[0]["question"]

    # Knowledge Agent
    print("\n--- Knowledge Agent ---")
    knowledge_agent = KnowledgeAgent()
    knowledge = await knowledge_agent.build_profile(question, grade=5, archetype="esl_student")

    print(f"Grade 5 ESL student knowledge about Electoral College:")
    print(f"  Known: {knowledge.concepts_known[:3]}")
    print(f"  Unknown: {knowledge.concepts_unknown[:3]}")
    print(f"  Misconceptions: {knowledge.misconceptions[:2]}")
    print(f"  Curriculum reasoning: {knowledge.curriculum_reasoning[:100]}...")

    # Perception Agent
    print("\n--- Perception Agent ---")
    perception_agent = PerceptionAgent()
    perception = await perception_agent.perceive(question, knowledge)

    print(f"How they read the question:")
    print(f"  Perceived: {perception.perceived_question}")
    print(f"  Comprehension: {perception.overall_comprehension:.0%}")
    print(f"  Unclear words: {perception.words_unclear}")
    print(f"  Interpretation: {perception.interpretation[:100]}...")

    # Answer Agent
    print("\n--- Answer Agent ---")
    answer_agent = AnswerAgent()
    answer = await answer_agent.answer(question, knowledge, perception)

    print(f"Their answer:")
    print(f"  Selected: {answer.selected} ({answer.confidence})")
    print(f"  Reasoning: {answer.thought_process[:150]}...")
    print(f"  Knowledge used: {answer.knowledge_applied[:2]}")
    print(f"  Knowledge gaps felt: {answer.knowledge_gaps_felt[:2]}")

    # Verifier Agent
    print("\n--- Verifier Agent ---")
    verifier_agent = VerifierAgent()
    verification = await verifier_agent.verify(
        question, knowledge, answer, correct_answer="B"
    )

    print(f"Verification:")
    print(f"  Consistent: {verification.is_consistent}")
    print(f"  Suspiciously correct: {verification.suspiciously_correct}")
    print(f"  Issues: {verification.issues_found[:2]}")
    print(f"  Reasoning: {verification.reasoning[:150]}...")


async def test_full_simulation():
    """Test the complete orchestrated simulation."""
    print("\n" + "=" * 70)
    print("TEST 2: Full Simulation Pipeline")
    print("=" * 70)

    question = QUESTIONS[0]["question"]

    for grade, archetype in [(5, "esl_student"), (8, "average_student"), (11, "honors_overachiever")]:
        print(f"\n--- Grade {grade} {archetype} ---")

        result = await simulate_student(
            question=question,
            grade=grade,
            archetype=archetype,
            correct_answer="B",
        )

        correct_str = "✓" if result.is_correct else "✗"
        consistent_str = "consistent" if result.verification.is_consistent else "INCONSISTENT"

        print(f"  Answer: {result.selected} ({correct_str}) - {result.confidence}")
        print(f"  Verification: {consistent_str}")
        print(f"  Knowledge gaps: {result.knowledge_profile.concepts_unknown[:2]}")

        if result.was_rerun:
            print(f"  (Rerun {result.rerun_count} times due to inconsistency)")


async def test_classroom():
    """Test classroom simulation."""
    print("\n" + "=" * 70)
    print("TEST 3: Classroom Simulation")
    print("=" * 70)

    question = QUESTIONS[2]["question"]  # Simple civics question

    print(f"\nQuestion: {question['text']}")
    print(f"Correct answer: B (Congress)")
    print("Simulating 10 students...")

    results = await simulate_classroom(
        question=question,
        grade=5,
        correct_answer="B",
        population_size=10,
        max_concurrent=3,
    )

    print(f"\nResults:")
    print(f"  Accuracy: {results['accuracy']:.0%}")
    print(f"  Consistency: {results['consistency_rate']:.0%}")
    print(f"\n  Response distribution:")
    for option, count in sorted(results['response_distribution'].items()):
        marker = " <-- correct" if option == "B" else ""
        print(f"    {option}: {count} students{marker}")

    print(f"\n  By archetype:")
    for arch, data in sorted(results['by_archetype'].items(), key=lambda x: -x[1]['accuracy']):
        print(f"    {arch}: {data['accuracy']:.0%} accuracy, {data['consistency']:.0%} consistent ({data['count']} students)")


async def test_hard_questions():
    """Test with questions that should differentiate grades."""
    print("\n" + "=" * 70)
    print("TEST 4: Grade Differentiation")
    print("=" * 70)

    for q_data in QUESTIONS[:2]:  # Skip simple civics
        question = q_data["question"]
        print(f"\n--- {q_data['name']} ---")
        print(f"Q: {question['text']}")
        print(f"Correct: {q_data['correct_answer']}")
        print(f"Notes: {q_data['notes']}")

        for grade in [5, 8, 11]:
            result = await simulate_student(
                question=question,
                grade=grade,
                archetype="average_student",
                correct_answer=q_data["correct_answer"],
            )

            correct = "✓" if result.is_correct else "✗"
            consistent = "C" if result.verification.is_consistent else "I"
            print(f"  Grade {grade}: {result.selected} ({correct}) [{consistent}] - {result.confidence}")


async def test_consistency_detection():
    """Test if verifier catches cheating."""
    print("\n" + "=" * 70)
    print("TEST 5: Consistency Detection (Catching Cheating)")
    print("=" * 70)

    question = QUESTIONS[0]["question"]  # Electoral College

    print("\nTesting if verifier catches when LLM 'cheats' (knows answer despite constraints)")
    print("A grade 5 ESL student shouldn't know 270 electoral votes...")

    # Run multiple times to see if cheating is detected
    cheating_detected = 0
    correct_answers = 0

    for i in range(5):
        result = await simulate_student(
            question=question,
            grade=5,
            archetype="esl_student",
            correct_answer="B",
        )

        if result.is_correct:
            correct_answers += 1

        if result.verification.suspiciously_correct:
            cheating_detected += 1

        status = "CAUGHT" if result.verification.suspiciously_correct else "ok"
        correct_str = "correct" if result.is_correct else "wrong"
        print(f"  Run {i+1}: {result.selected} ({correct_str}) - Verifier: {status}")

    print(f"\nSummary:")
    print(f"  Correct answers: {correct_answers}/5")
    print(f"  Cheating detected: {cheating_detected}/5")


async def main():
    print("\n🧪 MiroFish v2 Multi-Agent Simulation Test\n")
    print("This tests the 4-agent architecture:")
    print("  1. Knowledge Agent - What does student know?")
    print("  2. Perception Agent - How do they read the question?")
    print("  3. Answer Agent - What do they pick?")
    print("  4. Verifier Agent - Is the answer consistent?")

    if not os.environ.get("OPENAI_API_KEY"):
        print("\n⚠️  No OPENAI_API_KEY - tests will use fallback mode")
        print("   Set OPENAI_API_KEY for real agent behavior\n")

    await test_individual_agents()
    await test_full_simulation()
    await test_classroom()
    await test_hard_questions()
    await test_consistency_detection()

    print("\n" + "=" * 70)
    print("✅ All tests completed!")
    print("=" * 70)


if __name__ == "__main__":
    asyncio.run(main())
