"""TaxonomyValidator -- validates provider taxonomy codes against NUCC table."""

from __future__ import annotations

from claim_validator.constants import Severity
from claim_validator.models.claim import ClaimData
from claim_validator.models.results import Finding, ValidatorOutput
from claim_validator.shared.code_tables.taxonomy import lookup_taxonomy
from claim_validator.validators.base import BaseValidator


class TaxonomyValidator(BaseValidator):
    """Validates billing provider taxonomy code against the NUCC table."""

    name = "TaxonomyValidator"

    def validate(self, claim: ClaimData) -> ValidatorOutput:
        findings: list[Finding] = []

        tax = claim.billing_provider_taxonomy
        if tax is not None and tax.strip():
            if lookup_taxonomy(tax) is None:
                findings.append(
                    self._make_finding(
                        code="INVALID_TAXONOMY_CODE",
                        message=(
                            f"Provider taxonomy code {tax}"
                            " is not a recognized NUCC taxonomy code"
                        ),
                        severity=Severity.WARNING,
                        field_name="billing_provider_taxonomy",
                        suggestion=(
                            "Use a valid 10-character NUCC provider taxonomy code"
                            " (e.g., 207Q00000X = Family Medicine)"
                        ),
                    )
                )

        return self._make_output(findings)
