"""Tests for LangChain chain output schemas."""
from __future__ import annotations

import pytest
from pydantic import ValidationError

from claim_validator.constants import Severity


class TestCodeValidationOutput:
    def test_valid_output(self) -> None:
        from claim_validator.chains.output_schemas.code_validation import (
            CodeValidationFinding, CodeValidationOutput,
        )
        output = CodeValidationOutput(
            findings=[CodeValidationFinding(
                code="AI_CLINICAL_IMPLAUSIBILITY", message="Gender mismatch",
                severity=Severity.WARNING, field_name="procedure_code",
                line_number=1, suggestion="Verify gender",
            )],
            confidence=0.85, reasoning="Prostate procedure on female patient",
        )
        assert len(output.findings) == 1
        assert output.confidence == 0.85

    def test_empty_findings(self) -> None:
        from claim_validator.chains.output_schemas.code_validation import CodeValidationOutput
        output = CodeValidationOutput(findings=[], confidence=0.95, reasoning="All plausible")
        assert output.findings == []

    def test_confidence_out_of_range(self) -> None:
        from claim_validator.chains.output_schemas.code_validation import CodeValidationOutput
        with pytest.raises(ValidationError):
            CodeValidationOutput(findings=[], confidence=1.5, reasoning="Bad")

    def test_to_validator_output(self) -> None:
        from claim_validator.chains.output_schemas.code_validation import (
            CodeValidationFinding, CodeValidationOutput,
        )
        output = CodeValidationOutput(
            findings=[CodeValidationFinding(
                code="AI_CLINICAL_IMPLAUSIBILITY", message="Age mismatch",
                severity=Severity.WARNING, field_name="procedure_code",
                line_number=2, suggestion="Check age",
            )],
            confidence=0.9, reasoning="Pediatric procedure on adult",
        )
        vo = output.to_validator_output()
        assert vo.validator_name == "code_validation_ai"
        assert len(vo.findings) == 1
        assert vo.findings[0].code == "AI_CLINICAL_IMPLAUSIBILITY"
        assert vo.findings[0].context == {"confidence": 0.9, "reasoning": "Pediatric procedure on adult"}


class TestPriorAuthOutput:
    def test_valid_output(self) -> None:
        from claim_validator.chains.output_schemas.prior_auth import PriorAuthOutput, PriorAuthRequirement
        output = PriorAuthOutput(
            requirements=[PriorAuthRequirement(procedure_code="70553", requires_auth=True, reason="MRI requires PA", urgency="routine")],
            overall_determination="required", confidence=0.9, reasoning="High-cost imaging",
        )
        assert output.overall_determination == "required"

    def test_invalid_determination(self) -> None:
        from claim_validator.chains.output_schemas.prior_auth import PriorAuthOutput
        with pytest.raises(ValidationError):
            PriorAuthOutput(requirements=[], overall_determination="maybe", confidence=0.5, reasoning="Unsure")


class TestCoverageCheckOutput:
    def test_valid_output(self) -> None:
        from claim_validator.chains.output_schemas.coverage_check import CoverageCheckOutput, CoverageConcern
        output = CoverageCheckOutput(
            concerns=[CoverageConcern(code="AI_COVERAGE_CONCERN", message="Frequency limit", severity=Severity.WARNING, medical_necessity_score=0.6)],
            overall_medical_necessity="questionable", confidence=0.75, reasoning="Too many visits",
        )
        assert output.concerns[0].medical_necessity_score == 0.6


class TestEligibilityInterpreterOutput:
    def test_valid_output(self) -> None:
        from claim_validator.chains.output_schemas.eligibility_interpreter import EligibilityDetail, EligibilityInterpreterOutput
        output = EligibilityInterpreterOutput(
            eligible=True, details=EligibilityDetail(coverage_active=True, plan_name="PPO Gold", copay=25.0),
            warnings=[], reasoning="Active coverage",
        )
        assert output.eligible is True
        assert output.details.copay == 25.0


class TestPAInterpreterOutput:
    def test_valid_output(self) -> None:
        from claim_validator.chains.output_schemas.pa_interpreter import PADecision, PAInterpreterOutput
        output = PAInterpreterOutput(
            decision=PADecision(approved=True, auth_number="AUTH123"), next_steps=[], reasoning="Approved",
        )
        assert output.decision.auth_number == "AUTH123"


class TestRejectionSummaryOutput:
    def test_valid_output(self) -> None:
        from claim_validator.chains.output_schemas.rejection_summary import RejectionSummaryOutput
        output = RejectionSummaryOutput(
            summary="Eligibility failed due to inactive coverage",
            suggestions=["Verify member ID", "Check effective dates"],
        )
        assert len(output.suggestions) == 2


class TestPreSubmissionOutput:
    def test_valid_output(self) -> None:
        from claim_validator.chains.output_schemas.pre_submission import PreSubmissionOutput
        output = PreSubmissionOutput(ready_to_submit=True, concerns=[], summary="Ready to submit")
        assert output.ready_to_submit is True
