"""Public re-exports for the 837P parser.

Re-exports are lazy via PEP 562 ``__getattr__`` so that importing a name from
this package (e.g. ``from cyclone.parsers.exceptions import CycloneParseError``)
does not require every referenced module to be importable. Downstream code can
write ``cyclone.parsers.PayerConfig`` and it will be loaded on first access.
"""

from __future__ import annotations

# Eager: only the exceptions module is needed early and unconditionally.
from cyclone.parsers.exceptions import CycloneParseError, CycloneValidationError

_LAZY_EXPORTS: dict[str, str] = {
    # exceptions
    "CycloneParseError": "cyclone.parsers.exceptions",
    "CycloneValidationError": "cyclone.parsers.exceptions",
    # models (837P)
    "Address": "cyclone.parsers.models",
    "BatchSummary": "cyclone.parsers.models",
    "BillingProvider": "cyclone.parsers.models",
    "ClaimHeader": "cyclone.parsers.models",
    "ClaimOutput": "cyclone.parsers.models",
    "Diagnosis": "cyclone.parsers.models",
    "Envelope": "cyclone.parsers.models",
    "ParseResult": "cyclone.parsers.models",
    "Payer": "cyclone.parsers.models",
    "Procedure": "cyclone.parsers.models",
    "ServiceLine": "cyclone.parsers.models",
    "Subscriber": "cyclone.parsers.models",
    "ValidationIssue": "cyclone.parsers.models",
    "ValidationReport": "cyclone.parsers.models",
    # models (835)
    "ClaimAdjustment": "cyclone.parsers.models_835",
    "ClaimPayment": "cyclone.parsers.models_835",
    "FinancialInfo": "cyclone.parsers.models_835",
    "ParseResult835": "cyclone.parsers.models_835",
    "Payer835": "cyclone.parsers.models_835",
    "Payee835": "cyclone.parsers.models_835",
    "ReassociationTrace": "cyclone.parsers.models_835",
    "ServicePayment": "cyclone.parsers.models_835",
    "claim_status_label": "cyclone.parsers.models_835",
    # models (999 ACK — SP3 P3 T10)
    "AcknowledgmentHeader": "cyclone.parsers.models_999",
    "FunctionalGroupAck": "cyclone.parsers.models_999",
    "ParseResult999": "cyclone.parsers.models_999",
    "SegmentContext": "cyclone.parsers.models_999",
    "SegmentError": "cyclone.parsers.models_999",
    "SetAcceptReject": "cyclone.parsers.models_999",
    "SetFunctionalGroupResponse": "cyclone.parsers.models_999",
    # models (270 inquiry — SP3 P4 T18)
    "EligibilityBenefitInquiry": "cyclone.parsers.models_270",
    "InformationReceiver270": "cyclone.parsers.models_270",
    "InformationSource270": "cyclone.parsers.models_270",
    "ParseResult270": "cyclone.parsers.models_270",
    "Patient270": "cyclone.parsers.models_270",
    "Subscriber270": "cyclone.parsers.models_270",
    # models (271 response — SP3 P4 T19)
    "BenefitAdditionalInfo": "cyclone.parsers.models_271",
    "CoverageBenefit": "cyclone.parsers.models_271",
    "InformationReceiver271": "cyclone.parsers.models_271",
    "InformationSource271": "cyclone.parsers.models_271",
    "ParseResult271": "cyclone.parsers.models_271",
    "Patient271": "cyclone.parsers.models_271",
    "Subscriber271": "cyclone.parsers.models_271",
    "SERVICE_TYPE_CODES": "cyclone.parsers.models_271",
    "LAST_UPDATED": "cyclone.parsers.models_271",
    "service_type_description": "cyclone.parsers.models_271",
    # models (277CA — SP10)
    "ACCEPTED_CODES": "cyclone.parsers.models_277ca",
    "AcknowledgmentHeader277": "cyclone.parsers.models_277ca",
    "ClaimStatus": "cyclone.parsers.models_277ca",
    "PAID_CODES": "cyclone.parsers.models_277ca",
    "PENDED_CODES": "cyclone.parsers.models_277ca",
    "ParseResult277CA": "cyclone.parsers.models_277ca",
    "REJECTED_CODES": "cyclone.parsers.models_277ca",
    "classify_status_code": "cyclone.parsers.models_277ca",
    # CARC lookup (SP3 P2 T6)
    "reason_label": "cyclone.parsers.cas_codes",
    "all_known_codes": "cyclone.parsers.cas_codes",
    # payer
    "PayerConfig": "cyclone.parsers.payer",
    "PayerConfig835": "cyclone.parsers.payer",
    # segments
    "Delimiters": "cyclone.parsers.segments",
    "tokenize": "cyclone.parsers.segments",
    # orchestrators
    "parse": "cyclone.parsers.parse_837",
    "parse_835": "cyclone.parsers.parse_835",
    "parse_999": "cyclone.parsers.parse_999",
    "parse_270": "cyclone.parsers.parse_270",
    "parse_271": "cyclone.parsers.parse_271",
    "parse_277ca": "cyclone.parsers.parse_277ca",
    "serialize_999": "cyclone.parsers.serialize_999",
    "serialize_270": "cyclone.parsers.serialize_270",
    "build_ack_for_batch": "cyclone.parsers.batch_ack_builder",
    # writers
    "write_outputs": "cyclone.parsers.writer",
    "write_outputs_835": "cyclone.parsers.writer_835",
}

_cache: dict[str, object] = {}


def __getattr__(name: str) -> object:
    module_path = _LAZY_EXPORTS.get(name)
    if module_path is None:
        raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
    if name in _cache:
        return _cache[name]
    import importlib
    mod = importlib.import_module(module_path)
    value = getattr(mod, name)
    _cache[name] = value
    return value


def __dir__() -> list[str]:
    return sorted(list(_LAZY_EXPORTS.keys()))


__all__ = sorted(_LAZY_EXPORTS.keys())
