#!/usr/bin/env python3
"""Bank batch-337: Rule 295 visible water-fauna singular/third nonpast perception."""

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-337"
NOW = dt.datetime.now(dt.timezone.utc).replace(microsecond=0).isoformat().replace("+00:00", "Z")
PROCESS_LOG = "process_logs/batch-337-visible-water-fauna-singular-third-nonpast-perception-after-rule294-process.md"

TARGET_EVIDENCE = {
    "ngawiya": "Core lexicon line 744 gives ngawiya as 'sea turtle', neutral, with a hunted-food note. Rule 295 selects visible sea-turtle reference only and leaves hunting, turtle-spearing, cooking, and eating rows closed.",
    "ngujay": "Core lexicon line 745 gives ngujay as 'freshwater turtle', neutral. Rule 295 selects visible freshwater-turtle reference only.",
    "kuyu": "Core lexicon line 751 gives kuyu as 'fish', neutral as food/object. Rule 295 selects visible generic-fish reference only and leaves catching, spearing, bait, cooking, and eating rows closed.",
    "yawu": "Core lexicon line 753 gives yawu as 'stingray', ambiguous. Rule 295 selects visible stingray reference only and leaves danger, hunting, spearing, and catch rows closed.",
    "judi": "Core lexicon line 754 gives judi as 'barramundi' and identifies it as the Yalanji form. Rule 295 selects visible barramundi reference only.",
    "ngalkun": "Core lexicon line 755 gives ngalkun as 'mullet', neutral. Rule 295 selects visible mullet reference only.",
    "kubarr": "Core lexicon line 756 gives kubarr as 'big black freshwater eel', neutral. Rule 295 selects visible freshwater-eel reference only and does not open colour/property rows.",
    "kiju": "Core lexicon line 757 gives kiju as 'mudcrab', neutral. Rule 295 selects visible mudcrab reference only and leaves biting, catching, and cooking rows closed.",
    "wakal": "Core lexicon line 758 gives wakal as 'prawn, lobster', neutral. Rule 295 selects visible prawn reference only and leaves catching, cooking, and eating rows closed.",
    "julul": "Core lexicon line 759 gives julul as 'oyster', neutral. Rule 295 selects visible oyster reference only and leaves gathering, eating, and shore/ecology rows closed.",
}

SUPPORT_EVIDENCE = (
    "Patz pronoun tables source bare nominative ngayu, yundu, nyulu, bula, and jana; local-demonstrative lines "
    "source yaluy 'here' and yinyay 'there'; Patz section 3.8.3.2 sources nyaji-l 'see', and the tense section "
    "sources nonpast -l. Rule 295 uses corpus-conventional nyajil as the nonpast visual-perception surface."
)

TARGET_ORDER = ["ngawiya", "ngujay", "kuyu", "yawu", "judi", "ngalkun", "kubarr", "kiju", "wakal", "julul"]
FRAME_SPECS = [
    ("ngayu", "yaluy"),
    ("yundu", "yinyay"),
    ("nyulu", "yaluy"),
    ("bula", "yinyay"),
    ("jana", "yaluy"),
]

DENY_KUKU = {
    "yuday", "kulil", "nuwi", "yaburr", "badibu",
    "ngawiyangka", "ngujayngka", "kuyubu", "yawubu", "judibu", "ngalkundu", "kubarrbu",
    "kijubu", "wakalbu", "jululbu", "ngawiyaka", "ngujayka", "kuyuka", "yawuka", "judika",
    "ngalkunka", "kubarrka", "kijuka", "wakalka", "jululka", "ngawiyanga", "ngujaynga",
    "kuyunga", "yawunga", "judinga", "ngalkunga", "kubarrnga", "kijunga", "wakalnga", "jululnga",
    "nganya", "yununy", "nyunguny", "bulangan", "janangan", "ngaykunku", "yununku",
    "nyungunku", "bulanka", "jananka", "ngaykundu", "yunundu", "nyungundu", "bulanda",
    "jananda", "yalungku", "yaluyngku", "yinyangka", "yinyayngka", "yalamuny", "yinyamuny",
    "nyajin", "nyaji-ny", "nyaji-nka", "nyaji-l-nyaji", "milka",
}
DENY_ENGLISH = (
    "catch", "caught", "catching", "spear", "spearing", "hunt", "hunting", "kill", "bite", "biting",
    "cook", "cooking", "roast", "eat", "eating", "food", "meat", "bait", "net", "hook", "danger",
    "dangerous", "story", "place", "reef", "shore", "swim", "bleeding", "black", "colour", "color",
    "gather", "gathering", "lobster",
)


def row(seq: int, english: str, kuku: str, analysis: str, frame: str, words: list[str]) -> tuple:
    target = words[1]
    evidence = f"{TARGET_EVIDENCE[target]} {SUPPORT_EVIDENCE}"
    return (seq, english, kuku, analysis, frame, 3, json.dumps(words, ensure_ascii=False), json.dumps([], ensure_ascii=False), evidence, "0.86")


ROWS = [
    row(1, "I see the sea turtle here.", "Ngayu ngawiya yaluy nyajil.", "ngayu ngawiya yaluy nyajil | 1sg.NOM sea.turtle here see.NONPAST", "r295_1sg_here_water_fauna_see_ngawiya", ["ngayu", "ngawiya", "yaluy", "nyajil"]),
    row(2, "I see the freshwater turtle here.", "Ngayu ngujay yaluy nyajil.", "ngayu ngujay yaluy nyajil | 1sg.NOM freshwater.turtle here see.NONPAST", "r295_1sg_here_water_fauna_see_ngujay", ["ngayu", "ngujay", "yaluy", "nyajil"]),
    row(3, "I see the fish here.", "Ngayu kuyu yaluy nyajil.", "ngayu kuyu yaluy nyajil | 1sg.NOM fish here see.NONPAST", "r295_1sg_here_water_fauna_see_kuyu", ["ngayu", "kuyu", "yaluy", "nyajil"]),
    row(4, "I see the stingray here.", "Ngayu yawu yaluy nyajil.", "ngayu yawu yaluy nyajil | 1sg.NOM stingray here see.NONPAST", "r295_1sg_here_water_fauna_see_yawu", ["ngayu", "yawu", "yaluy", "nyajil"]),
    row(5, "I see the barramundi here.", "Ngayu judi yaluy nyajil.", "ngayu judi yaluy nyajil | 1sg.NOM barramundi here see.NONPAST", "r295_1sg_here_water_fauna_see_judi", ["ngayu", "judi", "yaluy", "nyajil"]),
    row(6, "I see the mullet here.", "Ngayu ngalkun yaluy nyajil.", "ngayu ngalkun yaluy nyajil | 1sg.NOM mullet here see.NONPAST", "r295_1sg_here_water_fauna_see_ngalkun", ["ngayu", "ngalkun", "yaluy", "nyajil"]),
    row(7, "I see the freshwater eel here.", "Ngayu kubarr yaluy nyajil.", "ngayu kubarr yaluy nyajil | 1sg.NOM freshwater.eel here see.NONPAST", "r295_1sg_here_water_fauna_see_kubarr", ["ngayu", "kubarr", "yaluy", "nyajil"]),
    row(8, "I see the mudcrab here.", "Ngayu kiju yaluy nyajil.", "ngayu kiju yaluy nyajil | 1sg.NOM mudcrab here see.NONPAST", "r295_1sg_here_water_fauna_see_kiju", ["ngayu", "kiju", "yaluy", "nyajil"]),
    row(9, "I see the prawn here.", "Ngayu wakal yaluy nyajil.", "ngayu wakal yaluy nyajil | 1sg.NOM prawn here see.NONPAST", "r295_1sg_here_water_fauna_see_wakal", ["ngayu", "wakal", "yaluy", "nyajil"]),
    row(10, "I see the oyster here.", "Ngayu julul yaluy nyajil.", "ngayu julul yaluy nyajil | 1sg.NOM oyster here see.NONPAST", "r295_1sg_here_water_fauna_see_julul", ["ngayu", "julul", "yaluy", "nyajil"]),
    row(11, "You see the sea turtle there.", "Yundu ngawiya yinyay nyajil.", "yundu ngawiya yinyay nyajil | 2sg.NOM sea.turtle there see.NONPAST", "r295_2sg_there_water_fauna_see_ngawiya", ["yundu", "ngawiya", "yinyay", "nyajil"]),
    row(12, "You see the freshwater turtle there.", "Yundu ngujay yinyay nyajil.", "yundu ngujay yinyay nyajil | 2sg.NOM freshwater.turtle there see.NONPAST", "r295_2sg_there_water_fauna_see_ngujay", ["yundu", "ngujay", "yinyay", "nyajil"]),
    row(13, "You see the fish there.", "Yundu kuyu yinyay nyajil.", "yundu kuyu yinyay nyajil | 2sg.NOM fish there see.NONPAST", "r295_2sg_there_water_fauna_see_kuyu", ["yundu", "kuyu", "yinyay", "nyajil"]),
    row(14, "You see the stingray there.", "Yundu yawu yinyay nyajil.", "yundu yawu yinyay nyajil | 2sg.NOM stingray there see.NONPAST", "r295_2sg_there_water_fauna_see_yawu", ["yundu", "yawu", "yinyay", "nyajil"]),
    row(15, "You see the barramundi there.", "Yundu judi yinyay nyajil.", "yundu judi yinyay nyajil | 2sg.NOM barramundi there see.NONPAST", "r295_2sg_there_water_fauna_see_judi", ["yundu", "judi", "yinyay", "nyajil"]),
    row(16, "You see the mullet there.", "Yundu ngalkun yinyay nyajil.", "yundu ngalkun yinyay nyajil | 2sg.NOM mullet there see.NONPAST", "r295_2sg_there_water_fauna_see_ngalkun", ["yundu", "ngalkun", "yinyay", "nyajil"]),
    row(17, "You see the freshwater eel there.", "Yundu kubarr yinyay nyajil.", "yundu kubarr yinyay nyajil | 2sg.NOM freshwater.eel there see.NONPAST", "r295_2sg_there_water_fauna_see_kubarr", ["yundu", "kubarr", "yinyay", "nyajil"]),
    row(18, "You see the mudcrab there.", "Yundu kiju yinyay nyajil.", "yundu kiju yinyay nyajil | 2sg.NOM mudcrab there see.NONPAST", "r295_2sg_there_water_fauna_see_kiju", ["yundu", "kiju", "yinyay", "nyajil"]),
    row(19, "You see the prawn there.", "Yundu wakal yinyay nyajil.", "yundu wakal yinyay nyajil | 2sg.NOM prawn there see.NONPAST", "r295_2sg_there_water_fauna_see_wakal", ["yundu", "wakal", "yinyay", "nyajil"]),
    row(20, "You see the oyster there.", "Yundu julul yinyay nyajil.", "yundu julul yinyay nyajil | 2sg.NOM oyster there see.NONPAST", "r295_2sg_there_water_fauna_see_julul", ["yundu", "julul", "yinyay", "nyajil"]),
    row(21, "He sees the sea turtle here.", "Nyulu ngawiya yaluy nyajil.", "nyulu ngawiya yaluy nyajil | 3sg.NOM sea.turtle here see.NONPAST", "r295_3sg_here_water_fauna_see_ngawiya", ["nyulu", "ngawiya", "yaluy", "nyajil"]),
    row(22, "He sees the freshwater turtle here.", "Nyulu ngujay yaluy nyajil.", "nyulu ngujay yaluy nyajil | 3sg.NOM freshwater.turtle here see.NONPAST", "r295_3sg_here_water_fauna_see_ngujay", ["nyulu", "ngujay", "yaluy", "nyajil"]),
    row(23, "He sees the fish here.", "Nyulu kuyu yaluy nyajil.", "nyulu kuyu yaluy nyajil | 3sg.NOM fish here see.NONPAST", "r295_3sg_here_water_fauna_see_kuyu", ["nyulu", "kuyu", "yaluy", "nyajil"]),
    row(24, "He sees the stingray here.", "Nyulu yawu yaluy nyajil.", "nyulu yawu yaluy nyajil | 3sg.NOM stingray here see.NONPAST", "r295_3sg_here_water_fauna_see_yawu", ["nyulu", "yawu", "yaluy", "nyajil"]),
    row(25, "He sees the barramundi here.", "Nyulu judi yaluy nyajil.", "nyulu judi yaluy nyajil | 3sg.NOM barramundi here see.NONPAST", "r295_3sg_here_water_fauna_see_judi", ["nyulu", "judi", "yaluy", "nyajil"]),
    row(26, "He sees the mullet here.", "Nyulu ngalkun yaluy nyajil.", "nyulu ngalkun yaluy nyajil | 3sg.NOM mullet here see.NONPAST", "r295_3sg_here_water_fauna_see_ngalkun", ["nyulu", "ngalkun", "yaluy", "nyajil"]),
    row(27, "He sees the freshwater eel here.", "Nyulu kubarr yaluy nyajil.", "nyulu kubarr yaluy nyajil | 3sg.NOM freshwater.eel here see.NONPAST", "r295_3sg_here_water_fauna_see_kubarr", ["nyulu", "kubarr", "yaluy", "nyajil"]),
    row(28, "He sees the mudcrab here.", "Nyulu kiju yaluy nyajil.", "nyulu kiju yaluy nyajil | 3sg.NOM mudcrab here see.NONPAST", "r295_3sg_here_water_fauna_see_kiju", ["nyulu", "kiju", "yaluy", "nyajil"]),
    row(29, "He sees the prawn here.", "Nyulu wakal yaluy nyajil.", "nyulu wakal yaluy nyajil | 3sg.NOM prawn here see.NONPAST", "r295_3sg_here_water_fauna_see_wakal", ["nyulu", "wakal", "yaluy", "nyajil"]),
    row(30, "He sees the oyster here.", "Nyulu julul yaluy nyajil.", "nyulu julul yaluy nyajil | 3sg.NOM oyster here see.NONPAST", "r295_3sg_here_water_fauna_see_julul", ["nyulu", "julul", "yaluy", "nyajil"]),
    row(31, "They two see the sea turtle there.", "Bula ngawiya yinyay nyajil.", "bula ngawiya yinyay nyajil | 3du.NOM sea.turtle there see.NONPAST", "r295_3du_there_water_fauna_see_ngawiya", ["bula", "ngawiya", "yinyay", "nyajil"]),
    row(32, "They two see the freshwater turtle there.", "Bula ngujay yinyay nyajil.", "bula ngujay yinyay nyajil | 3du.NOM freshwater.turtle there see.NONPAST", "r295_3du_there_water_fauna_see_ngujay", ["bula", "ngujay", "yinyay", "nyajil"]),
    row(33, "They two see the fish there.", "Bula kuyu yinyay nyajil.", "bula kuyu yinyay nyajil | 3du.NOM fish there see.NONPAST", "r295_3du_there_water_fauna_see_kuyu", ["bula", "kuyu", "yinyay", "nyajil"]),
    row(34, "They two see the stingray there.", "Bula yawu yinyay nyajil.", "bula yawu yinyay nyajil | 3du.NOM stingray there see.NONPAST", "r295_3du_there_water_fauna_see_yawu", ["bula", "yawu", "yinyay", "nyajil"]),
    row(35, "They two see the barramundi there.", "Bula judi yinyay nyajil.", "bula judi yinyay nyajil | 3du.NOM barramundi there see.NONPAST", "r295_3du_there_water_fauna_see_judi", ["bula", "judi", "yinyay", "nyajil"]),
    row(36, "They two see the mullet there.", "Bula ngalkun yinyay nyajil.", "bula ngalkun yinyay nyajil | 3du.NOM mullet there see.NONPAST", "r295_3du_there_water_fauna_see_ngalkun", ["bula", "ngalkun", "yinyay", "nyajil"]),
    row(37, "They two see the freshwater eel there.", "Bula kubarr yinyay nyajil.", "bula kubarr yinyay nyajil | 3du.NOM freshwater.eel there see.NONPAST", "r295_3du_there_water_fauna_see_kubarr", ["bula", "kubarr", "yinyay", "nyajil"]),
    row(38, "They two see the mudcrab there.", "Bula kiju yinyay nyajil.", "bula kiju yinyay nyajil | 3du.NOM mudcrab there see.NONPAST", "r295_3du_there_water_fauna_see_kiju", ["bula", "kiju", "yinyay", "nyajil"]),
    row(39, "They two see the prawn there.", "Bula wakal yinyay nyajil.", "bula wakal yinyay nyajil | 3du.NOM prawn there see.NONPAST", "r295_3du_there_water_fauna_see_wakal", ["bula", "wakal", "yinyay", "nyajil"]),
    row(40, "They two see the oyster there.", "Bula julul yinyay nyajil.", "bula julul yinyay nyajil | 3du.NOM oyster there see.NONPAST", "r295_3du_there_water_fauna_see_julul", ["bula", "julul", "yinyay", "nyajil"]),
    row(41, "They see the sea turtle here.", "Jana ngawiya yaluy nyajil.", "jana ngawiya yaluy nyajil | 3pl.NOM sea.turtle here see.NONPAST", "r295_3pl_here_water_fauna_see_ngawiya", ["jana", "ngawiya", "yaluy", "nyajil"]),
    row(42, "They see the freshwater turtle here.", "Jana ngujay yaluy nyajil.", "jana ngujay yaluy nyajil | 3pl.NOM freshwater.turtle here see.NONPAST", "r295_3pl_here_water_fauna_see_ngujay", ["jana", "ngujay", "yaluy", "nyajil"]),
    row(43, "They see the fish here.", "Jana kuyu yaluy nyajil.", "jana kuyu yaluy nyajil | 3pl.NOM fish here see.NONPAST", "r295_3pl_here_water_fauna_see_kuyu", ["jana", "kuyu", "yaluy", "nyajil"]),
    row(44, "They see the stingray here.", "Jana yawu yaluy nyajil.", "jana yawu yaluy nyajil | 3pl.NOM stingray here see.NONPAST", "r295_3pl_here_water_fauna_see_yawu", ["jana", "yawu", "yaluy", "nyajil"]),
    row(45, "They see the barramundi here.", "Jana judi yaluy nyajil.", "jana judi yaluy nyajil | 3pl.NOM barramundi here see.NONPAST", "r295_3pl_here_water_fauna_see_judi", ["jana", "judi", "yaluy", "nyajil"]),
    row(46, "They see the mullet here.", "Jana ngalkun yaluy nyajil.", "jana ngalkun yaluy nyajil | 3pl.NOM mullet here see.NONPAST", "r295_3pl_here_water_fauna_see_ngalkun", ["jana", "ngalkun", "yaluy", "nyajil"]),
    row(47, "They see the freshwater eel here.", "Jana kubarr yaluy nyajil.", "jana kubarr yaluy nyajil | 3pl.NOM freshwater.eel here see.NONPAST", "r295_3pl_here_water_fauna_see_kubarr", ["jana", "kubarr", "yaluy", "nyajil"]),
    row(48, "They see the mudcrab here.", "Jana kiju yaluy nyajil.", "jana kiju yaluy nyajil | 3pl.NOM mudcrab here see.NONPAST", "r295_3pl_here_water_fauna_see_kiju", ["jana", "kiju", "yaluy", "nyajil"]),
    row(49, "They see the prawn here.", "Jana wakal yaluy nyajil.", "jana wakal yaluy nyajil | 3pl.NOM prawn here see.NONPAST", "r295_3pl_here_water_fauna_see_wakal", ["jana", "wakal", "yaluy", "nyajil"]),
    row(50, "They see the oyster here.", "Jana julul yaluy nyajil.", "jana julul yaluy nyajil | 3pl.NOM oyster here see.NONPAST", "r295_3pl_here_water_fauna_see_julul", ["jana", "julul", "yaluy", "nyajil"]),
]


def scan_static() -> list[str]:
    issues: list[str] = []
    if len(ROWS) != 50:
        issues.append(f"row count {len(ROWS)} != 50")
    if [r[0] for r in ROWS] != list(range(1, 51)):
        issues.append("sequence is not 1..50")
    kukus = [r[2] for r in ROWS]
    if len(set(kukus)) != len(kukus):
        issues.append("duplicate Kuku surface inside batch")
    for seq, english, kuku, _analysis, frame, _tier, words_json, loans_json, _evidence, _confidence in ROWS:
        words = json.loads(words_json)
        loans = json.loads(loans_json)
        lower_kuku = kuku.lower()
        lower_english = english.lower()
        if loans:
            issues.append(f"{seq}: loans used {loans}")
        denied = sorted({w for w in DENY_KUKU if re.search(rf"(^|\\s){re.escape(w)}([\\s\\.]|$)", lower_kuku)})
        if denied:
            issues.append(f"{seq}: denied Kuku forms {denied}")
        denied_english = [term for term in DENY_ENGLISH if re.search(rf"\\b{re.escape(term.strip())}\\b", lower_english)]
        if denied_english:
            issues.append(f"{seq}: denied English drift {denied_english}")
        target_hits = [w for w in TARGET_ORDER if w in words]
        if len(target_hits) != 1:
            issues.append(f"{seq}: target hits {target_hits}")
        if len(words) != 4:
            issues.append(f"{seq}: expected 4 words, got {words}")
        if "nyajil" not in words or not lower_kuku.endswith(" nyajil."):
            issues.append(f"{seq}: missing final nyajil")
        if "nyajin" in lower_kuku:
            issues.append(f"{seq}: past nyajin leakage")
        if len([w for w in ("yaluy", "yinyay") if w in words]) != 1:
            issues.append(f"{seq}: demonstrative problem {words}")
        expected_pronoun, expected_demo = FRAME_SPECS[(seq - 1) // 10]
        if words[0] != expected_pronoun:
            issues.append(f"{seq}: expected pronoun {expected_pronoun}, got {words[0]}")
        if words[2] != expected_demo:
            issues.append(f"{seq}: expected demonstrative {expected_demo}, got {words[2]}")
        if words[1] != TARGET_ORDER[(seq - 1) % 10]:
            issues.append(f"{seq}: target order mismatch {words[1]}")
        if words[3] != "nyajil":
            issues.append(f"{seq}: word 4 is not nyajil {words[3]}")
        if lower_kuku.rstrip(".") != " ".join(words):
            issues.append(f"{seq}: Kuku does not match words_used {words}")
        if not frame.startswith("r295_"):
            issues.append(f"{seq}: frame not r295 {frame}")
    return issues


def count_words(db: sqlite3.Connection, words: list[str]) -> dict[str, int]:
    return {
        word: db.execute(
            """
            SELECT COUNT(*)
            FROM sentences s
            WHERE s.status IN ('accepted','revised')
              AND EXISTS (SELECT 1 FROM json_each(s.words_used) WHERE value=?)
            """,
            (word,),
        ).fetchone()[0]
        for word in words
    }


def main() -> int:
    issues = scan_static()
    if issues:
        for issue in issues:
            print(issue)
        raise SystemExit(1)

    db = sqlite3.connect(DB)
    db.execute("PRAGMA foreign_keys = ON")
    db.execute("BEGIN")
    ensure_schema(db)
    if db.execute("SELECT 1 FROM batches WHERE label=?", (LABEL,)).fetchone():
        raise SystemExit(f"{LABEL} already exists")
    collisions = []
    for _seq, _english, kuku, *_rest in ROWS:
        existing = db.execute(
            """
            SELECT b.label, s.seq, s.status
            FROM sentences s
            JOIN batches b ON b.id=s.batch_id
            WHERE lower(s.kuku)=lower(?) AND s.status!='rejected'
            """,
            (kuku,),
        ).fetchall()
        if existing:
            collisions.append((kuku, existing))
    if collisions:
        raise SystemExit(f"duplicate preflight failed: {collisions}")

    target_counts = count_words(db, TARGET_ORDER)
    support_counts = count_words(db, ["ngayu", "yundu", "nyulu", "bula", "jana", "yaluy", "yinyay", "nyajil"])
    verified_before, words_before = db.execute(
        """
        SELECT COUNT(*), SUM(LENGTH(kuku)-LENGTH(REPLACE(kuku,' ',''))+1)
        FROM sentences
        WHERE status IN ('accepted','revised')
        """
    ).fetchone()

    notes = (
        "VISIBLE WATER-FAUNA SINGULAR/THIRD NONPAST PERCEPTION SWEEP AFTER RULE 294 (Rule 295). "
        "50 under-review rows over ten checked visible target heads: ngawiya sea turtle, ngujay freshwater turtle, "
        "kuyu fish, yawu stingray, judi barramundi, ngalkun mullet, kubarr freshwater eel, kiju mudcrab, wakal prawn, "
        "and julul oyster. SUPPORT: bare nominative ngayu, yundu, nyulu, bula, and jana, plus yaluy/yinyay and nyajil. "
        "Only bare visible object nonpast perception/reference rows are opened; catching, spearing, hunting, cooking, "
        "eating, bait, net, hook, danger, ecology, place/story, target case, pronoun case, demonstrative case, compounds, "
        "productive reduplication, past/purposive/reduplicated perception drift, and support inflection remain closed."
    )
    cur = db.execute(
        "INSERT INTO batches(label, cheatsheet_version, guide_version, notes, created_at) VALUES(?,?,?,?,?)",
        (LABEL, "Rule 295 under review", "operator-guide-2026-07-09", notes, NOW),
    )
    batch_id = cur.lastrowid
    db.executemany(
        """
        INSERT INTO sentences(
          batch_id, seq, english, kuku, analysis, frame, tier, words_used, loans_used,
          evidence, status, confidence, rights_status, author, created_at, updated_at
        )
        VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
        """,
        [
            (
                batch_id, seq, english, kuku, analysis, frame, tier, words_used, loans_used,
                evidence, "under_review", confidence,
                "project_approved_synthetic_pending_elder_verification", "claude", NOW, NOW,
            )
            for seq, english, kuku, analysis, frame, tier, words_used, loans_used, evidence, confidence in ROWS
        ],
    )
    record_event(db, batch_label=LABEL, phase="source", event_type="source-readback", actor="claude-main", detail="Reread core dictionary-derived lexicon lines 744-759 for ten visible water-fauna targets and Patz pronoun, demonstrative, and nyaji-l nonpast support before authoring Rule 295.", artifact_path=PROCESS_LOG, created_at=NOW)
    record_event(db, batch_label=LABEL, phase="planning", event_type="lexical-field-scope", actor="claude-main", detail="Selected a fresh visible water-fauna lexical field after Rule 294. The batch uses singular/third-person nominative pronoun bands and does not open catching, spearing, hunting, cooking, eating, ecology, danger, or source-prose semantics.", artifact_path=PROCESS_LOG, created_at=NOW)
    record_event(db, batch_label=LABEL, phase="preflight", event_type="coverage-audit", actor="claude-main", detail=f"Token-aware accepted/revised coverage before banking from words_used JSON: targets={target_counts}; support={support_counts}.", artifact_path=PROCESS_LOG, created_at=NOW)
    record_event(db, batch_label=LABEL, phase="preflight", event_type="duplicate-preflight", actor="claude-main", detail="Final 50 Rule 295 visible water-fauna singular/third nonpast surfaces returned no accepted, revised, or under-review collisions after replacing collided yuday, kulil, and nuwi candidates.", artifact_path=PROCESS_LOG, created_at=NOW)
    record_event(db, batch_label=LABEL, phase="preflight", event_type="static-guard-pass", actor="claude-main", detail="Static guard passed: 50 explicit hand-authored rows, one target each, bare nominative singular/third pronoun bands, bare yaluy/yinyay, final nyajil, no loans, no denied Kuku forms, and no catch/spear/cook/eat/ecology/case/compound drift.", command="python3 -m py_compile batch_scripts/bank_337.py && python3 -c \"import sys; sys.path.insert(0,'batch_scripts'); import bank_337; print(bank_337.scan_static())\"", output_excerpt="issues 0", artifact_path="batch_scripts/bank_337.py", created_at=NOW)
    record_event(db, batch_label=LABEL, phase="bank", event_type="bank-success", actor="claude-main", detail=f"Banked 50 Rule 295 rows as under_review. Verified total remains {verified_before}; Kuku word count remains {words_before}.", command="python3 batch_scripts/bank_337.py", output_excerpt=f"banked batch-337: 50 rows under_review; verified_before={verified_before}; kuku_words_before={words_before}", artifact_path=PROCESS_LOG, created_at=NOW)
    db.commit()
    print("banked batch-337: 50 rows under_review")
    return 0


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