#!/usr/bin/env python3
"""
Test rapide du séjour 15_23096332
"""

from pathlib import Path
from pipeline_mco_pmsi.database.base import get_engine, get_session
from pipeline_mco_pmsi.database.models import StayDB, ClinicalDocumentDB
from pipeline_mco_pmsi.models.clinical import ClinicalDocument
from pipeline_mco_pmsi.models.metadata import StayMetadata
from pipeline_mco_pmsi.rag.rag_engine import RAGEngine
from pipeline_mco_pmsi.rag.referentiels_manager import ReferentielsManager
from pipeline_mco_pmsi.extractors.clinical_facts_extractor import ClinicalFactsExtractor
from pipeline_mco_pmsi.coders.codeur import Codeur

print("=" * 60)
print("TEST DU SÉJOUR 15_23096332")
print("=" * 60)

# 1. Charger le séjour depuis la base
print("\n1️⃣  Chargement du séjour depuis la base...")
engine = get_engine('sqlite:///pipeline_mco_pmsi.db')

with get_session(engine) as session:
    stay = session.query(StayDB).filter(StayDB.stay_id == '15_23096332').first()
    
    if not stay:
        print("❌ Séjour non trouvé")
        exit(1)
    
    print(f"✅ Séjour trouvé: {stay.stay_id}")
    print(f"   Spécialité: {stay.specialty}")
    
    # Récupérer les documents
    docs_db = session.query(ClinicalDocumentDB).filter(
        ClinicalDocumentDB.stay_id == stay.id
    ).order_by(ClinicalDocumentDB.priority).all()
    
    print(f"   Documents: {len(docs_db)}")
    
    # Créer les métadonnées du séjour (nécessaire pour process_documents)
    stay_metadata = StayMetadata(
        stay_id=stay.stay_id,
        admission_date=stay.admission_date,
        discharge_date=stay.discharge_date,
        specialty=stay.specialty
    )
    
    # Convertir en modèles Pydantic
    documents = []
    for doc_db in docs_db:
        doc = ClinicalDocument(
            document_id=doc_db.document_id,
            document_type=doc_db.document_type,
            content=doc_db.content,
            creation_date=doc_db.creation_date,
            author=doc_db.author or 'Inconnu',
            priority=doc_db.priority
        )
        documents.append(doc)
        print(f"     - {doc.document_id}: {doc.document_type} ({len(doc.content)} car.)")

# 2. Initialiser le RAG Engine
print("\n2️⃣  Initialisation du moteur RAG...")
referentiels_manager = ReferentielsManager(data_dir=Path('data/referentiels'))
rag_engine = RAGEngine(referentiels_manager=referentiels_manager)
print("✅ RAG Engine initialisé")

# 3. Segmentation des documents en sections
print("\n3️⃣  Segmentation des documents en sections...")

from pipeline_mco_pmsi.processors.document_processor import DocumentProcessor

processor = DocumentProcessor()

try:
    structured_stay = processor.process_documents(
        documents=documents,
        stay_metadata=stay_metadata
    )
    print(f"✅ {len(structured_stay.sections)} sections créées")
    
    # Afficher quelques sections
    for i, section in enumerate(structured_stay.sections[:3], 1):
        print(f"   {i}. {section.section_type}: {section.content[:80]}...")
    
    if len(structured_stay.sections) > 3:
        print(f"   ... et {len(structured_stay.sections) - 3} autres sections")

except Exception as e:
    print(f"❌ Erreur lors de la segmentation: {e}")
    import traceback
    traceback.print_exc()
    exit(1)

# 4. Extraction des faits cliniques
print("\n4️⃣  Extraction des faits cliniques...")
print("   ⏳ Cela peut prendre 1-2 minutes...")

extractor = ClinicalFactsExtractor()

try:
    facts = extractor.extract_facts(structured_stay)
    print(f"✅ {len(facts)} faits extraits")
    
    # Afficher quelques faits
    for i, fact in enumerate(facts[:5], 1):
        print(f"   {i}. [{fact.type}] {fact.text[:80]}...")
        print(f"      Certitude: {fact.qualifier.certainty}, Temporalité: {fact.temporality}")
    
    if len(facts) > 5:
        print(f"   ... et {len(facts) - 5} autres faits")

except Exception as e:
    print(f"❌ Erreur lors de l'extraction: {e}")
    import traceback
    traceback.print_exc()
    exit(1)

# 5. Codage
print("\n5️⃣  Proposition de codes CIM-10 et CCAM...")
print("   ⏳ Cela peut prendre 2-3 minutes...")

codeur = Codeur(rag_engine=rag_engine)

try:
    coding_proposal = codeur.propose_codes(
        facts=facts,
        stay_metadata=stay_metadata
    )
    
    print("✅ Codage terminé")
    print(f"\n📊 RÉSULTATS:")
    print(f"\n  DP (Diagnostic Principal):")
    if coding_proposal.dp:
        print(f"    {coding_proposal.dp.code} - {coding_proposal.dp.label}")
        print(f"    Confiance: {coding_proposal.dp.confidence:.2%}")
        print(f"    Justification: {coding_proposal.dp.reasoning[:150]}...")
    else:
        print("    Non proposé")
    
    print(f"\n  DR (Diagnostic Relié):")
    if coding_proposal.dr:
        print(f"    {coding_proposal.dr.code} - {coding_proposal.dr.label}")
        print(f"    Confiance: {coding_proposal.dr.confidence:.2%}")
    else:
        print("    Non proposé")
    
    print(f"\n  DAS (Diagnostics Associés Significatifs): {len(coding_proposal.das)}")
    for i, das in enumerate(coding_proposal.das[:5], 1):
        print(f"    {i}. {das.code} - {das.label}")
        print(f"       Confiance: {das.confidence:.2%}")
    
    if len(coding_proposal.das) > 5:
        print(f"    ... et {len(coding_proposal.das) - 5} autres DAS")
    
    print(f"\n  CCAM (Actes): {len(coding_proposal.ccam)}")
    for i, ccam in enumerate(coding_proposal.ccam[:5], 1):
        print(f"    {i}. {ccam.code} - {ccam.label}")
        print(f"       Confiance: {ccam.confidence:.2%}")
    
    if len(coding_proposal.ccam) > 5:
        print(f"    ... et {len(coding_proposal.ccam) - 5} autres actes")

except Exception as e:
    print(f"❌ Erreur lors du codage: {e}")
    import traceback
    traceback.print_exc()
    exit(1)

print("\n" + "=" * 60)
print("✅ TEST TERMINÉ AVEC SUCCÈS")
print("=" * 60)
