from pathlib import Path

from cyclone.parsers.parse_837 import parse
from cyclone.parsers.payer import PayerConfig


FIXTURE = Path(__file__).parent / "fixtures" / "co_medicaid_837p.txt"


def test_co_fixture_two_claims_pass():
    result = parse(FIXTURE.read_text(), PayerConfig.co_medicaid())
    assert result.summary.total_claims == 2
    assert result.summary.passed == 2
    assert result.summary.failed == 0


def test_first_claim_has_two_service_lines():
    result = parse(FIXTURE.read_text(), PayerConfig.co_medicaid())
    c1 = result.claims[0]
    assert c1.claim_id == "t991102984o1c1d"
    assert len(c1.service_lines) == 2
    assert c1.service_lines[0].procedure.code == "T1019"
    assert c1.service_lines[0].procedure.modifiers == ["U2"]
    assert c1.service_lines[1].service_date is not None


def test_second_claim_has_four_service_lines():
    result = parse(FIXTURE.read_text(), PayerConfig.co_medicaid())
    c2 = result.claims[1]
    assert c2.claim_id == "t991102984o1c2d"
    assert len(c2.service_lines) == 4


def test_subscriber_payer_reused_across_claims():
    """Both claims share the same billing provider; payers are recorded on each claim."""
    result = parse(FIXTURE.read_text(), PayerConfig.co_medicaid())
    assert result.claims[0].billing_provider.npi == "1881068062"
    assert result.claims[1].billing_provider.npi == "1881068062"
    assert result.claims[0].payer.id == "SKCO0"
    assert result.claims[1].payer.id == "SKCO0"
