#!/usr/bin/env -S uv run -q --script
import ast
import pathlib


def check_file(path: pathlib.Path) -> list[str]:
    errors = []
    with open(path) as f:
        tree = ast.parse(f.read(), filename=str(path))
    for node in ast.walk(tree):
        if isinstance(node, ast.ImportFrom):  # $ and node.level == 0:
            if (
                node.module == "__future__"
                or node.module == "context"
                or node.module is None
                or str(path).endswith("__init__.py")
            ):
                continue
            errors.append(
                f"{path}:{node.lineno} — use `import {node.module}` instead of `from {node.module} import ...`"
            )
    return errors


if __name__ == "__main__":
    import argparse

    parser = argparse.ArgumentParser()
    parser.add_argument("files", nargs="*", type=pathlib.Path)
    args = parser.parse_args()

    errors = []
    if args.files:
        for path in args.files:
            errors.extend(check_file(path))
    else:
        for path in pathlib.Path(".").rglob("*.py"):
            if "venv" in str(path) or str(path).startswith((".tox", "mutants/")):
                continue
            errors.extend(check_file(path))

    if errors:
        print("\n".join(errors))
        raise SystemExit(1)
