#!/usr/bin/env python3
"""Bank batch-245: Rule 203 surface-property adjective lexical expansion."""

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-245"
NOW = dt.datetime.now(dt.timezone.utc).replace(microsecond=0).isoformat().replace("+00:00", "Z")
PROCESS_LOG = "process_logs/batch-245-surface-property-adjective-lexical-expansion-after-rule202-process.md"

TARGET_HEADS = {
    "mulun": "folded",
    "dangal": "baggy",
    "dulngki": "wide",
    "dungkul": "huge",
    "dunju-dunju": "coarse",
    "jala-jala": "loose",
    "jalkar-jalkar": "steep",
    "yuku": "thick",
    "muja-muja": "rotten",
    "nguyal": "sticky",
}
SUBJECT_HEADS = {"bubu", "kambi", "baral", "bayan", "juku", "mayi"}
SUPPORT = {"yinya", "yaluy", "yinyay"} | SUBJECT_HEADS
DENY_KUKU = {
    "nyaji-l", "nyajil", "nyajin", "baral-l", "baral-y", "kari", "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", "ngaki", "ngakiny", "ngakiji",
    "yala", "yanyu",
    "kawal", "ngawungkal", "ngulnkul", "nyinja", "wungka", "kandarr", "ngangkun", "wankun",
    "warimaji", "kunji", "jangkuy", "kankar", "nginja", "numburr", "yanja", "kadil", "kuwimbur",
    "ngangkurr", "ngingkirr",
    "jinjajalka", "kurunkal", "madu", "marngkil", "marrabal", "milbarakal", "mungari",
    "wurmbariji", "wuymbariji", "kurrun",
    "ngumbu", "bingaji", "ngala-ngala", "dayirr", "yalbay", "karrkay", "kalbay", "kulka",
    "kulngkul", "wuban", "yayji", "wumbul", "kiway-kiway", "marral",
}
DENY_ENGLISH = (
    "hear", "heard", "hearing", "listen", "listening", "perceive", "perceived", "see", "saw", "feel", "felt",
    "eat", "eating", "cook", "cooking", "cooked", "gather", "gathering", "cut", "cutting", "chop", "chopping",
    "make", "making", "wear", "wearing", "sew", "sewing", "repair", "patched", "travel", "walk", "go", "going",
    "road along", "country", "owner", "ownership", "ill", "illness", "sick", "sickness", "body", "pain",
    "sound", "hear", "tree-use", "source", "because", "very", "more", "not",
)


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 clothing is folded.", "Kambi mulun.", "kambi mulun | clothing folded", "r203_np_adj_kambi_mulun", ["kambi", "mulun"], "Dictionary mulun 'folded/closed'; Patz adjective predicate pattern."),
    row(2, "The clothing is baggy.", "Kambi dangal.", "kambi dangal | clothing baggy", "r203_np_adj_kambi_dangal", ["kambi", "dangal"], "Dictionary dangal 'baggy/sloppy'; kambi clothing sense only."),
    row(3, "The road is wide.", "Baral dulngki.", "baral dulngki | road wide", "r203_np_adj_baral_dulngki", ["baral", "dulngki"], "Dictionary dulngki 'wide'; baral road/path noun sense only."),
    row(4, "The house is huge.", "Bayan dungkul.", "bayan dungkul | house huge", "r203_np_adj_bayan_dungkul", ["bayan", "dungkul"], "Dictionary dungkul 'huge/enormous'; no house-repair row."),
    row(5, "The wood is coarse.", "Juku dunju-dunju.", "juku dunju-dunju | wood coarse", "r203_np_adj_juku_dunju_dunju", ["juku", "dunju-dunju"], "Dictionary dunju-dunju 'having knobs/spikes; coarse'; juku wood sense only."),
    row(6, "The clothing is loose.", "Kambi jala-jala.", "kambi jala-jala | clothing loose", "r203_np_adj_kambi_jala_jala", ["kambi", "jala-jala"], "Dictionary jala-jala 'loose'; no wearing/making row."),
    row(7, "The road is steep.", "Baral jalkar-jalkar.", "baral jalkar-jalkar | road steep", "r203_np_adj_baral_jalkar_jalkar", ["baral", "jalkar-jalkar"], "Dictionary jalkar-jalkar 'steep'; no travel event."),
    row(8, "The wood is thick.", "Juku yuku.", "juku yuku | wood thick", "r203_np_adj_juku_yuku", ["juku", "yuku"], "Dictionary yuku 'big/thick'; selected as thick property only."),
    row(9, "The wood is rotten.", "Juku muja-muja.", "juku muja-muja | wood rotten", "r203_np_adj_juku_muja_muja", ["juku", "muja-muja"], "Dictionary muja-muja 'rotten/decayed wood or cloth'; no cutting/use row."),
    row(10, "The food is sticky.", "Mayi nguyal.", "mayi nguyal | food sticky", "r203_np_adj_mayi_nguyal", ["mayi", "nguyal"], "Dictionary nguyal 'sticky'; mayi food noun only, no eating/cooking row."),
    row(11, "The folded clothing is here.", "Kambi mulun yaluy.", "kambi mulun yaluy | clothing folded here", "r203_np_adj_here_kambi_mulun", ["kambi", "mulun", "yaluy"], "Rule 238 deictic property-frame precedent with yaluy."),
    row(12, "The baggy clothing is here.", "Kambi dangal yaluy.", "kambi dangal yaluy | clothing baggy here", "r203_np_adj_here_kambi_dangal", ["kambi", "dangal", "yaluy"], "Bare adjective plus yaluy support; kambi clothing only."),
    row(13, "The wide road is here.", "Baral dulngki yaluy.", "baral dulngki yaluy | road wide here", "r203_np_adj_here_baral_dulngki", ["baral", "dulngki", "yaluy"], "Bare adjective plus yaluy support; no travel predicate."),
    row(14, "The huge house is here.", "Bayan dungkul yaluy.", "bayan dungkul yaluy | house huge here", "r203_np_adj_here_bayan_dungkul", ["bayan", "dungkul", "yaluy"], "Bare adjective plus yaluy support; no ownership/repair row."),
    row(15, "The coarse wood is here.", "Juku dunju-dunju yaluy.", "juku dunju-dunju yaluy | wood coarse here", "r203_np_adj_here_juku_dunju_dunju", ["juku", "dunju-dunju", "yaluy"], "Bare adjective plus yaluy support; no material-source claim."),
    row(16, "The loose clothing is here.", "Kambi jala-jala yaluy.", "kambi jala-jala yaluy | clothing loose here", "r203_np_adj_here_kambi_jala_jala", ["kambi", "jala-jala", "yaluy"], "Bare adjective plus yaluy support; no wearing/making row."),
    row(17, "The steep road is here.", "Baral jalkar-jalkar yaluy.", "baral jalkar-jalkar yaluy | road steep here", "r203_np_adj_here_baral_jalkar_jalkar", ["baral", "jalkar-jalkar", "yaluy"], "Bare adjective plus yaluy support; baral noun only."),
    row(18, "The thick wood is here.", "Juku yuku yaluy.", "juku yuku yaluy | wood thick here", "r203_np_adj_here_juku_yuku", ["juku", "yuku", "yaluy"], "Bare adjective plus yaluy support; no cutting/use row."),
    row(19, "The rotten wood is here.", "Juku muja-muja yaluy.", "juku muja-muja yaluy | wood rotten here", "r203_np_adj_here_juku_muja_muja", ["juku", "muja-muja", "yaluy"], "Bare adjective plus yaluy support; no cutting/use row."),
    row(20, "The sticky food is here.", "Mayi nguyal yaluy.", "mayi nguyal yaluy | food sticky here", "r203_np_adj_here_mayi_nguyal", ["mayi", "nguyal", "yaluy"], "Bare adjective plus yaluy support; no eating/cooking row."),
    row(21, "The folded clothing is there.", "Kambi mulun yinyay.", "kambi mulun yinyay | clothing folded there", "r203_np_adj_there_kambi_mulun", ["kambi", "mulun", "yinyay"], "Bare adjective plus yinyay support; no wearing/making row."),
    row(22, "The baggy clothing is there.", "Kambi dangal yinyay.", "kambi dangal yinyay | clothing baggy there", "r203_np_adj_there_kambi_dangal", ["kambi", "dangal", "yinyay"], "Bare adjective plus yinyay support; flying-fox homograph closed."),
    row(23, "The wide road is there.", "Baral dulngki yinyay.", "baral dulngki yinyay | road wide there", "r203_np_adj_there_baral_dulngki", ["baral", "dulngki", "yinyay"], "Bare adjective plus yinyay support; no travel event."),
    row(24, "The huge house is there.", "Bayan dungkul yinyay.", "bayan dungkul yinyay | house huge there", "r203_np_adj_there_bayan_dungkul", ["bayan", "dungkul", "yinyay"], "Bare adjective plus yinyay support; no ownership row."),
    row(25, "The coarse wood is there.", "Juku dunju-dunju yinyay.", "juku dunju-dunju yinyay | wood coarse there", "r203_np_adj_there_juku_dunju_dunju", ["juku", "dunju-dunju", "yinyay"], "Bare adjective plus yinyay support; no tree-use row."),
    row(26, "The loose clothing is there.", "Kambi jala-jala yinyay.", "kambi jala-jala yinyay | clothing loose there", "r203_np_adj_there_kambi_jala_jala", ["kambi", "jala-jala", "yinyay"], "Bare adjective plus yinyay support; no wearing/making row."),
    row(27, "The steep road is there.", "Baral jalkar-jalkar yinyay.", "baral jalkar-jalkar yinyay | road steep there", "r203_np_adj_there_baral_jalkar_jalkar", ["baral", "jalkar-jalkar", "yinyay"], "Bare adjective plus yinyay support; no route/travel row."),
    row(28, "The thick wood is there.", "Juku yuku yinyay.", "juku yuku yinyay | wood thick there", "r203_np_adj_there_juku_yuku", ["juku", "yuku", "yinyay"], "Bare adjective plus yinyay support; no wood-use row."),
    row(29, "The rotten wood is there.", "Juku muja-muja yinyay.", "juku muja-muja yinyay | wood rotten there", "r203_np_adj_there_juku_muja_muja", ["juku", "muja-muja", "yinyay"], "Bare adjective plus yinyay support; no cutting/use row."),
    row(30, "The sticky food is there.", "Mayi nguyal yinyay.", "mayi nguyal yinyay | food sticky there", "r203_np_adj_there_mayi_nguyal", ["mayi", "nguyal", "yinyay"], "Bare adjective plus yinyay support; no eating/cooking row."),
    row(31, "That clothing is folded.", "Yinya kambi mulun.", "yinya kambi mulun | that clothing folded", "r203_yinya_np_adj_kambi_mulun", ["yinya", "kambi", "mulun"], "Patz example 295 yinya + noun + adjective predicate."),
    row(32, "That clothing is baggy.", "Yinya kambi dangal.", "yinya kambi dangal | that clothing baggy", "r203_yinya_np_adj_kambi_dangal", ["yinya", "kambi", "dangal"], "Initial yinya support; clothing sense only."),
    row(33, "That road is wide.", "Yinya baral dulngki.", "yinya baral dulngki | that road wide", "r203_yinya_np_adj_baral_dulngki", ["yinya", "baral", "dulngki"], "Initial yinya support; baral road noun only."),
    row(34, "That house is huge.", "Yinya bayan dungkul.", "yinya bayan dungkul | that house huge", "r203_yinya_np_adj_bayan_dungkul", ["yinya", "bayan", "dungkul"], "Initial yinya support; no ownership/repair row."),
    row(35, "That wood is coarse.", "Yinya juku dunju-dunju.", "yinya juku dunju-dunju | that wood coarse", "r203_yinya_np_adj_juku_dunju_dunju", ["yinya", "juku", "dunju-dunju"], "Initial yinya support; no material-source claim."),
    row(36, "That clothing is loose.", "Yinya kambi jala-jala.", "yinya kambi jala-jala | that clothing loose", "r203_yinya_np_adj_kambi_jala_jala", ["yinya", "kambi", "jala-jala"], "Initial yinya support; no wearing/making row."),
    row(37, "That road is steep.", "Yinya baral jalkar-jalkar.", "yinya baral jalkar-jalkar | that road steep", "r203_yinya_np_adj_baral_jalkar_jalkar", ["yinya", "baral", "jalkar-jalkar"], "Initial yinya support; no travel event."),
    row(38, "That wood is thick.", "Yinya juku yuku.", "yinya juku yuku | that wood thick", "r203_yinya_np_adj_juku_yuku", ["yinya", "juku", "yuku"], "Initial yinya support; yuku selected as thick only."),
    row(39, "That wood is rotten.", "Yinya juku muja-muja.", "yinya juku muja-muja | that wood rotten", "r203_yinya_np_adj_juku_muja_muja", ["yinya", "juku", "muja-muja"], "Initial yinya support; no cutting/use row."),
    row(40, "That food is sticky.", "Yinya mayi nguyal.", "yinya mayi nguyal | that food sticky", "r203_yinya_np_adj_mayi_nguyal", ["yinya", "mayi", "nguyal"], "Patz example 295 yinya mayi adjective pattern; no eating/cooking row."),
    row(41, "That folded clothing is there.", "Yinya kambi mulun yinyay.", "yinya kambi mulun yinyay | that clothing folded there", "r203_yinya_np_adj_there_kambi_mulun", ["yinya", "kambi", "mulun", "yinyay"], "Initial yinya plus yinyay support; property only."),
    row(42, "That baggy clothing is there.", "Yinya kambi dangal yinyay.", "yinya kambi dangal yinyay | that clothing baggy there", "r203_yinya_np_adj_there_kambi_dangal", ["yinya", "kambi", "dangal", "yinyay"], "Initial yinya plus yinyay support; no wearing/making row."),
    row(43, "That wide road is there.", "Yinya baral dulngki yinyay.", "yinya baral dulngki yinyay | that road wide there", "r203_yinya_np_adj_there_baral_dulngki", ["yinya", "baral", "dulngki", "yinyay"], "Initial yinya plus yinyay support; no travel event."),
    row(44, "That huge house is there.", "Yinya bayan dungkul yinyay.", "yinya bayan dungkul yinyay | that house huge there", "r203_yinya_np_adj_there_bayan_dungkul", ["yinya", "bayan", "dungkul", "yinyay"], "Initial yinya plus yinyay support; no repair/ownership row."),
    row(45, "That coarse wood is there.", "Yinya juku dunju-dunju yinyay.", "yinya juku dunju-dunju yinyay | that wood coarse there", "r203_yinya_np_adj_there_juku_dunju_dunju", ["yinya", "juku", "dunju-dunju", "yinyay"], "Initial yinya plus yinyay support; no cutting/use row."),
    row(46, "That loose clothing is there.", "Yinya kambi jala-jala yinyay.", "yinya kambi jala-jala yinyay | that clothing loose there", "r203_yinya_np_adj_there_kambi_jala_jala", ["yinya", "kambi", "jala-jala", "yinyay"], "Initial yinya plus yinyay support; no wearing/making row."),
    row(47, "That steep road is there.", "Yinya baral jalkar-jalkar yinyay.", "yinya baral jalkar-jalkar yinyay | that road steep there", "r203_yinya_np_adj_there_baral_jalkar_jalkar", ["yinya", "baral", "jalkar-jalkar", "yinyay"], "Initial yinya plus yinyay support; no route/travel row."),
    row(48, "That thick wood is there.", "Yinya juku yuku yinyay.", "yinya juku yuku yinyay | that wood thick there", "r203_yinya_np_adj_there_juku_yuku", ["yinya", "juku", "yuku", "yinyay"], "Initial yinya plus yinyay support; no cutting/use row."),
    row(49, "That rotten wood is there.", "Yinya juku muja-muja yinyay.", "yinya juku muja-muja yinyay | that wood rotten there", "r203_yinya_np_adj_there_juku_muja_muja", ["yinya", "juku", "muja-muja", "yinyay"], "Initial yinya plus yinyay support; no cutting/use row."),
    row(50, "That sticky food is there.", "Yinya mayi nguyal yinyay.", "yinya mayi nguyal yinyay | that food sticky there", "r203_yinya_np_adj_there_mayi_nguyal", ["yinya", "mayi", "nguyal", "yinyay"], "Initial yinya plus yinyay support; no eating/cooking row."),
]


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("r203_"):
            issues.append(f"{label}: Rule 203 frame must start with r203_")
        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 203")
        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 203 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 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 bare NP ADJ with no deictic support")
        if 11 <= seq <= 20 and (has_token(low, "yinya") or has_token(low, "yinyay") or not low.endswith(" yaluy.")):
            issues.append(f"{label}: rows 11-20 require NP ADJ yaluy")
        if 21 <= seq <= 30 and (has_token(low, "yinya") or has_token(low, "yaluy") or not low.endswith(" yinyay.")):
            issues.append(f"{label}: rows 21-30 require NP ADJ yinyay")
        if 31 <= seq <= 40 and (not low.startswith("yinya ") or has_token(low, "yaluy") or has_token(low, "yinyay")):
            issues.append(f"{label}: rows 31-40 require Yinya NP ADJ")
        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 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 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 203 draft: surface-property adjective lexical expansion after Rule 202",
                "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

    record_event(
        db,
        batch_label=LABEL,
        phase="source",
        event_type="source-pass",
        actor="claude-main",
        detail="Rule 203 source pass selected ten unused dictionary-backed surface/physical-property adjectives: mulun, dangal, dulngki, dungkul, dunju-dunju, jala-jala, jalkar-jalkar, yuku, muja-muja, and nguyal. Patz 3.1 supports adjectives as property nominals; example 2 supports NP ADJ predicates; example 295 supports yinya NP ADJ.",
        command="dictionary.yaml exact readback for target/support heads; grammar.md adjective and predicate examples; sqlite lexeme/corpus frequency and duplicate preflight",
        artifact_path=PROCESS_LOG,
        created_at=NOW,
    )
    record_event(
        db,
        batch_label=LABEL,
        phase="scope",
        event_type="lexical-scope",
        actor="claude-main",
        detail="Rule 203 is a surface-property adjective lexical expansion. It admits only bare target adjectives in simple property-predicate frames and excludes perception verbs, negation, -kari, degree/comparison, actions, use/source claims, illness/body-state readings, food/eating/cooking, clothing wear/make, road travel, house repair, wood cutting/use, and demonstrative inflection.",
        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 surface-property adjective surfaces returned no accepted/revised/under_review collisions.",
        command="sqlite exact lower(trim(kuku)) duplicate scan before banking",
        artifact_path="batch_scripts/bank_245.py",
        created_at=NOW,
    )
    record_event(
        db,
        batch_label=LABEL,
        phase="preflight",
        event_type="static-guard-pass",
        actor="claude-main",
        detail="Static guard passed: exactly 50 unique surfaces; exactly one target adjective and one support subject noun per row; all target adjectives bare; no loans; no nyaji-l/perception/negation/degree/proximal-demonstrative tokens; no denied action/event/source/body-state English; and correct five-frame distribution.",
        command="python3 batch_scripts/bank_245.py pre-bank scan",
        artifact_path="batch_scripts/bank_245.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",
                NOW,
                NOW,
            )
            for seq, english, kuku, analysis, frame, tier, words_used, loans_used, evidence, confidence in ROWS
        ],
    )
    db.commit()

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


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