# allow: no-sut-import — guardian; sweeps src/ to forbid printf-style placeholders in loguru calls
import ast
from pathlib import Path
import re


PRINTF_PLACEHOLDER_RE = re.compile(
    r"%(?:\([^)]+\))?[#0 +\-]*\d*(?:\.\d+)?[sdfr]"
)
LOGURU_METHODS = {
    "trace",
    "debug",
    "info",
    "success",
    "warning",
    "error",
    "critical",
    "exception",
    "log",
}


def test_loguru_calls_do_not_use_printf_style_placeholders():
    """Guard direct `loguru` logger calls against stdlib printf formatting."""
    src_root = Path(__file__).resolve().parents[2] / "src"
    failures = []

    for path in src_root.rglob("*.py"):
        source = path.read_text()
        tree = ast.parse(source)

        imports_loguru_logger = any(
            isinstance(node, ast.ImportFrom)
            and node.module == "loguru"
            and any(alias.name == "logger" for alias in node.names)
            for node in tree.body
        )
        if not imports_loguru_logger:
            continue

        for node in ast.walk(tree):
            if not (
                isinstance(node, ast.Call)
                and isinstance(node.func, ast.Attribute)
                and isinstance(node.func.value, ast.Name)
                and node.func.value.id == "logger"
                and node.func.attr in LOGURU_METHODS
            ):
                continue

            method_name = node.func.attr
            message_index = 1 if method_name == "log" else 0
            min_args = 3 if method_name == "log" else 2
            if len(node.args) < min_args:
                continue

            message_arg = node.args[message_index]
            if not (
                isinstance(message_arg, ast.Constant)
                and isinstance(message_arg.value, str)
                and PRINTF_PLACEHOLDER_RE.search(message_arg.value)
            ):
                continue

            failures.append(
                f"{path.relative_to(src_root.parent)}:{node.lineno} -> "
                f"{message_arg.value!r}"
            )

    assert not failures, (
        "found loguru calls using printf-style placeholders:\n"
        + "\n".join(sorted(failures))
    )
