"""Schema constant and validation tests for regulatory_export."""

from __future__ import annotations

import re

import pytest

from orxaq.credit.regulatory_export import (
    ASC_326_REQUIRED_FIELDS,
    CALL_REPORT_RC_R_MAP,
    CCAR_SCHEMA_VERSION,
    CECL_SCHEMA_VERSION,
    FR_Y14A_FIELD_MAP,
    RegulatoryExporter,
    validate_ccar_schema,
    validate_cecl_schema,
)

_SEMVER_RE = re.compile(r"^\d+\.\d+\.\d+$")


class TestSchemaConstants:
    def test_cecl_schema_version_semver(self) -> None:
        assert _SEMVER_RE.match(CECL_SCHEMA_VERSION), CECL_SCHEMA_VERSION

    def test_ccar_schema_version_semver(self) -> None:
        assert _SEMVER_RE.match(CCAR_SCHEMA_VERSION), CCAR_SCHEMA_VERSION

    def test_fr_y14a_map_is_dict(self) -> None:
        assert isinstance(FR_Y14A_FIELD_MAP, dict)

    def test_fr_y14a_map_nonempty(self) -> None:
        assert len(FR_Y14A_FIELD_MAP) >= 5

    def test_call_report_map_is_dict(self) -> None:
        assert isinstance(CALL_REPORT_RC_R_MAP, dict)

    def test_call_report_map_nonempty(self) -> None:
        assert len(CALL_REPORT_RC_R_MAP) >= 3

    def test_asc326_required_fields_list(self) -> None:
        assert isinstance(ASC_326_REQUIRED_FIELDS, list)
        assert len(ASC_326_REQUIRED_FIELDS) >= 5


@pytest.fixture(scope="module")
def valid_cecl() -> dict:
    e = RegulatoryExporter(institution_id="VAL001", reporting_period="2024-Q4")
    result = e.export_cecl(format="json")
    assert isinstance(result, dict)
    return result


@pytest.fixture(scope="module")
def valid_ccar() -> dict:
    e = RegulatoryExporter(institution_id="VAL001", reporting_period="2024-Q4")
    result = e.export_ccar(format="json")
    assert isinstance(result, dict)
    return result


class TestSchemaValidation:
    def test_valid_cecl_export_passes(self, valid_cecl: dict) -> None:
        missing = validate_cecl_schema(valid_cecl)
        assert missing == [], f"Missing CECL fields: {missing}"

    def test_valid_ccar_export_passes(self, valid_ccar: dict) -> None:
        missing = validate_ccar_schema(valid_ccar)
        assert missing == [], f"Missing CCAR fields: {missing}"

    def test_missing_field_caught(self) -> None:
        broken = {"framework": "CECL"}
        missing = validate_cecl_schema(broken)
        assert len(missing) > 0

    def test_validate_returns_list(self) -> None:
        result = validate_cecl_schema({})
        assert isinstance(result, list)

    def test_all_required_cecl_fields_in_export(self, valid_cecl: dict) -> None:
        for field in ASC_326_REQUIRED_FIELDS:
            assert field in valid_cecl, f"Missing: {field}"

    def test_all_required_ccar_fields_in_export(self, valid_ccar: dict) -> None:
        required = [
            "framework",
            "institution_id",
            "reporting_period",
            "fr_y14a_fields",
            "scenario_results",
            "call_report_rc_r",
            "model_governance",
        ]
        for field in required:
            assert field in valid_ccar, f"Missing: {field}"
