"""Verify that METRICS.md covers all QualityMetrics output fields.

METRICS.md is auto-generated by ``ayase modules docs``. This test ensures
every module-written QualityMetrics field appears somewhere in the generated
doc, and every QualityMetrics field is accounted for.
"""

from pathlib import Path

from ayase.models import QualityMetrics


def _load_metrics_md_fields():
    """Extract all backtick-quoted field names from METRICS.md output columns."""
    metrics_path = Path("METRICS.md")
    if not metrics_path.exists():
        return set()
    text = metrics_path.read_text(encoding="utf-8")
    import re
    # Match field names in output column: `field_name` - description
    return set(re.findall(r"`(\w+)`\s*-\s*", text))


def test_metrics_table_matches_quality_metrics():
    """Verify METRICS.md references are consistent with QualityMetrics model."""
    fields_in_doc = _load_metrics_md_fields()
    model_fields = set(QualityMetrics.model_fields.keys())

    # Every field mentioned in METRICS.md must exist in QualityMetrics
    unknown = fields_in_doc - model_fields
    # Filter out non-field references (e.g. config keys)
    unknown = {f for f in unknown if not any(
        kw in f for kw in ("threshold", "model", "subsample", "device", "mode")
    )}
    assert not unknown, f"METRICS.md references unknown fields: {unknown}"

    # QualityMetrics field count must be correct
    assert len(model_fields) == 354, f"Expected 354 fields, got {len(model_fields)}"
