"""Regression test — no CSS gradients across RIMS templates.

Locked design-system rule (CLAUDE.md / project memory): RIMS templates must
not introduce ``bg-gradient-to-*`` Tailwind classes or raw ``linear-gradient``
calls in inline ``<style>`` blocks. This test fails the build if a future PR
re-introduces a gradient.

Functional SVG ``<linearGradient>`` elements (used in icon decorations) are
exempt — they are SVG paint, not CSS.
"""
from __future__ import annotations

import re
from pathlib import Path

import pytest

_RIMS_TEMPLATES_DIR = Path(__file__).resolve().parents[1]
# linear-gradient(...) in inline CSS, plus Tailwind utility variants.
_GRADIENT_PATTERNS = [
    re.compile(r"linear-gradient\("),
    re.compile(r"\bbg-gradient-to-[bltr]"),
    re.compile(r"\bbg-gradient-to-(?:tr|tl|br|bl)"),
]


def _iter_template_files() -> list[Path]:
    return sorted(_RIMS_TEMPLATES_DIR.rglob("templates/**/*.html"))


@pytest.mark.parametrize("pattern", _GRADIENT_PATTERNS)
def test_rims_templates_have_no_css_gradients(pattern):
    offenders: list[tuple[Path, int, str]] = []
    for path in _iter_template_files():
        try:
            text = path.read_text(encoding="utf-8")
        except (OSError, UnicodeDecodeError):
            continue
        for line_no, line in enumerate(text.splitlines(), start=1):
            if pattern.search(line):
                offenders.append((path, line_no, line.strip()))
    if offenders:
        formatted = "\n".join(
            f"  {p.relative_to(_RIMS_TEMPLATES_DIR.parent.parent)}:{n}: {snippet[:120]}"
            for p, n, snippet in offenders
        )
        pytest.fail(
            f"{pattern.pattern} matched in {len(offenders)} RIMS template line(s):\n{formatted}"
        )
