"""Tests for ValueCodeValidator."""

from __future__ import annotations

from claim_validator.models.institutional import (
    InstitutionalClaimData,
    ValueCode,
)
from claim_validator.validators.institutional.value_code import ValueCodeValidator


class TestValueCodeValidator:
    def setup_method(self) -> None:
        self.v = ValueCodeValidator()

    def test_valid_value_codes_with_amounts(self) -> None:
        claim = InstitutionalClaimData(
            value_codes=[
                ValueCode(code="01", amount=100.00),
                ValueCode(code="50", amount=250.50),
            ]
        )
        result = self.v.validate(claim)
        assert len(result.findings) == 0

    def test_empty_list_skips(self) -> None:
        claim = InstitutionalClaimData(value_codes=[])
        result = self.v.validate(claim)
        assert len(result.findings) == 0

    def test_invalid_code_format(self) -> None:
        claim = InstitutionalClaimData(
            value_codes=[
                ValueCode(code="X", amount=10.0),
                ValueCode(code="ABC", amount=20.0),
            ]
        )
        result = self.v.validate(claim)
        assert len(result.findings) == 2
        assert all(f.code == "INST_INVALID_VALUE_CODE" for f in result.findings)

    def test_missing_amount_on_required_code(self) -> None:
        claim = InstitutionalClaimData(
            value_codes=[
                ValueCode(code="01", amount=None),
                ValueCode(code="A5", amount=0.0),
            ]
        )
        result = self.v.validate(claim)
        assert len(result.findings) == 2
        assert all(
            f.code == "INST_MISSING_VALUE_CODE_AMOUNT" for f in result.findings
        )
        assert all(f.severity.value == "warning" for f in result.findings)

    def test_mixed_valid_invalid_missing(self) -> None:
        claim = InstitutionalClaimData(
            value_codes=[
                ValueCode(code="01", amount=100.00),  # valid
                ValueCode(code="@!", amount=10.0),  # invalid format
                ValueCode(code="50", amount=None),  # missing amount
            ]
        )
        result = self.v.validate(claim)
        codes = [f.code for f in result.findings]
        assert "INST_INVALID_VALUE_CODE" in codes
        assert "INST_MISSING_VALUE_CODE_AMOUNT" in codes
        assert len(result.findings) == 2
