#!/usr/bin/env python3
"""Enforce the database layer naming convention.

Rules checked (see the commit series "database naming cleanup"):

  1. every public function's name starts with the name of the file it lives in
  2. the file's own name uses a prefix from the closed set, or is exempt
  3. iterate_ iff the function takes a caller-supplied callback; get_ iff not
  4. a .h exists beside every .c, and a .sql (if any) shares the name
  5. every prepared statement is named after the function preparing it
  6. no prepared statement name is used in two files
  7. no numeric suffixes and no repeated library prefix

Run from the top of the source tree; exits non-zero on any violation.
"""
import re
import sys
import pathlib
import collections

LAYERS = [
    ("src/exchangedb", "src/include/exchange-database", "TALER_EXCHANGEDB_"),
    ("src/auditordb", "src/include/auditor-database", "TALER_AUDITORDB_"),
]

# Prefixes that describe what the caller gets.
PREFIXES = ("get_", "get_count_", "get_exists_", "iterate_",
            "insert_", "update_", "update_to_", "delete_", "do_")

# Not CRUD: transaction control, DDL, events, memory, session plumbing,
# cancellable handle APIs, shard lifecycle, and multi-function helper modules.
EXEMPT = {
    "start", "start_read_only", "start_read_committed",
    "start_deferred_wire_out", "commit", "rollback", "preflight", "disconnect",
    "create_tables", "drop_tables", "inject_auditor_triggers", "gc",
    "event_listen", "event_listen_cancel", "event_notify",
    "free_coin_transaction_list", "free_reserve_history",
    "compute_shard", "enable_rules", "disable_rules", "begin_rule_update",
    "begin_shard", "begin_revolving_shard", "complete_shard", "abort_shard",
    "release_revolving_shard",
    "account_history", "exchangedb_accounts", "exchangedb_transactions",
}

# Files that are not part of the API surface at all.
SKIP = {"pg", "template", "helper", "bench_db"}

PREP = re.compile(r'PREPARE\s*\(\s*\w+\s*,\s*"([A-Za-z0-9_]+)"', re.S)

errors = []


def check_layer(impl_dir, hdr_dir, prefix):
    impl, hdr = pathlib.Path(impl_dir), pathlib.Path(hdr_dir)
    if not impl.is_dir():
        return
    decl = re.compile(r"^" + re.escape(prefix) + r"(\w+)\s*\((.*?)\)\s*;",
                      re.S | re.M)
    stmt_owner = {}

    for c in sorted(impl.glob("*.c")):
        stem = c.stem
        if stem in SKIP or stem.startswith("test_"):
            continue

        # 2. prefix from the closed set, or exempt
        if stem not in EXEMPT and not stem.startswith(PREFIXES):
            errors.append(f"{c}: '{stem}' uses no approved prefix")

        # 7. numeric suffix
        if re.search(r"\d$", stem):
            errors.append(f"{c}: '{stem}' ends in a digit")

        # 4. matching header
        h = hdr / (stem + ".h")
        if stem not in EXEMPT and not h.exists():
            errors.append(f"{c}: no matching header {h}")

        text = c.read_text(errors="replace")

        # 5. prepared statements named after the function
        for s in dict.fromkeys(PREP.findall(text)):
            if s != stem and not s.startswith(stem + "_"):
                errors.append(
                    f"{c}: prepared statement '{s}' is not named after '{stem}'")
            # 6. cross-file uniqueness
            if s in stmt_owner and stmt_owner[s] != str(c):
                errors.append(
                    f"{c}: prepared statement '{s}' also prepared in "
                    f"{stmt_owner[s]}")
            stmt_owner[s] = str(c)

        if not h.exists():
            continue
        htext = re.sub(r"/\*.*?\*/", "", h.read_text(errors="replace"),
                       flags=re.S)
        for name, params in decl.findall(htext):
            # 1. declared names belong to their file.  Exempt entries are the
            # documented multi-function modules, so this does not apply there.
            if (stem not in EXEMPT
                    and name != stem and not name.startswith(stem + "_")):
                errors.append(
                    f"{h}: declares '{name}', which does not belong to "
                    f"'{stem}'")
            # 3. iterate_ iff callback
            has_cb = bool(re.search(r"\w*(Callback|Iterator)\s+\w+", params))
            if name.startswith("iterate_") and not has_cb:
                errors.append(f"{h}: '{name}' is iterate_ but takes no callback")
            if name.startswith("get_") and has_cb:
                errors.append(f"{h}: '{name}' takes a callback; use iterate_")

    # 7. repeated library prefix anywhere in the tree
    for f in list(impl.glob("*.[ch]")) + list(hdr.glob("*.h")):
        if "unc-backup" in f.name:
            continue
        if re.search(r"\b" + re.escape(prefix.split("_")[0]) + r"_" +
                     re.escape(prefix), f.read_text(errors="replace")):
            errors.append(f"{f}: repeated '{prefix}' prefix")


for a in LAYERS:
    check_layer(*a)

for e in errors:
    print("ERROR:", e)
print(f"\ndb-naming: {len(errors)} violation(s)")
sys.exit(1 if errors else 0)
