#!/usr/bin/env python3
"""Bank batch-248: Rule 206 negated low-frequency property consolidation."""

from __future__ import annotations

import datetime as dt
import json
import re
import sqlite3

from process_ledger import ensure_schema, record_event

DB = "/mnt/donto-data/donto-resources/research/translation-training/kuku-yalanji-runpod-2026-06-30/synthetic/claude-synthetic-v1-2026-07-02/synthetic.db"
LABEL = "batch-248"
NOW = dt.datetime.now(dt.timezone.utc).replace(microsecond=0).isoformat().replace("+00:00", "Z")
PROCESS_LOG = "process_logs/batch-248-negated-low-frequency-property-consolidation-after-rule205-process.md"

TARGET_HEADS = {
    "barrangkul": "flat",
    "jalngka": "smooth",
    "kurru-kurru": "crooked",
    "darra": "narrow",
    "yalmbar": "wide",
    "kiray": "quiet",
    "murru": "round",
    "murru-murru": "lumpy",
    "muru-muru": "close together",
    "manun-manun": "kind",
}
SUBJECT_HEADS = {"bubu", "juku", "baral", "kambi", "kulji", "bama"}
SUPPORT = {"kari", "yinya", "yaluy", "yinyay"} | SUBJECT_HEADS
DENY_KUKU = {
    "nyaji-l", "nyajil", "nyajin", "bajaku", "baja",
    "nuka", "nukal", "nukan", "waju", "wajul", "wajun", "mani", "manil", "manin",
    "dunga", "dungan", "dungay", "warri", "warriny", "daka", "dakany", "dakay",
    "kida", "kidal", "kidan", "yaka", "yakan", "yakay", "kuni", "kunil", "kunin",
    "ngara", "ngaran", "bunday", "bundanda", "yala", "yanyu",
    "kayal", "kabu", "jumbun", "jika", "murrba", "jalngkaji", "jalngkay",
}
DENY_ENGLISH = (
    "without", "very", "more", "rather", "too", "hear", "heard", "listen", "see", "saw", "feel", "felt",
    "itch", "itchy", "eat", "eating", "cook", "cooking", "cooked", "raw", "unripe", "gather", "gathering",
    "repair", "travel", "walk", "go", "going", "move", "moving", "secret", "secretly", "country", "owner",
    "ownership", "money", "coin", "ill", "illness", "sick", "body", "pain", "source", "use", "using",
    "because", "make", "making", "put", "putting",
)


def js(values: list[str]) -> str:
    return json.dumps(values, ensure_ascii=False)


def row(
    seq: int,
    english: str,
    kuku: str,
    analysis: str,
    frame: str,
    words: list[str],
    evidence: str,
) -> tuple:
    return (seq, english, kuku, analysis, frame, 3, js(words), js([]), evidence, "0.86")


ROWS = [
    row(1, "The ground is not flat.", "Bubu kari barrangkul.", "bubu kari barrangkul | ground NEG flat", "r206_np_kari_adj_bubu_barrangkul", ["bubu", "kari", "barrangkul"], "Patz adjective negation permits kari before adjective; bubu ground only."),
    row(2, "The wood is not smooth.", "Juku kari jalngka.", "juku kari jalngka | wood NEG smooth", "r206_np_kari_adj_juku_jalngka", ["juku", "kari", "jalngka"], "Jalngka smooth adjective only; itchy noun homonym closed."),
    row(3, "The road is not crooked.", "Baral kari kurru-kurru.", "baral kari kurru-kurru | road NEG crooked", "r206_np_kari_adj_baral_kurru_kurru", ["baral", "kari", "kurru-kurru"], "Baral road/path noun only; no travel event."),
    row(4, "The clothing is not narrow.", "Kambi kari darra.", "kambi kari darra | clothing NEG narrow", "r206_np_kari_adj_kambi_darra", ["kambi", "kari", "darra"], "Darra narrow/too small property only; no degree."),
    row(5, "The road is not wide.", "Baral kari yalmbar.", "baral kari yalmbar | road NEG wide", "r206_np_kari_adj_baral_yalmbar", ["baral", "kari", "yalmbar"], "Yalmbar wide property only; no travel."),
    row(6, "The person is not quiet.", "Bama kari kiray.", "bama kari kiray | person NEG quiet", "r206_np_kari_adj_bama_kiray", ["bama", "kari", "kiray"], "Kiray selected as quiet property; no motion or secrecy adverb."),
    row(7, "The pebble is not round.", "Kulji kari murru.", "kulji kari murru | pebble NEG round", "r206_np_kari_adj_kulji_murru", ["kulji", "kari", "murru"], "Kulji pebble/stone only; murru round property."),
    row(8, "The ground is not lumpy.", "Bubu kari murru-murru.", "bubu kari murru-murru | ground NEG lumpy", "r206_np_kari_adj_bubu_murru_murru", ["bubu", "kari", "murru-murru"], "Murru-murru lexicalized lumpy adjective only."),
    row(9, "The stones are not close together.", "Kulji kari muru-muru.", "kulji kari muru-muru | stones NEG close-together", "r206_np_kari_adj_kulji_muru_muru", ["kulji", "kari", "muru-muru"], "Kulji plural by context only; no plural morphology."),
    row(10, "The person is not kind.", "Bama kari manun-manun.", "bama kari manun-manun | person NEG kind", "r206_np_kari_adj_bama_manun_manun", ["bama", "kari", "manun-manun"], "Manun-manun character property only; no social event."),
    row(11, "That ground is not flat.", "Yinya bubu kari barrangkul.", "yinya bubu kari barrangkul | that ground NEG flat", "r206_yinya_np_kari_adj_bubu_barrangkul", ["yinya", "bubu", "kari", "barrangkul"], "Yinya NP ADJ plus independent kari."),
    row(12, "That wood is not smooth.", "Yinya juku kari jalngka.", "yinya juku kari jalngka | that wood NEG smooth", "r206_yinya_np_kari_adj_juku_jalngka", ["yinya", "juku", "kari", "jalngka"], "Yinya support; no smoothing verb."),
    row(13, "That road is not crooked.", "Yinya baral kari kurru-kurru.", "yinya baral kari kurru-kurru | that road NEG crooked", "r206_yinya_np_kari_adj_baral_kurru_kurru", ["yinya", "baral", "kari", "kurru-kurru"], "Yinya support; baral noun only."),
    row(14, "That clothing is not narrow.", "Yinya kambi kari darra.", "yinya kambi kari darra | that clothing NEG narrow", "r206_yinya_np_kari_adj_kambi_darra", ["yinya", "kambi", "kari", "darra"], "Yinya support; no degree."),
    row(15, "That road is not wide.", "Yinya baral kari yalmbar.", "yinya baral kari yalmbar | that road NEG wide", "r206_yinya_np_kari_adj_baral_yalmbar", ["yinya", "baral", "kari", "yalmbar"], "Yinya support; no route event."),
    row(16, "That person is not quiet.", "Yinya bama kari kiray.", "yinya bama kari kiray | that person NEG quiet", "r206_yinya_np_kari_adj_bama_kiray", ["yinya", "bama", "kari", "kiray"], "Yinya support; no motion/secrecy."),
    row(17, "That pebble is not round.", "Yinya kulji kari murru.", "yinya kulji kari murru | that pebble NEG round", "r206_yinya_np_kari_adj_kulji_murru", ["yinya", "kulji", "kari", "murru"], "Yinya support; no money reading."),
    row(18, "That ground is not lumpy.", "Yinya bubu kari murru-murru.", "yinya bubu kari murru-murru | that ground NEG lumpy", "r206_yinya_np_kari_adj_bubu_murru_murru", ["yinya", "bubu", "kari", "murru-murru"], "Yinya support; lexicalized target only."),
    row(19, "Those stones are not close together.", "Yinya kulji kari muru-muru.", "yinya kulji kari muru-muru | those stones NEG close-together", "r206_yinya_np_kari_adj_kulji_muru_muru", ["yinya", "kulji", "kari", "muru-muru"], "Yinya plural by context only; no plural morphology."),
    row(20, "That person is not kind.", "Yinya bama kari manun-manun.", "yinya bama kari manun-manun | that person NEG kind", "r206_yinya_np_kari_adj_bama_manun_manun", ["yinya", "bama", "kari", "manun-manun"], "Character property only; no social event."),
    row(21, "The ground here is not flat.", "Bubu kari barrangkul yaluy.", "bubu kari barrangkul yaluy | ground NEG flat here", "r206_np_kari_adj_here_bubu_barrangkul", ["bubu", "kari", "barrangkul", "yaluy"], "Yaluy support plus independent kari."),
    row(22, "The wood here is not smooth.", "Juku kari jalngka yaluy.", "juku kari jalngka yaluy | wood NEG smooth here", "r206_np_kari_adj_here_juku_jalngka", ["juku", "kari", "jalngka", "yaluy"], "Local support; no itchy/body-state reading."),
    row(23, "The road here is not crooked.", "Baral kari kurru-kurru yaluy.", "baral kari kurru-kurru yaluy | road NEG crooked here", "r206_np_kari_adj_here_baral_kurru_kurru", ["baral", "kari", "kurru-kurru", "yaluy"], "Local support; no travel."),
    row(24, "The clothing here is not narrow.", "Kambi kari darra yaluy.", "kambi kari darra yaluy | clothing NEG narrow here", "r206_np_kari_adj_here_kambi_darra", ["kambi", "kari", "darra", "yaluy"], "Local support; no degree."),
    row(25, "The road here is not wide.", "Baral kari yalmbar yaluy.", "baral kari yalmbar yaluy | road NEG wide here", "r206_np_kari_adj_here_baral_yalmbar", ["baral", "kari", "yalmbar", "yaluy"], "Local support; no route event."),
    row(26, "The person here is not quiet.", "Bama kari kiray yaluy.", "bama kari kiray yaluy | person NEG quiet here", "r206_np_kari_adj_here_bama_kiray", ["bama", "kari", "kiray", "yaluy"], "Local support; no secrecy adverb."),
    row(27, "The pebble here is not round.", "Kulji kari murru yaluy.", "kulji kari murru yaluy | pebble NEG round here", "r206_np_kari_adj_here_kulji_murru", ["kulji", "kari", "murru", "yaluy"], "Local support; kulji stone only."),
    row(28, "The ground here is not lumpy.", "Bubu kari murru-murru yaluy.", "bubu kari murru-murru yaluy | ground NEG lumpy here", "r206_np_kari_adj_here_bubu_murru_murru", ["bubu", "kari", "murru-murru", "yaluy"], "Local support; lexicalized lumpy adjective."),
    row(29, "The stones here are not close together.", "Kulji kari muru-muru yaluy.", "kulji kari muru-muru yaluy | stones NEG close-together here", "r206_np_kari_adj_here_kulji_muru_muru", ["kulji", "kari", "muru-muru", "yaluy"], "Local support; no plural morphology."),
    row(30, "The person here is not kind.", "Bama kari manun-manun yaluy.", "bama kari manun-manun yaluy | person NEG kind here", "r206_np_kari_adj_here_bama_manun_manun", ["bama", "kari", "manun-manun", "yaluy"], "Local support; no social event."),
    row(31, "The ground there is not flat.", "Bubu kari barrangkul yinyay.", "bubu kari barrangkul yinyay | ground NEG flat there", "r206_np_kari_adj_there_bubu_barrangkul", ["bubu", "kari", "barrangkul", "yinyay"], "Yinyay support plus independent kari."),
    row(32, "The wood there is not smooth.", "Juku kari jalngka yinyay.", "juku kari jalngka yinyay | wood NEG smooth there", "r206_np_kari_adj_there_juku_jalngka", ["juku", "kari", "jalngka", "yinyay"], "Yinyay support; no smoothing verb."),
    row(33, "The road there is not crooked.", "Baral kari kurru-kurru yinyay.", "baral kari kurru-kurru yinyay | road NEG crooked there", "r206_np_kari_adj_there_baral_kurru_kurru", ["baral", "kari", "kurru-kurru", "yinyay"], "Yinyay support; no route/travel row."),
    row(34, "The clothing there is not narrow.", "Kambi kari darra yinyay.", "kambi kari darra yinyay | clothing NEG narrow there", "r206_np_kari_adj_there_kambi_darra", ["kambi", "kari", "darra", "yinyay"], "Yinyay support; no degree/comparison."),
    row(35, "The road there is not wide.", "Baral kari yalmbar yinyay.", "baral kari yalmbar yinyay | road NEG wide there", "r206_np_kari_adj_there_baral_yalmbar", ["baral", "kari", "yalmbar", "yinyay"], "Yinyay support; no travel event."),
    row(36, "The person there is not quiet.", "Bama kari kiray yinyay.", "bama kari kiray yinyay | person NEG quiet there", "r206_np_kari_adj_there_bama_kiray", ["bama", "kari", "kiray", "yinyay"], "Yinyay support; no motion."),
    row(37, "The pebble there is not round.", "Kulji kari murru yinyay.", "kulji kari murru yinyay | pebble NEG round there", "r206_np_kari_adj_there_kulji_murru", ["kulji", "kari", "murru", "yinyay"], "Yinyay support; no money reading."),
    row(38, "The ground there is not lumpy.", "Bubu kari murru-murru yinyay.", "bubu kari murru-murru yinyay | ground NEG lumpy there", "r206_np_kari_adj_there_bubu_murru_murru", ["bubu", "kari", "murru-murru", "yinyay"], "Yinyay support; lexicalized target only."),
    row(39, "The stones there are not close together.", "Kulji kari muru-muru yinyay.", "kulji kari muru-muru yinyay | stones NEG close-together there", "r206_np_kari_adj_there_kulji_muru_muru", ["kulji", "kari", "muru-muru", "yinyay"], "Yinyay support; no plural morphology."),
    row(40, "The person there is not kind.", "Bama kari manun-manun yinyay.", "bama kari manun-manun yinyay | person NEG kind there", "r206_np_kari_adj_there_bama_manun_manun", ["bama", "kari", "manun-manun", "yinyay"], "Yinyay support; no social event."),
    row(41, "That ground there is not flat.", "Yinya bubu kari barrangkul yinyay.", "yinya bubu kari barrangkul yinyay | that ground NEG flat there", "r206_yinya_np_kari_adj_there_bubu_barrangkul", ["yinya", "bubu", "kari", "barrangkul", "yinyay"], "Yinya plus yinyay support."),
    row(42, "That wood there is not smooth.", "Yinya juku kari jalngka yinyay.", "yinya juku kari jalngka yinyay | that wood NEG smooth there", "r206_yinya_np_kari_adj_there_juku_jalngka", ["yinya", "juku", "kari", "jalngka", "yinyay"], "Yinya plus yinyay; no body-state homonym."),
    row(43, "That road there is not crooked.", "Yinya baral kari kurru-kurru yinyay.", "yinya baral kari kurru-kurru yinyay | that road NEG crooked there", "r206_yinya_np_kari_adj_there_baral_kurru_kurru", ["yinya", "baral", "kari", "kurru-kurru", "yinyay"], "Yinya plus yinyay; road noun only."),
    row(44, "That clothing there is not narrow.", "Yinya kambi kari darra yinyay.", "yinya kambi kari darra yinyay | that clothing NEG narrow there", "r206_yinya_np_kari_adj_there_kambi_darra", ["yinya", "kambi", "kari", "darra", "yinyay"], "Yinya plus yinyay; no degree."),
    row(45, "That road there is not wide.", "Yinya baral kari yalmbar yinyay.", "yinya baral kari yalmbar yinyay | that road NEG wide there", "r206_yinya_np_kari_adj_there_baral_yalmbar", ["yinya", "baral", "kari", "yalmbar", "yinyay"], "Yinya plus yinyay; no travel."),
    row(46, "That person there is not quiet.", "Yinya bama kari kiray yinyay.", "yinya bama kari kiray yinyay | that person NEG quiet there", "r206_yinya_np_kari_adj_there_bama_kiray", ["yinya", "bama", "kari", "kiray", "yinyay"], "Yinya plus yinyay; no secrecy adverb."),
    row(47, "That pebble there is not round.", "Yinya kulji kari murru yinyay.", "yinya kulji kari murru yinyay | that pebble NEG round there", "r206_yinya_np_kari_adj_there_kulji_murru", ["yinya", "kulji", "kari", "murru", "yinyay"], "Yinya plus yinyay; no money sense."),
    row(48, "That ground there is not lumpy.", "Yinya bubu kari murru-murru yinyay.", "yinya bubu kari murru-murru yinyay | that ground NEG lumpy there", "r206_yinya_np_kari_adj_there_bubu_murru_murru", ["yinya", "bubu", "kari", "murru-murru", "yinyay"], "Yinya plus yinyay; no productive reduplication."),
    row(49, "Those stones there are not close together.", "Yinya kulji kari muru-muru yinyay.", "yinya kulji kari muru-muru yinyay | those stones NEG close-together there", "r206_yinya_np_kari_adj_there_kulji_muru_muru", ["yinya", "kulji", "kari", "muru-muru", "yinyay"], "Yinya plus yinyay; plural context only."),
    row(50, "That person there is not kind.", "Yinya bama kari manun-manun yinyay.", "yinya bama kari manun-manun yinyay | that person NEG kind there", "r206_yinya_np_kari_adj_there_bama_manun_manun", ["yinya", "bama", "kari", "manun-manun", "yinyay"], "Yinya plus yinyay; character property only."),
]


def has_token(low: str, token: str) -> bool:
    return re.search(rf"(?<![a-z-]){re.escape(token)}(?![a-z-])", low) is not None


def has_target_morphology(low: str, head: str) -> bool:
    suffixes = "ngka|ngku|angka|nga|ngu|ba|mba|mbu|muny|darr|burr|bu|ka|nka|nku|ji|jirr|ku|mundu|amu|mu|iji|nda|ndu|anda|undu|unji|nji|kari|baka|baja|bungal|bungan|mal|man|rriny"
    return re.search(rf"(?<![a-z-]){re.escape(head)}-(?:{suffixes})\b", low) is not None


def english_has_denied(english_low: str, denied: str) -> bool:
    return denied in english_low if " " in denied else re.search(rf"\b{re.escape(denied)}\b", english_low) is not None


def scan_static() -> list[str]:
    issues: list[str] = []
    if len(ROWS) != 50:
        issues.append(f"{LABEL}: expected 50 rows, found {len(ROWS)}")
    seen_ku: set[str] = set()
    for expected, row_data in enumerate(ROWS, start=1):
        seq, english, kuku, analysis, frame, _tier, words_used, loans_used, evidence, confidence = row_data
        label = f"{LABEL}.{seq:02d}"
        low = kuku.lower()
        english_low = english.lower()
        words = json.loads(words_used)
        if seq != expected:
            issues.append(f"{label}: sequence mismatch; expected {expected}")
        if not english[:1].isupper() or english[-1:] not in ".!?":
            issues.append(f"{label}: English punctuation/capitalization failure")
        if " | " not in analysis:
            issues.append(f"{label}: analysis missing gloss separator")
        if not frame.startswith("r206_"):
            issues.append(f"{label}: Rule 206 frame must start with r206_")
        if low in seen_ku:
            issues.append(f"{label}: duplicate Kuku surface within batch")
        seen_ku.add(low)
        if json.loads(loans_used):
            issues.append(f"{label}: no loans are admitted in Rule 206")
        if "-kari" in low:
            issues.append(f"{label}: privative -kari is forbidden in Rule 206")
        if not has_token(low, "kari"):
            issues.append(f"{label}: every Rule 206 row requires independent kari")
        for denied in DENY_KUKU:
            if has_token(low, denied):
                issues.append(f"{label}: denied carry-forward token {denied!r}")
        targets = [head for head in TARGET_HEADS if has_token(low, head)]
        if len(targets) != 1:
            issues.append(f"{label}: expected exactly one Rule 206 target adjective, found {targets}")
        subjects = [head for head in SUBJECT_HEADS if has_token(low, head)]
        if len(subjects) != 1:
            issues.append(f"{label}: expected exactly one support subject noun, found {subjects}")
        for head in targets:
            selected = TARGET_HEADS[head]
            if head not in words:
                issues.append(f"{label}: target head {head} missing from words_used")
            if selected not in english_low:
                issues.append(f"{label}: English must include selected sense {selected!r}")
            if selected.replace(" ", "-") not in analysis and selected not in analysis:
                issues.append(f"{label}: analysis must gloss {head} as {selected}")
            if has_target_morphology(low, head):
                issues.append(f"{label}: target head {head} has forbidden morphology")
        for support in SUPPORT:
            if has_token(low, support) and support not in words:
                issues.append(f"{label}: support {support} missing from words_used")
        if 1 <= seq <= 10 and (has_token(low, "yinya") or has_token(low, "yaluy") or has_token(low, "yinyay")):
            issues.append(f"{label}: rows 1-10 require NP kari ADJ")
        if 11 <= seq <= 20 and (not low.startswith("yinya ") or has_token(low, "yaluy") or has_token(low, "yinyay")):
            issues.append(f"{label}: rows 11-20 require Yinya NP kari ADJ")
        if 21 <= seq <= 30 and (has_token(low, "yinya") or has_token(low, "yinyay") or not low.endswith(" yaluy.")):
            issues.append(f"{label}: rows 21-30 require NP kari ADJ yaluy")
        if 31 <= seq <= 40 and (has_token(low, "yinya") or has_token(low, "yaluy") or not low.endswith(" yinyay.")):
            issues.append(f"{label}: rows 31-40 require NP kari ADJ yinyay")
        if 41 <= seq <= 50 and (not low.startswith("yinya ") or has_token(low, "yaluy") or not low.endswith(" yinyay.")):
            issues.append(f"{label}: rows 41-50 require Yinya NP kari ADJ yinyay")
        if any(english_has_denied(english_low, bad) for bad in DENY_ENGLISH):
            issues.append(f"{label}: denied action/event/source/body-state field in English")
        if "not " not in english_low:
            issues.append(f"{label}: English must express negation with 'not'")
        if not evidence or not confidence:
            issues.append(f"{label}: missing evidence/confidence")
    return issues


def main() -> int:
    issues = scan_static()
    if issues:
        print("pre-bank scan failed:")
        for issue in issues:
            print(f"  - {issue}")
        return 1

    db = sqlite3.connect(DB)
    db.execute("PRAGMA foreign_keys = ON")
    db.execute("BEGIN")
    ensure_schema(db)
    existing = db.execute("SELECT id FROM batches WHERE label=?", (LABEL,)).fetchone()
    if existing:
        batch_id = existing[0]
        accepted = db.execute(
            "SELECT COUNT(*) FROM sentences WHERE batch_id=? AND status IN ('accepted','revised')",
            (batch_id,),
        ).fetchone()[0]
        if accepted:
            raise SystemExit(f"{LABEL} already has accepted/revised rows; refusing to overwrite")
        db.execute("DELETE FROM reviews WHERE sentence_id IN (SELECT id FROM sentences WHERE batch_id=?)", (batch_id,))
        db.execute("DELETE FROM sentences WHERE batch_id=?", (batch_id,))
        db.execute("UPDATE batches SET notes=?, created_at=? WHERE id=?", ("rebanked under_review", NOW, batch_id))
    else:
        cur = db.execute(
            "INSERT INTO batches(label, cheatsheet_version, guide_version, notes, created_at) VALUES(?,?,?,?,?)",
            (
                LABEL,
                "Rule 206 draft: negated low-frequency property construction consolidation after Rule 205",
                "operator-guide-2026-07-08",
                "under_review",
                NOW,
            ),
        )
        batch_id = cur.lastrowid

    dupes: list[str] = []
    for seq, _english, kuku, *_rest in ROWS:
        hit = db.execute(
            """
            SELECT b.label, s.seq
            FROM sentences s JOIN batches b ON b.id=s.batch_id
            WHERE lower(trim(s.kuku))=lower(trim(?))
              AND s.status!='rejected'
              AND s.batch_id!=?
            """,
            (kuku, batch_id),
        ).fetchone()
        if hit:
            dupes.append(f"{LABEL}.{seq:02d} duplicates {hit[0]}.{hit[1]:02d}: {kuku}")
    if dupes:
        db.rollback()
        print("cross-corpus duplicate check failed:")
        for dupe in dupes:
            print(f"  - {dupe}")
        return 1

    db.executemany(
        """
        INSERT INTO sentences(
          batch_id, seq, english, kuku, analysis, frame, tier, words_used,
          loans_used, evidence, status, confidence, author, created_at
        )
        VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?)
        """,
        [
            (batch_id, *row_data[:-1], "under_review", row_data[-1], "claude", NOW)
            for row_data in ROWS
        ],
    )
    verified_before, words_before = db.execute(
        """
        SELECT COUNT(*), SUM(LENGTH(kuku)-LENGTH(REPLACE(kuku,' ',''))+1)
        FROM sentences
        WHERE status IN ('accepted','revised')
        """
    ).fetchone()
    record_event(
        db,
        batch_label=LABEL,
        phase="source",
        event_type="source-pass",
        actor="claude-main",
        detail="Checked Patz adjective-property and negated-adjective grammar plus dictionary entries for retained Rule 205 targets, independent kari, and support nouns/demonstratives.",
        command="sed/rg dictionary.yaml and grammar.md around target, support, and negation passages",
        artifact_path=PROCESS_LOG,
        created_at=NOW,
    )
    record_event(
        db,
        batch_label=LABEL,
        phase="scope",
        event_type="construction-scope",
        actor="claude-main",
        detail="Rule 206 admits only independent pre-adjective kari negation in NP kari ADJ, Yinya NP kari ADJ, NP kari ADJ yaluy, NP kari ADJ yinyay, and Yinya NP kari ADJ yinyay frames; excludes privative ADJ-kari, degree, comparison, case, derivation, perception, events, source/use, illness/body-state, social events, homograph drift, and productive reduplication.",
        artifact_path=PROCESS_LOG,
        created_at=NOW,
    )
    record_event(
        db,
        batch_label=LABEL,
        phase="preflight",
        event_type="duplicate-preflight",
        actor="claude-main",
        detail="Exact duplicate preflight over all 50 planned Rule 206 Kuku surfaces returned no accepted, revised, or under-review collisions.",
        command="sqlite3 duplicate-preflight over planned surfaces",
        output_excerpt="No collisions.",
        artifact_path=PROCESS_LOG,
        created_at=NOW,
    )
    record_event(
        db,
        batch_label=LABEL,
        phase="preflight",
        event_type="static-guard-pass",
        actor="claude-main",
        detail="bank_248.py static guards passed: 50 explicit rows, exactly one target adjective and one support subject per row, required independent kari, no -kari, no denied Kuku/English fields, no target morphology, correct frame bands, no loans.",
        artifact_path="batch_scripts/bank_248.py",
        created_at=NOW,
    )
    db.commit()
    print(f"banked {LABEL}: rows=50, under_review=50, verified_before_close={verified_before}, words_before={words_before}")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())
