#!/usr/bin/env python3
"""Bank batch-263: Rule 221 second bird-reference 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-263"
NOW = dt.datetime.now(dt.timezone.utc).replace(microsecond=0).isoformat().replace("+00:00", "Z")
PROCESS_LOG = "process_logs/batch-263-second-bird-reference-lexical-expansion-after-rule220-process.md"

TARGET_HEADS = {
    "bajal": "bower bird",
    "jiwurrmal": "pheasant cuckoo",
    "jangkan": "swift",
    "karunkurr": "crested pigeon",
    "jurriyan": "satin bower bird",
    "kuludu": "dove",
    "kurrbal": "native companion",
    "jaka": "friarbird",
    "kalkuy": "owl",
    "wakuka": "crow",
}
SUPPORT = {"yinya", "yaluy", "yinyay"}
DENY_KUKU = {
    # Rule 211 bird targets.
    "barinkan", "bilu-warra", "bujil-yiran", "kalmbangarr", "dijarrdijin",
    "dukunjuju", "jiwal-jiwal", "karrmbil", "kujuway", "yiki",
    # Rejected bird candidates and high-risk alternates.
    "dawadawa", "dawa-dawa", "bidubidu", "bidu-bidu", "binjurrbinjurr",
    "binjurr-binjurr", "dibirrdibirr", "dibirr-dibirr", "jukijuki", "jukujuku",
    "juku-juku", "dabway", "dakwurr-dakwun", "jiway", "kurri-kurri", "walu-dalbaji",
    "bulbul", "bulbululul",
    # Recent mammal, fish, and reptile/amphibian targets.
    "bala", "darrba", "jarrabina", "jayju", "jungarr", "kukur", "mali", "murramu",
    "ngumbi", "yawa", "baban", "bububay", "ngalkun", "walkan", "barrbal", "jalkay",
    "janbal", "jijirr", "kurrubal", "murraja", "bajuy", "bilngkumu", "binanjal",
    "bindimu", "bubal", "jijirrbay", "kabul", "kalajarr", "kundurr", "yuday",
}
DENY_ENGLISH = (
    "fly", "flying", "flew", "sing", "sings", "singing", "call", "calling", "called",
    "warning", "warn", "message", "messenger", "omen", "death", "dead", "mourning",
    "totem", "moiety", "sacred", "story", "story-site", "myth", "legend", "place",
    "place-name", "nest", "egg", "eggs", "feather", "feathers", "food", "edible", "eat",
    "eating", "hunting", "hunt", "catch", "catching", "caught", "habitat", "ecology",
    "storm", "weather", "dance", "dancing", "display", "build", "building", "bower nest",
    "body-part", "head", "crest", "leather", "night", "plain turkey", "kookaburra",
    "death-herald", "herald", "social", "friend",
)


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


EVIDENCE = {
    "bajal": "Dictionary bajal noun 'bower bird' / 'species of bower bird'. Patz section 3.1 nouns include fauna and dikal as generic bird; section 3.7 supports yaluy/yinyay/yinya.",
    "jiwurrmal": "Dictionary jiwurrmal noun 'pheasant cuckoo' / 'cuckoo pheasant'; alternate bulbululul name not opened. Patz section 3.1 fauna noun and section 3.7 deictic support.",
    "jangkan": "Dictionary jangkan noun 'swift' / 'swift bird'. Motion/flying prose not opened. Patz section 3.1 fauna noun and section 3.7 deictic support.",
    "karunkurr": "Dictionary karunkurr noun 'crested pigeon'. Crest/body-part prose not opened. Patz section 3.1 fauna noun and section 3.7 deictic support.",
    "jurriyan": "Dictionary jurriyan noun 'satin bower bird'. Bower/display prose not opened. Patz section 3.1 fauna noun and section 3.7 deictic support.",
    "kuludu": "Dictionary kuludu noun 'dove' / 'small grey dove'. Colour/size prose stays inside species label. Patz section 3.1 fauna noun and section 3.7 deictic support.",
    "kurrbal": "Dictionary kurrbal noun 'native companion'; variant plain-turkey reading held closed. Patz section 3.1 fauna noun and section 3.7 deictic support.",
    "jaka": "Dictionary jaka noun 'friarbird' / 'friar bird'. Leather-head/body-part reading held closed. Patz section 3.1 fauna noun and section 3.7 deictic support.",
    "kalkuy": "Dictionary kalkuy noun 'owl' / 'night owl'. Night/timing prose held closed. Patz section 3.1 fauna noun and section 3.7 deictic support.",
    "wakuka": "Dictionary wakuka noun 'crow' / species of crow; kookaburra and messenger/death-herald readings held closed. Patz section 3.1 fauna noun and section 3.7 deictic support.",
}


ROWS = [
    row(1, "The bower bird is here.", "Bajal yaluy.", "bajal yaluy | bower.bird here", "r221_bird_ref_here_bajal", ["bajal", "yaluy"], EVIDENCE["bajal"]),
    row(2, "The pheasant cuckoo is here.", "Jiwurrmal yaluy.", "jiwurrmal yaluy | pheasant.cuckoo here", "r221_bird_ref_here_jiwurrmal", ["jiwurrmal", "yaluy"], EVIDENCE["jiwurrmal"]),
    row(3, "The swift is here.", "Jangkan yaluy.", "jangkan yaluy | swift here", "r221_bird_ref_here_jangkan", ["jangkan", "yaluy"], EVIDENCE["jangkan"]),
    row(4, "The crested pigeon is here.", "Karunkurr yaluy.", "karunkurr yaluy | crested.pigeon here", "r221_bird_ref_here_karunkurr", ["karunkurr", "yaluy"], EVIDENCE["karunkurr"]),
    row(5, "The satin bower bird is here.", "Jurriyan yaluy.", "jurriyan yaluy | satin.bower.bird here", "r221_bird_ref_here_jurriyan", ["jurriyan", "yaluy"], EVIDENCE["jurriyan"]),
    row(6, "The dove is here.", "Kuludu yaluy.", "kuludu yaluy | dove here", "r221_bird_ref_here_kuludu", ["kuludu", "yaluy"], EVIDENCE["kuludu"]),
    row(7, "The native companion is here.", "Kurrbal yaluy.", "kurrbal yaluy | native.companion here", "r221_bird_ref_here_kurrbal", ["kurrbal", "yaluy"], EVIDENCE["kurrbal"]),
    row(8, "The friarbird is here.", "Jaka yaluy.", "jaka yaluy | friarbird here", "r221_bird_ref_here_jaka", ["jaka", "yaluy"], EVIDENCE["jaka"]),
    row(9, "The owl is here.", "Kalkuy yaluy.", "kalkuy yaluy | owl here", "r221_bird_ref_here_kalkuy", ["kalkuy", "yaluy"], EVIDENCE["kalkuy"]),
    row(10, "The crow is here.", "Wakuka yaluy.", "wakuka yaluy | crow here", "r221_bird_ref_here_wakuka", ["wakuka", "yaluy"], EVIDENCE["wakuka"]),
    row(11, "The bower bird is there.", "Bajal yinyay.", "bajal yinyay | bower.bird there", "r221_bird_ref_there_bajal", ["bajal", "yinyay"], EVIDENCE["bajal"]),
    row(12, "The pheasant cuckoo is there.", "Jiwurrmal yinyay.", "jiwurrmal yinyay | pheasant.cuckoo there", "r221_bird_ref_there_jiwurrmal", ["jiwurrmal", "yinyay"], EVIDENCE["jiwurrmal"]),
    row(13, "The swift is there.", "Jangkan yinyay.", "jangkan yinyay | swift there", "r221_bird_ref_there_jangkan", ["jangkan", "yinyay"], EVIDENCE["jangkan"]),
    row(14, "The crested pigeon is there.", "Karunkurr yinyay.", "karunkurr yinyay | crested.pigeon there", "r221_bird_ref_there_karunkurr", ["karunkurr", "yinyay"], EVIDENCE["karunkurr"]),
    row(15, "The satin bower bird is there.", "Jurriyan yinyay.", "jurriyan yinyay | satin.bower.bird there", "r221_bird_ref_there_jurriyan", ["jurriyan", "yinyay"], EVIDENCE["jurriyan"]),
    row(16, "The dove is there.", "Kuludu yinyay.", "kuludu yinyay | dove there", "r221_bird_ref_there_kuludu", ["kuludu", "yinyay"], EVIDENCE["kuludu"]),
    row(17, "The native companion is there.", "Kurrbal yinyay.", "kurrbal yinyay | native.companion there", "r221_bird_ref_there_kurrbal", ["kurrbal", "yinyay"], EVIDENCE["kurrbal"]),
    row(18, "The friarbird is there.", "Jaka yinyay.", "jaka yinyay | friarbird there", "r221_bird_ref_there_jaka", ["jaka", "yinyay"], EVIDENCE["jaka"]),
    row(19, "The owl is there.", "Kalkuy yinyay.", "kalkuy yinyay | owl there", "r221_bird_ref_there_kalkuy", ["kalkuy", "yinyay"], EVIDENCE["kalkuy"]),
    row(20, "The crow is there.", "Wakuka yinyay.", "wakuka yinyay | crow there", "r221_bird_ref_there_wakuka", ["wakuka", "yinyay"], EVIDENCE["wakuka"]),
    row(21, "That bower bird is there.", "Yinya bajal yinyay.", "yinya bajal yinyay | that bower.bird there", "r221_that_bird_ref_there_bajal", ["yinya", "bajal", "yinyay"], EVIDENCE["bajal"]),
    row(22, "That pheasant cuckoo is there.", "Yinya jiwurrmal yinyay.", "yinya jiwurrmal yinyay | that pheasant.cuckoo there", "r221_that_bird_ref_there_jiwurrmal", ["yinya", "jiwurrmal", "yinyay"], EVIDENCE["jiwurrmal"]),
    row(23, "That swift is there.", "Yinya jangkan yinyay.", "yinya jangkan yinyay | that swift there", "r221_that_bird_ref_there_jangkan", ["yinya", "jangkan", "yinyay"], EVIDENCE["jangkan"]),
    row(24, "That crested pigeon is there.", "Yinya karunkurr yinyay.", "yinya karunkurr yinyay | that crested.pigeon there", "r221_that_bird_ref_there_karunkurr", ["yinya", "karunkurr", "yinyay"], EVIDENCE["karunkurr"]),
    row(25, "That satin bower bird is there.", "Yinya jurriyan yinyay.", "yinya jurriyan yinyay | that satin.bower.bird there", "r221_that_bird_ref_there_jurriyan", ["yinya", "jurriyan", "yinyay"], EVIDENCE["jurriyan"]),
    row(26, "That dove is there.", "Yinya kuludu yinyay.", "yinya kuludu yinyay | that dove there", "r221_that_bird_ref_there_kuludu", ["yinya", "kuludu", "yinyay"], EVIDENCE["kuludu"]),
    row(27, "That native companion is there.", "Yinya kurrbal yinyay.", "yinya kurrbal yinyay | that native.companion there", "r221_that_bird_ref_there_kurrbal", ["yinya", "kurrbal", "yinyay"], EVIDENCE["kurrbal"]),
    row(28, "That friarbird is there.", "Yinya jaka yinyay.", "yinya jaka yinyay | that friarbird there", "r221_that_bird_ref_there_jaka", ["yinya", "jaka", "yinyay"], EVIDENCE["jaka"]),
    row(29, "That owl is there.", "Yinya kalkuy yinyay.", "yinya kalkuy yinyay | that owl there", "r221_that_bird_ref_there_kalkuy", ["yinya", "kalkuy", "yinyay"], EVIDENCE["kalkuy"]),
    row(30, "That crow is there.", "Yinya wakuka yinyay.", "yinya wakuka yinyay | that crow there", "r221_that_bird_ref_there_wakuka", ["yinya", "wakuka", "yinyay"], EVIDENCE["wakuka"]),
    row(31, "That bower bird is there.", "Bajal yinya yinyay.", "bajal yinya yinyay | that bower.bird there", "r221_bird_that_there_bajal", ["bajal", "yinya", "yinyay"], EVIDENCE["bajal"]),
    row(32, "That pheasant cuckoo is there.", "Jiwurrmal yinya yinyay.", "jiwurrmal yinya yinyay | that pheasant.cuckoo there", "r221_bird_that_there_jiwurrmal", ["jiwurrmal", "yinya", "yinyay"], EVIDENCE["jiwurrmal"]),
    row(33, "That swift is there.", "Jangkan yinya yinyay.", "jangkan yinya yinyay | that swift there", "r221_bird_that_there_jangkan", ["jangkan", "yinya", "yinyay"], EVIDENCE["jangkan"]),
    row(34, "That crested pigeon is there.", "Karunkurr yinya yinyay.", "karunkurr yinya yinyay | that crested.pigeon there", "r221_bird_that_there_karunkurr", ["karunkurr", "yinya", "yinyay"], EVIDENCE["karunkurr"]),
    row(35, "That satin bower bird is there.", "Jurriyan yinya yinyay.", "jurriyan yinya yinyay | that satin.bower.bird there", "r221_bird_that_there_jurriyan", ["jurriyan", "yinya", "yinyay"], EVIDENCE["jurriyan"]),
    row(36, "That dove is there.", "Kuludu yinya yinyay.", "kuludu yinya yinyay | that dove there", "r221_bird_that_there_kuludu", ["kuludu", "yinya", "yinyay"], EVIDENCE["kuludu"]),
    row(37, "That native companion is there.", "Kurrbal yinya yinyay.", "kurrbal yinya yinyay | that native.companion there", "r221_bird_that_there_kurrbal", ["kurrbal", "yinya", "yinyay"], EVIDENCE["kurrbal"]),
    row(38, "That friarbird is there.", "Jaka yinya yinyay.", "jaka yinya yinyay | that friarbird there", "r221_bird_that_there_jaka", ["jaka", "yinya", "yinyay"], EVIDENCE["jaka"]),
    row(39, "That owl is there.", "Kalkuy yinya yinyay.", "kalkuy yinya yinyay | that owl there", "r221_bird_that_there_kalkuy", ["kalkuy", "yinya", "yinyay"], EVIDENCE["kalkuy"]),
    row(40, "That crow is there.", "Wakuka yinya yinyay.", "wakuka yinya yinyay | that crow there", "r221_bird_that_there_wakuka", ["wakuka", "yinya", "yinyay"], EVIDENCE["wakuka"]),
    row(41, "That bower bird is here.", "Yinya bajal yaluy.", "yinya bajal yaluy | that bower.bird here", "r221_that_bird_ref_here_bajal", ["yinya", "bajal", "yaluy"], EVIDENCE["bajal"]),
    row(42, "That pheasant cuckoo is here.", "Yinya jiwurrmal yaluy.", "yinya jiwurrmal yaluy | that pheasant.cuckoo here", "r221_that_bird_ref_here_jiwurrmal", ["yinya", "jiwurrmal", "yaluy"], EVIDENCE["jiwurrmal"]),
    row(43, "That swift is here.", "Yinya jangkan yaluy.", "yinya jangkan yaluy | that swift here", "r221_that_bird_ref_here_jangkan", ["yinya", "jangkan", "yaluy"], EVIDENCE["jangkan"]),
    row(44, "That crested pigeon is here.", "Yinya karunkurr yaluy.", "yinya karunkurr yaluy | that crested.pigeon here", "r221_that_bird_ref_here_karunkurr", ["yinya", "karunkurr", "yaluy"], EVIDENCE["karunkurr"]),
    row(45, "That satin bower bird is here.", "Yinya jurriyan yaluy.", "yinya jurriyan yaluy | that satin.bower.bird here", "r221_that_bird_ref_here_jurriyan", ["yinya", "jurriyan", "yaluy"], EVIDENCE["jurriyan"]),
    row(46, "That dove is here.", "Yinya kuludu yaluy.", "yinya kuludu yaluy | that dove here", "r221_that_bird_ref_here_kuludu", ["yinya", "kuludu", "yaluy"], EVIDENCE["kuludu"]),
    row(47, "That native companion is here.", "Yinya kurrbal yaluy.", "yinya kurrbal yaluy | that native.companion here", "r221_that_bird_ref_here_kurrbal", ["yinya", "kurrbal", "yaluy"], EVIDENCE["kurrbal"]),
    row(48, "That friarbird is here.", "Yinya jaka yaluy.", "yinya jaka yaluy | that friarbird here", "r221_that_bird_ref_here_jaka", ["yinya", "jaka", "yaluy"], EVIDENCE["jaka"]),
    row(49, "That owl is here.", "Yinya kalkuy yaluy.", "yinya kalkuy yaluy | that owl here", "r221_that_bird_ref_here_kalkuy", ["yinya", "kalkuy", "yaluy"], EVIDENCE["kalkuy"]),
    row(50, "That crow is here.", "Yinya wakuka yaluy.", "yinya wakuka yaluy | that crow here", "r221_that_bird_ref_here_wakuka", ["yinya", "wakuka", "yaluy"], EVIDENCE["wakuka"]),
]


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.strip())}\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("r221_"):
            issues.append(f"{label}: Rule 221 frame must start with r221_")
        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 221")
        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 221 target noun, found {targets}")
        for head in targets:
            selected = TARGET_HEADS[head]
            dotted = selected.replace("-", ".").replace(" ", ".")
            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 and dotted 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, "yinyay") or not low.endswith(" yaluy.") or len(low.split()) != 2):
            issues.append(f"{label}: rows 1-10 require TARGET yaluy")
        if 11 <= seq <= 20 and (has_token(low, "yinya") or has_token(low, "yaluy") or not low.endswith(" yinyay.") or len(low.split()) != 2):
            issues.append(f"{label}: rows 11-20 require TARGET yinyay")
        if 21 <= seq <= 30 and (not low.startswith("yinya ") or has_token(low, "yaluy") or not low.endswith(" yinyay.") or len(low.split()) != 3):
            issues.append(f"{label}: rows 21-30 require Yinya TARGET yinyay")
        if 31 <= seq <= 40 and (not has_token(low, "yinya") or has_token(low, "yaluy") or not low.endswith(" yinyay.") or len(low.split()) != 3 or low.startswith("yinya ")):
            issues.append(f"{label}: rows 31-40 require TARGET yinya yinyay")
        if 41 <= seq <= 50 and (not low.startswith("yinya ") or has_token(low, "yinyay") or not low.endswith(" yaluy.") or len(low.split()) != 3):
            issues.append(f"{label}: rows 41-50 require Yinya TARGET yaluy")
        if any(english_has_denied(english_low, bad) for bad in DENY_ENGLISH):
            issues.append(f"{label}: denied bird behavior/story/use/polysemy 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,))
    else:
        db.execute(
            "INSERT INTO batches(label, cheatsheet_version, guide_version, notes, created_at) VALUES(?,?,?,?,?)",
            (LABEL, "Rule 221 draft", "operator-guide-2026-07-08", "draft under review", NOW),
        )
        batch_id = db.execute("SELECT id FROM batches WHERE label=?", (LABEL,)).fetchone()[0]

    collisions: list[str] = []
    for seq, _english, kuku, *_rest in ROWS:
        found = 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 IN ('accepted','revised','under_review')
            """,
            (kuku,),
        ).fetchall()
        if found:
            collisions.append(f"{seq:02d} {kuku}: {found}")
    if collisions:
        raise SystemExit("duplicate preflight failed:\n" + "\n".join(collisions))

    record_event(
        db,
        batch_label=LABEL,
        phase="source",
        event_type="source-pass",
        actor="claude-main",
        detail="Checked Patz section 3.1 noun/fauna and dikal bird basis, Patz section 3.7 yinya/yaluy/yinyay support, earlier Rule 211 bird rows, and dictionary entries for bajal, jiwurrmal, jangkan, karunkurr, jurriyan, kuludu, kurrbal, jaka, kalkuy, and wakuka. Admitted only selected bare bird-reference labels.",
        artifact_path=PROCESS_LOG,
        created_at=NOW,
    )
    record_event(
        db,
        batch_label=LABEL,
        phase="scope",
        event_type="candidate-rejection",
        actor="claude-main",
        detail="Selected a second bird-reference field after excluding earlier Rule 211 exact target heads, unhyphenated-reduplication variants, story/totem/warning/death-heavy entries, storm/weather/message entries, food/egg/feather/nest/event entries, habitat-heavy entries, and homograph readings not selected for this batch.",
        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 221 Kuku surfaces returned no accepted, revised, or under-review collisions.",
        artifact_path="batch_scripts/bank_263.py",
        created_at=NOW,
    )
    record_event(
        db,
        batch_label=LABEL,
        phase="preflight",
        event_type="static-guard-pass",
        actor="claude-main",
        detail="bank_263.py static guards passed: 50 explicit hand-authored rows, exactly one bird-reference target noun per row, no loans, no target morphology, no bird behavior/story/use/polysemy English, and only the five sourced deictic frame bands.",
        artifact_path="batch_scripts/bank_263.py",
        created_at=NOW,
    )

    db.executemany(
        """
        INSERT INTO sentences(
          batch_id, seq, english, kuku, analysis, frame, tier, words_used,
          loans_used, evidence, status, confidence, created_at, updated_at
        )
        VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?)
        """,
        [
            (batch_id, seq, english, kuku, analysis, frame, tier, words_used, loans_used, evidence, "under_review", confidence, NOW, NOW)
            for seq, english, kuku, analysis, frame, tier, words_used, loans_used, evidence, confidence in ROWS
        ],
    )
    db.commit()

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


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