#!/usr/bin/env python3
"""Bank batch-194: Rule 152 construction consolidation after Rule 151."""

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-194"
NOW = dt.datetime.now(dt.timezone.utc).replace(microsecond=0).isoformat().replace("+00:00", "Z")
PROCESS_LOG = "process_logs/batch-194-construction-consolidation-after-rule151-process.md"

TARGET_HEADS = {
    "wawubaja": "river",
    "baral": "road",
    "bana": "water",
    "jukar": "sand",
    "manjal": "mountain",
    "jalkaraburr": "mountain.peak",
    "daburri": "swamp",
    "dajali": "deep.water",
    "yalkan": "shallow.water",
    "ngalkal": "open.ground",
}
TARGET_ENGLISH = {
    "wawubaja": "river",
    "baral": "road",
    "bana": "water",
    "jukar": "sand",
    "manjal": "mountain",
    "jalkaraburr": "mountain peak",
    "daburri": "swamp",
    "dajali": "deep water",
    "yalkan": "shallow water",
    "ngalkal": "open ground",
}
PRONOUN_A = {"ngayu", "ngana", "jana"}
COMMON_A = {"ngamu-ngku", "nganjan-angka", "babi-ngka", "karrkay-ngku", "kami-ngka", "jalbu-ngku", "dingkar-angka"}
FORBIDDEN_HEADS = {"wuburr", "balay", "dubar"}
DENY_KUKU = {
    "dama", "daman", "damanka", "nyurrba", "nyurrban", "yaka", "yakan", "kuni", "kunin", "baka", "bakan",
    "wundi", "wundin", "bangka", "bangkan", "balka", "balkan", "kaja", "kajan", "yurril", "yurrin",
    "kida", "kidan", "jurrnga", "jurrngan", "yilba", "yilban", "duda", "dudan", "nyanda", "nyandan",
    "mani", "manin", "yijarri", "yijarrin", "daka", "dakan", "waju", "wajun", "wunba", "mayi",
    "dunga", "dungan", "dungay", "kadan", "kaday", "kada", "janji", "janjil", "mariji", "marijin",
}
DENY_ENGLISH = (
    "go", "went", "come", "came", "walk", "walked", "run", "ran", "swim", "swam", "bathe", "bathed",
    "drink", "drank", "take", "took", "get", "got", "cut", "cutting", "spear", "speared", "kill",
    "killed", "catch", "caught", "hunt", "hunting", "fish with", "dive", "dived", "road along",
    "mountain top", "mission", "place name",
)


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, 4, js(words), js([]), evidence, "0.84")


ROWS = [
    row(1, "I see the river there.", "Ngayu wawubaja yinyay nyajil.", "ngayu wawubaja yinyay nyaji-l | I river there see-NONPAST", "r152_present_i_river_there", ["ngayu", "wawubaja", "yinyay", "nyaji-l"], "Rule 152 retained target; pronoun A bare; yinyay support."),
    row(2, "We see the road there.", "Ngana baral yinyay nyajil.", "ngana baral yinyay nyaji-l | we road there see-NONPAST", "r152_present_we_road_there", ["ngana", "baral", "yinyay", "nyaji-l"], "Rule 152 retained road noun; pronoun A bare; yinyay support."),
    row(3, "They see the water there.", "Jana bana yinyay nyajil.", "jana bana yinyay nyaji-l | they water there see-NONPAST", "r152_present_they_water_there", ["jana", "bana", "yinyay", "nyaji-l"], "Rule 152 retained water noun; pronoun A bare; yinyay support."),
    row(4, "Mother sees the sand there.", "Ngamu-ngku jukar yinyay nyajil.", "ngamu-ngku jukar yinyay nyaji-l | mother-ERG sand there see-NONPAST", "r152_present_mother_sand_there", ["ngamu", "jukar", "yinyay", "nyaji-l"], "Common-noun A ERG; retained sand noun."),
    row(5, "Father sees the mountain there.", "Nganjan-angka manjal yinyay nyajil.", "nganjan-angka manjal yinyay nyaji-l | father-ERG mountain there see-NONPAST", "r152_present_father_mountain_there", ["nganjan", "manjal", "yinyay", "nyaji-l"], "Common-noun A ERG; retained mountain noun."),
    row(6, "The child sees the mountain peak there.", "Karrkay-ngku jalkaraburr yinyay nyajil.", "karrkay-ngku jalkaraburr yinyay nyaji-l | child-ERG mountain.peak there see-NONPAST", "r152_present_child_peak_there", ["karrkay", "jalkaraburr", "yinyay", "nyaji-l"], "Common-noun A ERG; retained peak noun."),
    row(7, "Grandmother sees the swamp there.", "Babi-ngka daburri yinyay nyajil.", "babi-ngka daburri yinyay nyaji-l | grandmother-ERG swamp there see-NONPAST", "r152_present_grandmother_swamp_there", ["babi", "daburri", "yinyay", "nyaji-l"], "Common-noun A ERG; retained swamp noun."),
    row(8, "Grandfather sees the deep water there.", "Kami-ngka dajali yinyay nyajil.", "kami-ngka dajali yinyay nyaji-l | grandfather-ERG deep.water there see-NONPAST", "r152_present_grandfather_deep_water_there", ["kami", "dajali", "yinyay", "nyaji-l"], "Common-noun A ERG; dajali retained as deep-water noun."),
    row(9, "The woman sees the shallow water there.", "Jalbu-ngku yalkan yinyay nyajil.", "jalbu-ngku yalkan yinyay nyaji-l | woman-ERG shallow.water there see-NONPAST", "r152_present_woman_shallow_water_there", ["jalbu", "yalkan", "yinyay", "nyaji-l"], "Common-noun A ERG; yalkan retained as shallow-water noun."),
    row(10, "The man sees the open ground there.", "Dingkar-angka ngalkal yinyay nyajil.", "dingkar-angka ngalkal yinyay nyaji-l | man-ERG open.ground there see-NONPAST", "r152_present_man_open_ground_there", ["dingkar", "ngalkal", "yinyay", "nyaji-l"], "Common-noun A ERG; retained open-ground noun."),
    row(11, "I see the mountain.", "Ngayu manjal nyajil.", "ngayu manjal nyaji-l | I mountain see-NONPAST", "r152_present_i_mountain", ["ngayu", "manjal", "nyaji-l"], "Pronoun A bare; retained mountain noun."),
    row(12, "We see the mountain peak.", "Ngana jalkaraburr nyajil.", "ngana jalkaraburr nyaji-l | we mountain.peak see-NONPAST", "r152_present_we_peak", ["ngana", "jalkaraburr", "nyaji-l"], "Pronoun A bare; retained peak noun."),
    row(13, "They see the swamp.", "Jana daburri nyajil.", "jana daburri nyaji-l | they swamp see-NONPAST", "r152_present_they_swamp", ["jana", "daburri", "nyaji-l"], "Pronoun A bare; retained swamp noun."),
    row(14, "Mother sees the deep water.", "Ngamu-ngku dajali nyajil.", "ngamu-ngku dajali nyaji-l | mother-ERG deep.water see-NONPAST", "r152_present_mother_deep_water", ["ngamu", "dajali", "nyaji-l"], "Common-noun A ERG; dajali deep-water noun."),
    row(15, "Father sees the shallow water.", "Nganjan-angka yalkan nyajil.", "nganjan-angka yalkan nyaji-l | father-ERG shallow.water see-NONPAST", "r152_present_father_shallow_water", ["nganjan", "yalkan", "nyaji-l"], "Common-noun A ERG; yalkan shallow-water noun."),
    row(16, "The child sees the open ground.", "Karrkay-ngku ngalkal nyajil.", "karrkay-ngku ngalkal nyaji-l | child-ERG open.ground see-NONPAST", "r152_present_child_open_ground", ["karrkay", "ngalkal", "nyaji-l"], "Common-noun A ERG; retained open-ground noun."),
    row(17, "Grandmother sees the river.", "Babi-ngka wawubaja nyajil.", "babi-ngka wawubaja nyaji-l | grandmother-ERG river see-NONPAST", "r152_present_grandmother_river", ["babi", "wawubaja", "nyaji-l"], "Common-noun A ERG; retained river noun."),
    row(18, "Grandfather sees the road.", "Kami-ngka baral nyajil.", "kami-ngka baral nyaji-l | grandfather-ERG road see-NONPAST", "r152_present_grandfather_road", ["kami", "baral", "nyaji-l"], "Common-noun A ERG; baral road noun."),
    row(19, "The woman sees the water.", "Jalbu-ngku bana nyajil.", "jalbu-ngku bana nyaji-l | woman-ERG water see-NONPAST", "r152_present_woman_water", ["jalbu", "bana", "nyaji-l"], "Common-noun A ERG; retained water noun."),
    row(20, "The man sees the sand.", "Dingkar-angka jukar nyajil.", "dingkar-angka jukar nyaji-l | man-ERG sand see-NONPAST", "r152_present_man_sand", ["dingkar", "jukar", "nyaji-l"], "Common-noun A ERG; retained sand noun."),
    row(21, "I saw the mountain.", "Ngayu manjal nyajin.", "ngayu manjal nyaji-ny | I mountain see-PAST", "r152_past_i_mountain", ["ngayu", "manjal", "nyaji-l"], "Pronoun A bare; retained mountain noun."),
    row(22, "We saw the mountain peak.", "Ngana jalkaraburr nyajin.", "ngana jalkaraburr nyaji-ny | we mountain.peak see-PAST", "r152_past_we_peak", ["ngana", "jalkaraburr", "nyaji-l"], "Pronoun A bare; retained peak noun."),
    row(23, "They saw the swamp.", "Jana daburri nyajin.", "jana daburri nyaji-ny | they swamp see-PAST", "r152_past_they_swamp", ["jana", "daburri", "nyaji-l"], "Pronoun A bare; retained swamp noun."),
    row(24, "Mother saw the deep water.", "Ngamu-ngku dajali nyajin.", "ngamu-ngku dajali nyaji-ny | mother-ERG deep.water see-PAST", "r152_past_mother_deep_water", ["ngamu", "dajali", "nyaji-l"], "Common-noun A ERG; dajali deep-water noun."),
    row(25, "Father saw the shallow water.", "Nganjan-angka yalkan nyajin.", "nganjan-angka yalkan nyaji-ny | father-ERG shallow.water see-PAST", "r152_past_father_shallow_water", ["nganjan", "yalkan", "nyaji-l"], "Common-noun A ERG; yalkan shallow-water noun."),
    row(26, "The child saw the open ground.", "Karrkay-ngku ngalkal nyajin.", "karrkay-ngku ngalkal nyaji-ny | child-ERG open.ground see-PAST", "r152_past_child_open_ground", ["karrkay", "ngalkal", "nyaji-l"], "Common-noun A ERG; retained open-ground noun."),
    row(27, "Grandmother saw the river.", "Babi-ngka wawubaja nyajin.", "babi-ngka wawubaja nyaji-ny | grandmother-ERG river see-PAST", "r152_past_grandmother_river", ["babi", "wawubaja", "nyaji-l"], "Common-noun A ERG; retained river noun."),
    row(28, "Grandfather saw the road.", "Kami-ngka baral nyajin.", "kami-ngka baral nyaji-ny | grandfather-ERG road see-PAST", "r152_past_grandfather_road", ["kami", "baral", "nyaji-l"], "Common-noun A ERG; baral road noun."),
    row(29, "The woman saw the water.", "Jalbu-ngku bana nyajin.", "jalbu-ngku bana nyaji-ny | woman-ERG water see-PAST", "r152_past_woman_water", ["jalbu", "bana", "nyaji-l"], "Common-noun A ERG; retained water noun."),
    row(30, "The man saw the sand.", "Dingkar-angka jukar nyajin.", "dingkar-angka jukar nyaji-ny | man-ERG sand see-PAST", "r152_past_man_sand", ["dingkar", "jukar", "nyaji-l"], "Common-noun A ERG; retained sand noun."),
    row(31, "I saw the mountain there.", "Ngayu manjal yinyay nyajin.", "ngayu manjal yinyay nyaji-ny | I mountain there see-PAST", "r152_past_i_mountain_there", ["ngayu", "manjal", "yinyay", "nyaji-l"], "Pronoun A bare; deictic support yinyay."),
    row(32, "We saw the mountain peak there.", "Ngana jalkaraburr yinyay nyajin.", "ngana jalkaraburr yinyay nyaji-ny | we mountain.peak there see-PAST", "r152_past_we_peak_there", ["ngana", "jalkaraburr", "yinyay", "nyaji-l"], "Pronoun A bare; retained peak noun plus yinyay."),
    row(33, "They saw the swamp there.", "Jana daburri yinyay nyajin.", "jana daburri yinyay nyaji-ny | they swamp there see-PAST", "r152_past_they_swamp_there", ["jana", "daburri", "yinyay", "nyaji-l"], "Pronoun A bare; retained swamp noun plus yinyay."),
    row(34, "Mother saw the deep water there.", "Ngamu-ngku dajali yinyay nyajin.", "ngamu-ngku dajali yinyay nyaji-ny | mother-ERG deep.water there see-PAST", "r152_past_mother_deep_water_there", ["ngamu", "dajali", "yinyay", "nyaji-l"], "Common-noun A ERG; dajali plus yinyay."),
    row(35, "Father saw the shallow water there.", "Nganjan-angka yalkan yinyay nyajin.", "nganjan-angka yalkan yinyay nyaji-ny | father-ERG shallow.water there see-PAST", "r152_past_father_shallow_water_there", ["nganjan", "yalkan", "yinyay", "nyaji-l"], "Common-noun A ERG; yalkan plus yinyay."),
    row(36, "The child saw the open ground there.", "Karrkay-ngku ngalkal yinyay nyajin.", "karrkay-ngku ngalkal yinyay nyaji-ny | child-ERG open.ground there see-PAST", "r152_past_child_open_ground_there", ["karrkay", "ngalkal", "yinyay", "nyaji-l"], "Common-noun A ERG; ngalkal plus yinyay."),
    row(37, "Grandmother saw the river there.", "Babi-ngka wawubaja yinyay nyajin.", "babi-ngka wawubaja yinyay nyaji-ny | grandmother-ERG river there see-PAST", "r152_past_grandmother_river_there", ["babi", "wawubaja", "yinyay", "nyaji-l"], "Common-noun A ERG; river plus yinyay."),
    row(38, "Grandfather saw the road there.", "Kami-ngka baral yinyay nyajin.", "kami-ngka baral yinyay nyaji-ny | grandfather-ERG road there see-PAST", "r152_past_grandfather_road_there", ["kami", "baral", "yinyay", "nyaji-l"], "Common-noun A ERG; road plus yinyay."),
    row(39, "The woman saw the water there.", "Jalbu-ngku bana yinyay nyajin.", "jalbu-ngku bana yinyay nyaji-ny | woman-ERG water there see-PAST", "r152_past_woman_water_there", ["jalbu", "bana", "yinyay", "nyaji-l"], "Common-noun A ERG; water plus yinyay."),
    row(40, "The man saw the sand there.", "Dingkar-angka jukar yinyay nyajin.", "dingkar-angka jukar yinyay nyaji-ny | man-ERG sand there see-PAST", "r152_past_man_sand_there", ["dingkar", "jukar", "yinyay", "nyaji-l"], "Common-noun A ERG; sand plus yinyay."),
    row(41, "The woman sees the river there.", "Jalbu-ngku wawubaja yinyay nyajil.", "jalbu-ngku wawubaja yinyay nyaji-l | woman-ERG river there see-NONPAST", "r152_present_woman_river_there", ["jalbu", "wawubaja", "yinyay", "nyaji-l"], "Common-noun A ERG; river plus yinyay."),
    row(42, "The man sees the road there.", "Dingkar-angka baral yinyay nyajil.", "dingkar-angka baral yinyay nyaji-l | man-ERG road there see-NONPAST", "r152_present_man_road_there", ["dingkar", "baral", "yinyay", "nyaji-l"], "Common-noun A ERG; road plus yinyay."),
    row(43, "I see the water there.", "Ngayu bana yinyay nyajil.", "ngayu bana yinyay nyaji-l | I water there see-NONPAST", "r152_present_i_water_there", ["ngayu", "bana", "yinyay", "nyaji-l"], "Pronoun A bare; water plus yinyay."),
    row(44, "We see the sand there.", "Ngana jukar yinyay nyajil.", "ngana jukar yinyay nyaji-l | we sand there see-NONPAST", "r152_present_we_sand_there", ["ngana", "jukar", "yinyay", "nyaji-l"], "Pronoun A bare; sand plus yinyay."),
    row(45, "They see the mountain there.", "Jana manjal yinyay nyajil.", "jana manjal yinyay nyaji-l | they mountain there see-NONPAST", "r152_present_they_mountain_there", ["jana", "manjal", "yinyay", "nyaji-l"], "Pronoun A bare; mountain plus yinyay."),
    row(46, "Mother sees the mountain peak there.", "Ngamu-ngku jalkaraburr yinyay nyajil.", "ngamu-ngku jalkaraburr yinyay nyaji-l | mother-ERG mountain.peak there see-NONPAST", "r152_present_mother_peak_there", ["ngamu", "jalkaraburr", "yinyay", "nyaji-l"], "Common-noun A ERG; peak plus yinyay."),
    row(47, "Father sees the swamp there.", "Nganjan-angka daburri yinyay nyajil.", "nganjan-angka daburri yinyay nyaji-l | father-ERG swamp there see-NONPAST", "r152_present_father_swamp_there", ["nganjan", "daburri", "yinyay", "nyaji-l"], "Common-noun A ERG; swamp plus yinyay."),
    row(48, "The child sees the deep water there.", "Karrkay-ngku dajali yinyay nyajil.", "karrkay-ngku dajali yinyay nyaji-l | child-ERG deep.water there see-NONPAST", "r152_present_child_deep_water_there", ["karrkay", "dajali", "yinyay", "nyaji-l"], "Common-noun A ERG; dajali plus yinyay."),
    row(49, "Grandmother sees the shallow water there.", "Babi-ngka yalkan yinyay nyajil.", "babi-ngka yalkan yinyay nyaji-l | grandmother-ERG shallow.water there see-NONPAST", "r152_present_grandmother_shallow_water_there", ["babi", "yalkan", "yinyay", "nyaji-l"], "Common-noun A ERG; yalkan plus yinyay."),
    row(50, "Grandfather sees the open ground there.", "Kami-ngka ngalkal yinyay nyajil.", "kami-ngka ngalkal yinyay nyaji-l | grandfather-ERG open.ground there see-NONPAST", "r152_present_grandfather_open_ground_there", ["kami", "ngalkal", "yinyay", "nyaji-l"], "Common-noun A ERG; ngalkal plus yinyay."),
]


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


def first_word(low: str) -> str:
    return low.split()[0].strip(".,!?;:\"")


def has_target_case(low: str, head: str) -> bool:
    return re.search(rf"(?<![a-z-]){re.escape(head)}-(?:ngka|ngku|angka|nga|ngu|ba|muny|darr|burr|bu|ka|nka|nku|ji|jirr|ku|mundu|amu|mu)\b", low) is not None


def english_has_denied(english_low: str, denied: str) -> bool:
    if " " in denied:
        return denied in english_low
    return 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("r152_"):
            issues.append(f"{label}: Rule 152 frame must start with r152_")
        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 new loans are admitted in Rule 152")
        for denied in DENY_KUKU:
            if has_token(low, denied):
                issues.append(f"{label}: denied action/travel token {denied!r}")
        for denied in FORBIDDEN_HEADS:
            if has_token(low, denied):
                issues.append(f"{label}: forbidden place-name/adjective trap {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 152 target head, found {targets}")
        for head in targets:
            if head not in words:
                issues.append(f"{label}: target head {head} missing from words_used")
            if TARGET_HEADS[head] not in analysis:
                issues.append(f"{label}: target head {head} must be glossed as {TARGET_HEADS[head]}")
            if TARGET_ENGLISH[head] not in english_low:
                issues.append(f"{label}: {head} must be translated with selected sense {TARGET_ENGLISH[head]!r}")
            if has_target_case(low, head):
                issues.append(f"{label}: target head {head} has forbidden case/compound morphology")
        if has_token(low, "baral") and "feel" in english_low:
            issues.append(f"{label}: baral must not be used as perception verb 'feel'")
        if has_token(low, "dajali") and "deep water" not in english_low:
            issues.append(f"{label}: dajali must be translated as lexicalized deep water")
        if has_token(low, "yalkan") and "shallow water" not in english_low:
            issues.append(f"{label}: yalkan must be translated as lexicalized shallow water")
        if ("nyajin" not in low and "nyajil" not in low) or "nyaji-l" not in words:
            issues.append(f"{label}: Rule 152 rows must consolidate nyaji-l perception")
        if "yinyay" in low and "yinyay" not in words:
            issues.append(f"{label}: yinyay support missing from words_used")
        first = first_word(low)
        if first not in PRONOUN_A and first not in COMMON_A:
            issues.append(f"{label}: perception A should be an approved bare pronoun or ERG-marked common noun")
        if "nyajin" in low and "see-PAST" not in analysis:
            issues.append(f"{label}: nyajin must be glossed as see-PAST")
        if "nyajil" in low and "see-NONPAST" not in analysis:
            issues.append(f"{label}: nyajil must be glossed as see-NONPAST")
        if any(english_has_denied(english_low, bad) for bad in DENY_ENGLISH):
            issues.append(f"{label}: denied travel/action/place-name field in English")
        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 152 draft: construction consolidation after Rule 151", "operator-guide-2026-07-07", "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

    record_event(
        db,
        batch_label=LABEL,
        phase="source",
        event_type="source-pass",
        actor="claude-main",
        detail=(
            "Rule 152 source pass admitted no new target heads. It retained Rule 151 geography/water targets "
            "wawubaja, baral, bana, jukar, manjal, jalkaraburr, daburri, dajali, yalkan, and ngalkal, plus support "
            "forms yinyay and nyaji-l. Dictionary, Patz grammar, lexeme-table, and corpus-surface readback completed."
        ),
        command="dictionary.yaml exact readback; grammar.md exact occurrence readback; sqlite lexeme and surface readback",
        artifact_path=PROCESS_LOG,
        created_at=NOW,
    )
    record_event(
        db,
        batch_label=LABEL,
        phase="scope",
        event_type="construction-scope",
        actor="claude-main",
        detail=(
            "Rule 152 consolidates Rule 151 heads in perception frames only: present perception, bare past "
            "perception, and perception with yinyay deictic support. It excludes target case forms, target compounds, "
            "travel/swimming/bathing/diving/drinking/water-taking/road-traversal semantics, wuburr/balay/dubar, and "
            "all carry-forward implement/action traps."
        ),
        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 found 50 planned Kuku surfaces, 50 unique within-batch surfaces, and no exact accepted/revised/under_review cross-corpus duplicates.",
        command="sqlite exact lower(trim(kuku)) duplicate scan before banking",
        artifact_path="batch_scripts/bank_194.py",
        created_at=NOW,
    )
    record_event(
        db,
        batch_label=LABEL,
        phase="preflight",
        event_type="static-guard-pass",
        actor="claude-main",
        detail=(
            "Static guards passed: one bare retained geography/water target per row, selected English senses enforced, "
            "dajali/yalkan lexicalized as deep/shallow water, baral forced to road/path noun, no wuburr/balay/dubar "
            "trap, no travel/action/water-use field, no target case or compound morphology, common-noun A ERG, and "
            "pronoun A bare."
        ),
        command="python3 batch_scripts/bank_194.py static scan",
        artifact_path="batch_scripts/bank_194.py",
        created_at=NOW,
    )

    db.executemany(
        """
        INSERT INTO sentences(
          batch_id, seq, english, kuku, analysis, frame, tier, words_used,
          loans_used, evidence, status, confidence, author, created_at, updated_at
        )
        VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
        """,
        [
            (batch_id, seq, english, kuku, analysis, frame, tier, words_used, loans_used, evidence,
             "under_review", confidence, "claude-main", NOW, NOW)
            for seq, english, kuku, analysis, frame, tier, words_used, loans_used, evidence, confidence in ROWS
        ],
    )
    record_event(
        db,
        batch_label=LABEL,
        phase="bank",
        event_type="bank-success",
        actor="claude-main",
        detail="Banked 50 under_review Rule 152 geography/water construction-consolidation rows.",
        command="python3 batch_scripts/bank_194.py",
        artifact_path="batch_scripts/bank_194.py",
        created_at=NOW,
    )
    db.commit()

    verified = db.execute("SELECT COUNT(*) FROM sentences WHERE status IN ('accepted','revised')").fetchone()[0]
    under_review = db.execute("SELECT COUNT(*) FROM sentences WHERE status='under_review'").fetchone()[0]
    print(f"banked {LABEL}: rows=50, under_review={under_review}, verified_before_close={verified}")
    return 0


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