"""
Regression tests for copied kernel code.

Ensures the foundation (kernel_core, correction_engine, evidence_fusion, etc.)
still works after being copied/refactored.
"""

import pytest


def test_kernel_core_imports():
    """Ensure kernel_core can be imported and ContextCollector class exists."""
    module = pytest.importorskip("kernel_service.kernel_core")
    assert hasattr(module, "ContextCollector")


def test_context_collector_methods():
    """Ensure ContextCollector has expected methods."""
    kernel_core = pytest.importorskip("kernel_service.kernel_core")
    collector = kernel_core.ContextCollector(conn=None)
    for method in [
        "get_patient_context",
        "get_provider_context",
        "get_payer_context",
        "get_temporal_context",
        "get_financial_context",
        "get_relationship_context",
        "get_policy_context",
    ]:
        assert hasattr(collector, method)


def test_correction_engine_generates_actions():
    """Ensure correction_engine can generate corrections."""
    correction_engine = pytest.importorskip("kernel_service.correction_engine")
    engine = correction_engine.CorrectionEngine()

    docs = [
        {
            "text": "Add modifier 59 for distinct diagnostic service",
            "score": 0.9,
            "source": "Policy",
        }
    ]
    corrections = engine.generate(docs, decision="FAIL")
    assert isinstance(corrections, list)
    assert len(corrections) >= 1
    assert "action" in corrections[0]


def test_evidence_fusion_3plane():
    """Ensure 3-plane evidence fusion works."""
    fusion_module = pytest.importorskip("kernel_service.fusion.evidence_fusion")
    engine = fusion_module.EvidenceFusionEngine(conn=None)

    # Should have fusion_weights and methods
    assert hasattr(engine, "fusion_weights")
    assert "default" in engine.fusion_weights


def test_policy_recommendation_extractor_import():
    """Ensure policy recommendation extractor can be imported."""
    module = pytest.importorskip("kernel_service.policy_recommendation_extractor")
    assert hasattr(module, "PolicyRecommendationExtractor")


def test_dynamic_mue_checker_import():
    """Ensure dynamic MUE checker can be imported."""
    module = pytest.importorskip("kernel_service.dynamic_mue_checker")
    assert hasattr(module, "DynamicMUEChecker")


def test_dynamic_ptp_checker_import():
    """Ensure dynamic PTP checker can be imported."""
    module = pytest.importorskip("kernel_service.dynamic_ptp_checker")
    assert hasattr(module, "DynamicPTPChecker")


def test_fraud_risk_scorer_import():
    """Ensure fraud risk scorer can be imported."""
    module = pytest.importorskip("kernel_service.fraud_risk_scorer")
    assert hasattr(module, "FraudRiskScorer")


def test_threshold_calibration_callable():
    """Ensure threshold calibration module has entry point."""
    module = pytest.importorskip("kernel_service.cxr_threshold_calibration")
    assert hasattr(module, "calibrate_thresholds")

