import os
import sys
import json
from pathlib import Path

# Add project root to sys.path
sys.path.append(str(Path(__file__).resolve().parent))

import config
from services.data_loader import load_personas, load_questions
from services.simulator import SimulationEngine

def reproduce_issue():
    print("🧪 Reproducing Systematic Failure on Personality Chunk 4...")
    
    # Load data
    adolescents, _ = load_personas()
    questions_map = load_questions()
    
    # Pick first student
    student = adolescents[0]
    personality_qs = questions_map.get('Personality', [])
    age_qs = [q for q in personality_qs if '14-17' in q.get('age_group', '')]
    
    # Target Chunk 4 (105-130)
    chunk4 = age_qs[105:130] 
    
    print(f"👤 Testing Student: {student.get('StudentCPID')}")
    print(f"📋 Chunk Size: {len(chunk4)}")
    
    engine = SimulationEngine(config.ANTHROPIC_API_KEY)
    
    # Run simulation with verbose logging
    answers = engine.simulate_batch(student, chunk4, verbose=True)
    
    print("\n✅ Simulation Complete")
    print(f"🔢 Answers captured: {len(answers)}/{len(chunk4)}")
    print(f"🔍 Answer keys: {list(answers.keys())}")
    
    # Find missing
    chunk_codes = [q['q_code'] for q in chunk4]
    missing = [c for c in chunk_codes if c not in answers]
    if missing:
        print(f"❌ Missing keys: {missing}")
    else:
        print("🎉 All keys captured!")

if __name__ == '__main__':
    reproduce_issue()
