#!/usr/bin/env python3
"""
Pre-commit hook to prevent removing parentheses from utcnow() in SQLAlchemy
Column defaults.

utcnow (from sqlalchemy_utc) is a FunctionElement *class*, not a plain
Python function.  ``default=utcnow()`` creates a SQL expression object that
SQLAlchemy renders inline per-INSERT (e.g. CURRENT_TIMESTAMP).  Passing the
bare class ``default=utcnow`` causes SQLAlchemy to call it as a Python
callable and then try to bind the resulting FunctionElement as a parameter
value, raising TypeError at insert time.

Correct:   default=utcnow()    onupdate=utcnow()    server_default=utcnow()
Wrong:     default=utcnow[no parens]      onupdate=utcnow[no parens]
"""

import re
import sys

# Match default=utcnow or onupdate=utcnow NOT followed by (
# This catches: default=utcnow, default=utcnow) default=utcnow\n
_BAD_PATTERN = re.compile(r"\b(default|onupdate)\s*=\s*utcnow\s*(?=[,\)\s\n])")


def main() -> int:
    exit_code = 0
    for filepath in sys.argv[1:]:
        try:
            # bearer:disable python_lang_path_using_user_input
            # Pre-commit hook: paths come from the pre-commit framework
            # (staged files in this repo), not from external user input.
            with open(filepath, encoding="utf-8") as f:
                lines = f.readlines()
        except Exception:
            continue

        for i, line in enumerate(lines, 1):
            # Skip comment and docstring lines — a note like
            # "# default=utcnow is wrong" should not fire the check.
            if line.lstrip().startswith(("#", '"""', "'''")):
                continue
            if _BAD_PATTERN.search(line):
                print(
                    f"{filepath}:{i}: utcnow without parentheses — "
                    f"use default=utcnow() or onupdate=utcnow(). "
                    f"utcnow is a FunctionElement class; the parens "
                    f"create a SQL expression object, not a frozen value."
                )
                exit_code = 1

    if exit_code:
        print()
        print(
            "Hint: utcnow() is correct — it creates a SQL expression "
            "(CURRENT_TIMESTAMP) evaluated per-INSERT by the database."
        )

    return exit_code


if __name__ == "__main__":
    sys.exit(main())
