#!/usr/bin/env python3
"""Bank batch-307: Rule 265 complementary mammal non-singular pronoun perception sweep."""

from __future__ import annotations

import datetime as dt
import json
import sqlite3

from bank_306 import (
    DENY_ENGLISH,
    DENY_KUKU,
    TARGET_HEADS,
    english_has_denied,
    has_target_morphology,
    has_token,
    row,
)
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-307"
NOW = dt.datetime.now(dt.timezone.utc).replace(microsecond=0).isoformat().replace("+00:00", "Z")
PROCESS_LOG = "process_logs/batch-307-mammal-complementary-nonsingular-pronoun-perception-after-rule264-process.md"
SUPPORT = {"ngali", "ngaliny", "yubal", "nganjin", "ngana", "yaluy", "yinyay", "nyajil"}
PRONOUN_SUPPORT = {"ngali", "ngaliny", "yubal", "nganjin", "ngana"}

EVIDENCE = {
    "bala": "Dictionary lines 720-728 give bala as noun 'sugar glider'; lines 729-738 give the skinny/weak/boney adjective homograph, which Rule 265 keeps closed. Patz grammar lines 2577-2603 source bare nominative ngali, ngaliny, yubal, nganjin, and ngana; lines 2682-2684 discuss inclusive/exclusive and second-person non-singular forms; lines 2838-2845 source yaluy/yinyay; and lines 3043-3049 source nyaji-l 'see'.",
    "darrba": "Dictionary lines 5306-5315 give darrba as noun 'wallaby', definition/translations 'grass wallaby', semantic domain fauna-mammal. Rule 265 selects grass wallaby only. Patz grammar lines 2577-2603, 2682-2684, 2838-2845, and 3043-3049 source support.",
    "jarrabina": "Dictionary lines 8248-8259 give jarrabina as noun 'tree kangaroo', definition 'tree-climbing kangaroo', semantic domain fauna-mammal; Rule 265 selects the lexical label only and leaves tree-climbing and murral cross-reference drift closed. Patz grammar lines 2577-2603, 2682-2684, 2838-2845, and 3043-3049 source support.",
    "jayju": "Dictionary lines 8602-8610 give jayju as noun 'kangaroo rat', semantic domain fauna-mammal. Rule 265 selects kangaroo rat only. Patz grammar lines 2577-2603, 2682-2684, 2838-2845, and 3043-3049 source support.",
    "jungarr": "Dictionary lines 10882-10892 give jungarr as noun 'marsupial native cat', semantic domain fauna-mammal, with quoll commentary; Rule 265 selects marsupial native cat only and leaves domestic/introduced cat drift closed. Patz grammar lines 2577-2603, 2682-2684, 2838-2845, and 3043-3049 source support.",
    "kukur": "Dictionary lines 15002-15010 give kukur as noun 'rat', semantic domain fauna-mammal. Rule 265 selects rat only. Patz grammar lines 2577-2603, 2682-2684, 2838-2845, and 3043-3049 source support.",
    "mali": "Dictionary lines 17566-17577 give mali as noun 'bat', definition 'species of small bat', translations bat/small bat, semantic domain fauna-mammal; Rule 265 selects small bat as a lexical label only. Patz grammar lines 2577-2603, 2682-2684, 2838-2845, and 3043-3049 source support.",
    "murramu": "Dictionary lines 21010-21020 give murramu as noun 'dingo', semantic domain fauna-mammal, with synonym ngamu-murramu; Rule 265 selects murramu/dingo only and leaves synonym, dog/loan, attack, and event drift closed. Patz grammar lines 2577-2603, 2682-2684, 2838-2845, and 3043-3049 source support.",
    "ngumbi": "Dictionary lines 23562-23572 give ngumbi as noun 'bandicoot', definition 'little spotted bandicoot', semantic domain fauna-mammal; Rule 265 selects bandicoot only and leaves little/spotted as source-prose modifiers. Patz grammar lines 2577-2603, 2682-2684, 2838-2845, and 3043-3049 source support.",
    "yawa": "Dictionary lines 29634-29642 give yawa as noun 'possum', semantic domain fauna-mammal. Rule 265 selects possum only. Patz grammar lines 2577-2603, 2682-2684, 2838-2845, and 3043-3049 source support.",
}

ROWS = [
    row(1, "We two, not you, see the sugar glider here.", "Ngali bala yaluy nyajil.", "ngali bala yaluy nyajil | 1du.EXCL.NOM sugar.glider here see.NPST", "r265_1du_excl_target_here_see_bala", ["ngali", "bala", "yaluy", "nyajil"], EVIDENCE["bala"]),
    row(2, "We two, not you, see the grass wallaby here.", "Ngali darrba yaluy nyajil.", "ngali darrba yaluy nyajil | 1du.EXCL.NOM grass.wallaby here see.NPST", "r265_1du_excl_target_here_see_darrba", ["ngali", "darrba", "yaluy", "nyajil"], EVIDENCE["darrba"]),
    row(3, "We two, not you, see the tree kangaroo here.", "Ngali jarrabina yaluy nyajil.", "ngali jarrabina yaluy nyajil | 1du.EXCL.NOM tree.kangaroo here see.NPST", "r265_1du_excl_target_here_see_jarrabina", ["ngali", "jarrabina", "yaluy", "nyajil"], EVIDENCE["jarrabina"]),
    row(4, "We two, not you, see the kangaroo rat here.", "Ngali jayju yaluy nyajil.", "ngali jayju yaluy nyajil | 1du.EXCL.NOM kangaroo.rat here see.NPST", "r265_1du_excl_target_here_see_jayju", ["ngali", "jayju", "yaluy", "nyajil"], EVIDENCE["jayju"]),
    row(5, "We two, not you, see the marsupial native cat here.", "Ngali jungarr yaluy nyajil.", "ngali jungarr yaluy nyajil | 1du.EXCL.NOM marsupial.native.cat here see.NPST", "r265_1du_excl_target_here_see_jungarr", ["ngali", "jungarr", "yaluy", "nyajil"], EVIDENCE["jungarr"]),
    row(6, "We two, not you, see the rat here.", "Ngali kukur yaluy nyajil.", "ngali kukur yaluy nyajil | 1du.EXCL.NOM rat here see.NPST", "r265_1du_excl_target_here_see_kukur", ["ngali", "kukur", "yaluy", "nyajil"], EVIDENCE["kukur"]),
    row(7, "We two, not you, see the small bat here.", "Ngali mali yaluy nyajil.", "ngali mali yaluy nyajil | 1du.EXCL.NOM small.bat here see.NPST", "r265_1du_excl_target_here_see_mali", ["ngali", "mali", "yaluy", "nyajil"], EVIDENCE["mali"]),
    row(8, "We two, not you, see the dingo here.", "Ngali murramu yaluy nyajil.", "ngali murramu yaluy nyajil | 1du.EXCL.NOM dingo here see.NPST", "r265_1du_excl_target_here_see_murramu", ["ngali", "murramu", "yaluy", "nyajil"], EVIDENCE["murramu"]),
    row(9, "We two, not you, see the bandicoot here.", "Ngali ngumbi yaluy nyajil.", "ngali ngumbi yaluy nyajil | 1du.EXCL.NOM bandicoot here see.NPST", "r265_1du_excl_target_here_see_ngumbi", ["ngali", "ngumbi", "yaluy", "nyajil"], EVIDENCE["ngumbi"]),
    row(10, "We two, not you, see the possum here.", "Ngali yawa yaluy nyajil.", "ngali yawa yaluy nyajil | 1du.EXCL.NOM possum here see.NPST", "r265_1du_excl_target_here_see_yawa", ["ngali", "yawa", "yaluy", "nyajil"], EVIDENCE["yawa"]),
    row(11, "You and I see the sugar glider there.", "Ngaliny bala yinyay nyajil.", "ngaliny bala yinyay nyajil | 1du.INCL.NOM sugar.glider there see.NPST", "r265_1du_incl_target_there_see_bala", ["ngaliny", "bala", "yinyay", "nyajil"], EVIDENCE["bala"]),
    row(12, "You and I see the grass wallaby there.", "Ngaliny darrba yinyay nyajil.", "ngaliny darrba yinyay nyajil | 1du.INCL.NOM grass.wallaby there see.NPST", "r265_1du_incl_target_there_see_darrba", ["ngaliny", "darrba", "yinyay", "nyajil"], EVIDENCE["darrba"]),
    row(13, "You and I see the tree kangaroo there.", "Ngaliny jarrabina yinyay nyajil.", "ngaliny jarrabina yinyay nyajil | 1du.INCL.NOM tree.kangaroo there see.NPST", "r265_1du_incl_target_there_see_jarrabina", ["ngaliny", "jarrabina", "yinyay", "nyajil"], EVIDENCE["jarrabina"]),
    row(14, "You and I see the kangaroo rat there.", "Ngaliny jayju yinyay nyajil.", "ngaliny jayju yinyay nyajil | 1du.INCL.NOM kangaroo.rat there see.NPST", "r265_1du_incl_target_there_see_jayju", ["ngaliny", "jayju", "yinyay", "nyajil"], EVIDENCE["jayju"]),
    row(15, "You and I see the marsupial native cat there.", "Ngaliny jungarr yinyay nyajil.", "ngaliny jungarr yinyay nyajil | 1du.INCL.NOM marsupial.native.cat there see.NPST", "r265_1du_incl_target_there_see_jungarr", ["ngaliny", "jungarr", "yinyay", "nyajil"], EVIDENCE["jungarr"]),
    row(16, "You and I see the rat there.", "Ngaliny kukur yinyay nyajil.", "ngaliny kukur yinyay nyajil | 1du.INCL.NOM rat there see.NPST", "r265_1du_incl_target_there_see_kukur", ["ngaliny", "kukur", "yinyay", "nyajil"], EVIDENCE["kukur"]),
    row(17, "You and I see the small bat there.", "Ngaliny mali yinyay nyajil.", "ngaliny mali yinyay nyajil | 1du.INCL.NOM small.bat there see.NPST", "r265_1du_incl_target_there_see_mali", ["ngaliny", "mali", "yinyay", "nyajil"], EVIDENCE["mali"]),
    row(18, "You and I see the dingo there.", "Ngaliny murramu yinyay nyajil.", "ngaliny murramu yinyay nyajil | 1du.INCL.NOM dingo there see.NPST", "r265_1du_incl_target_there_see_murramu", ["ngaliny", "murramu", "yinyay", "nyajil"], EVIDENCE["murramu"]),
    row(19, "You and I see the bandicoot there.", "Ngaliny ngumbi yinyay nyajil.", "ngaliny ngumbi yinyay nyajil | 1du.INCL.NOM bandicoot there see.NPST", "r265_1du_incl_target_there_see_ngumbi", ["ngaliny", "ngumbi", "yinyay", "nyajil"], EVIDENCE["ngumbi"]),
    row(20, "You and I see the possum there.", "Ngaliny yawa yinyay nyajil.", "ngaliny yawa yinyay nyajil | 1du.INCL.NOM possum there see.NPST", "r265_1du_incl_target_there_see_yawa", ["ngaliny", "yawa", "yinyay", "nyajil"], EVIDENCE["yawa"]),
    row(21, "You two see the sugar glider here.", "Yubal bala yaluy nyajil.", "yubal bala yaluy nyajil | 2du.NOM sugar.glider here see.NPST", "r265_2du_target_here_see_bala", ["yubal", "bala", "yaluy", "nyajil"], EVIDENCE["bala"]),
    row(22, "You two see the grass wallaby here.", "Yubal darrba yaluy nyajil.", "yubal darrba yaluy nyajil | 2du.NOM grass.wallaby here see.NPST", "r265_2du_target_here_see_darrba", ["yubal", "darrba", "yaluy", "nyajil"], EVIDENCE["darrba"]),
    row(23, "You two see the tree kangaroo here.", "Yubal jarrabina yaluy nyajil.", "yubal jarrabina yaluy nyajil | 2du.NOM tree.kangaroo here see.NPST", "r265_2du_target_here_see_jarrabina", ["yubal", "jarrabina", "yaluy", "nyajil"], EVIDENCE["jarrabina"]),
    row(24, "You two see the kangaroo rat here.", "Yubal jayju yaluy nyajil.", "yubal jayju yaluy nyajil | 2du.NOM kangaroo.rat here see.NPST", "r265_2du_target_here_see_jayju", ["yubal", "jayju", "yaluy", "nyajil"], EVIDENCE["jayju"]),
    row(25, "You two see the marsupial native cat here.", "Yubal jungarr yaluy nyajil.", "yubal jungarr yaluy nyajil | 2du.NOM marsupial.native.cat here see.NPST", "r265_2du_target_here_see_jungarr", ["yubal", "jungarr", "yaluy", "nyajil"], EVIDENCE["jungarr"]),
    row(26, "You two see the rat here.", "Yubal kukur yaluy nyajil.", "yubal kukur yaluy nyajil | 2du.NOM rat here see.NPST", "r265_2du_target_here_see_kukur", ["yubal", "kukur", "yaluy", "nyajil"], EVIDENCE["kukur"]),
    row(27, "You two see the small bat here.", "Yubal mali yaluy nyajil.", "yubal mali yaluy nyajil | 2du.NOM small.bat here see.NPST", "r265_2du_target_here_see_mali", ["yubal", "mali", "yaluy", "nyajil"], EVIDENCE["mali"]),
    row(28, "You two see the dingo here.", "Yubal murramu yaluy nyajil.", "yubal murramu yaluy nyajil | 2du.NOM dingo here see.NPST", "r265_2du_target_here_see_murramu", ["yubal", "murramu", "yaluy", "nyajil"], EVIDENCE["murramu"]),
    row(29, "You two see the bandicoot here.", "Yubal ngumbi yaluy nyajil.", "yubal ngumbi yaluy nyajil | 2du.NOM bandicoot here see.NPST", "r265_2du_target_here_see_ngumbi", ["yubal", "ngumbi", "yaluy", "nyajil"], EVIDENCE["ngumbi"]),
    row(30, "You two see the possum here.", "Yubal yawa yaluy nyajil.", "yubal yawa yaluy nyajil | 2du.NOM possum here see.NPST", "r265_2du_target_here_see_yawa", ["yubal", "yawa", "yaluy", "nyajil"], EVIDENCE["yawa"]),
    row(31, "We all, not you, see the sugar glider there.", "Nganjin bala yinyay nyajil.", "nganjin bala yinyay nyajil | 1pl.EXCL.NOM sugar.glider there see.NPST", "r265_1pl_excl_target_there_see_bala", ["nganjin", "bala", "yinyay", "nyajil"], EVIDENCE["bala"]),
    row(32, "We all, not you, see the grass wallaby there.", "Nganjin darrba yinyay nyajil.", "nganjin darrba yinyay nyajil | 1pl.EXCL.NOM grass.wallaby there see.NPST", "r265_1pl_excl_target_there_see_darrba", ["nganjin", "darrba", "yinyay", "nyajil"], EVIDENCE["darrba"]),
    row(33, "We all, not you, see the tree kangaroo there.", "Nganjin jarrabina yinyay nyajil.", "nganjin jarrabina yinyay nyajil | 1pl.EXCL.NOM tree.kangaroo there see.NPST", "r265_1pl_excl_target_there_see_jarrabina", ["nganjin", "jarrabina", "yinyay", "nyajil"], EVIDENCE["jarrabina"]),
    row(34, "We all, not you, see the kangaroo rat there.", "Nganjin jayju yinyay nyajil.", "nganjin jayju yinyay nyajil | 1pl.EXCL.NOM kangaroo.rat there see.NPST", "r265_1pl_excl_target_there_see_jayju", ["nganjin", "jayju", "yinyay", "nyajil"], EVIDENCE["jayju"]),
    row(35, "We all, not you, see the marsupial native cat there.", "Nganjin jungarr yinyay nyajil.", "nganjin jungarr yinyay nyajil | 1pl.EXCL.NOM marsupial.native.cat there see.NPST", "r265_1pl_excl_target_there_see_jungarr", ["nganjin", "jungarr", "yinyay", "nyajil"], EVIDENCE["jungarr"]),
    row(36, "We all, not you, see the rat there.", "Nganjin kukur yinyay nyajil.", "nganjin kukur yinyay nyajil | 1pl.EXCL.NOM rat there see.NPST", "r265_1pl_excl_target_there_see_kukur", ["nganjin", "kukur", "yinyay", "nyajil"], EVIDENCE["kukur"]),
    row(37, "We all, not you, see the small bat there.", "Nganjin mali yinyay nyajil.", "nganjin mali yinyay nyajil | 1pl.EXCL.NOM small.bat there see.NPST", "r265_1pl_excl_target_there_see_mali", ["nganjin", "mali", "yinyay", "nyajil"], EVIDENCE["mali"]),
    row(38, "We all, not you, see the dingo there.", "Nganjin murramu yinyay nyajil.", "nganjin murramu yinyay nyajil | 1pl.EXCL.NOM dingo there see.NPST", "r265_1pl_excl_target_there_see_murramu", ["nganjin", "murramu", "yinyay", "nyajil"], EVIDENCE["murramu"]),
    row(39, "We all, not you, see the bandicoot there.", "Nganjin ngumbi yinyay nyajil.", "nganjin ngumbi yinyay nyajil | 1pl.EXCL.NOM bandicoot there see.NPST", "r265_1pl_excl_target_there_see_ngumbi", ["nganjin", "ngumbi", "yinyay", "nyajil"], EVIDENCE["ngumbi"]),
    row(40, "We all, not you, see the possum there.", "Nganjin yawa yinyay nyajil.", "nganjin yawa yinyay nyajil | 1pl.EXCL.NOM possum there see.NPST", "r265_1pl_excl_target_there_see_yawa", ["nganjin", "yawa", "yinyay", "nyajil"], EVIDENCE["yawa"]),
    row(41, "We see the sugar glider here.", "Ngana bala yaluy nyajil.", "ngana bala yaluy nyajil | 1pl.INC.NOM sugar.glider here see.NPST", "r265_1pl_incl_target_here_see_bala", ["ngana", "bala", "yaluy", "nyajil"], EVIDENCE["bala"]),
    row(42, "We see the grass wallaby here.", "Ngana darrba yaluy nyajil.", "ngana darrba yaluy nyajil | 1pl.INC.NOM grass.wallaby here see.NPST", "r265_1pl_incl_target_here_see_darrba", ["ngana", "darrba", "yaluy", "nyajil"], EVIDENCE["darrba"]),
    row(43, "We see the tree kangaroo here.", "Ngana jarrabina yaluy nyajil.", "ngana jarrabina yaluy nyajil | 1pl.INC.NOM tree.kangaroo here see.NPST", "r265_1pl_incl_target_here_see_jarrabina", ["ngana", "jarrabina", "yaluy", "nyajil"], EVIDENCE["jarrabina"]),
    row(44, "We see the kangaroo rat here.", "Ngana jayju yaluy nyajil.", "ngana jayju yaluy nyajil | 1pl.INC.NOM kangaroo.rat here see.NPST", "r265_1pl_incl_target_here_see_jayju", ["ngana", "jayju", "yaluy", "nyajil"], EVIDENCE["jayju"]),
    row(45, "We see the marsupial native cat here.", "Ngana jungarr yaluy nyajil.", "ngana jungarr yaluy nyajil | 1pl.INC.NOM marsupial.native.cat here see.NPST", "r265_1pl_incl_target_here_see_jungarr", ["ngana", "jungarr", "yaluy", "nyajil"], EVIDENCE["jungarr"]),
    row(46, "We see the rat here.", "Ngana kukur yaluy nyajil.", "ngana kukur yaluy nyajil | 1pl.INC.NOM rat here see.NPST", "r265_1pl_incl_target_here_see_kukur", ["ngana", "kukur", "yaluy", "nyajil"], EVIDENCE["kukur"]),
    row(47, "We see the small bat here.", "Ngana mali yaluy nyajil.", "ngana mali yaluy nyajil | 1pl.INC.NOM small.bat here see.NPST", "r265_1pl_incl_target_here_see_mali", ["ngana", "mali", "yaluy", "nyajil"], EVIDENCE["mali"]),
    row(48, "We see the dingo here.", "Ngana murramu yaluy nyajil.", "ngana murramu yaluy nyajil | 1pl.INC.NOM dingo here see.NPST", "r265_1pl_incl_target_here_see_murramu", ["ngana", "murramu", "yaluy", "nyajil"], EVIDENCE["murramu"]),
    row(49, "We see the bandicoot here.", "Ngana ngumbi yaluy nyajil.", "ngana ngumbi yaluy nyajil | 1pl.INC.NOM bandicoot here see.NPST", "r265_1pl_incl_target_here_see_ngumbi", ["ngana", "ngumbi", "yaluy", "nyajil"], EVIDENCE["ngumbi"]),
    row(50, "We see the possum here.", "Ngana yawa yaluy nyajil.", "ngana yawa yaluy nyajil | 1pl.INC.NOM possum here see.NPST", "r265_1pl_incl_target_here_see_yawa", ["ngana", "yawa", "yaluy", "nyajil"], EVIDENCE["yawa"]),
]


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("r265_"):
            issues.append(f"{label}: Rule 265 frame must start with r265_")
        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 265")
        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 265 target mammal 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")
        pronouns = [pronoun for pronoun in PRONOUN_SUPPORT if has_token(low, pronoun)]
        if len(pronouns) != 1:
            issues.append(f"{label}: rows require exactly one bare nominative non-singular pronoun, found {pronouns}")
        if 1 <= seq <= 10 and (not low.startswith("ngali ") or not low.endswith(" yaluy nyajil.") or len(low.split()) != 4):
            issues.append(f"{label}: rows 1-10 require Ngali TARGET yaluy nyajil")
        if 11 <= seq <= 20 and (not low.startswith("ngaliny ") or not low.endswith(" yinyay nyajil.") or len(low.split()) != 4):
            issues.append(f"{label}: rows 11-20 require Ngaliny TARGET yinyay nyajil")
        if 21 <= seq <= 30 and (not low.startswith("yubal ") or not low.endswith(" yaluy nyajil.") or len(low.split()) != 4):
            issues.append(f"{label}: rows 21-30 require Yubal TARGET yaluy nyajil")
        if 31 <= seq <= 40 and (not low.startswith("nganjin ") or not low.endswith(" yinyay nyajil.") or len(low.split()) != 4):
            issues.append(f"{label}: rows 31-40 require Nganjin TARGET yinyay nyajil")
        if 41 <= seq <= 50 and (not low.startswith("ngana ") or not low.endswith(" yaluy nyajil.") or len(low.split()) != 4):
            issues.append(f"{label}: rows 41-50 require Ngana TARGET yaluy nyajil")
        if any(english_has_denied(english_low, bad) for bad in DENY_ENGLISH):
            issues.append(f"{label}: denied mammal behavior/use/habitat/story/body/source-prose 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 lessons 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:
        db.execute(
            "INSERT INTO batches(label, cheatsheet_version, guide_version, notes, created_at) VALUES(?,?,?,?,?)",
            (LABEL, "Rule 265 draft", "operator-guide-2026-07-09", "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}: {kuku} -> {found}")
    if collisions:
        raise SystemExit("collision scan failed:\n" + "\n".join(collisions))

    db.executemany(
        """
        INSERT INTO sentences(
          batch_id, seq, english, kuku, analysis, frame, tier, words_used,
          loans_used, evidence, status, confidence, created_at
        )
        VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)
        """,
        [
            (
                batch_id,
                seq,
                english,
                kuku,
                analysis,
                frame,
                tier,
                words_used,
                loans_used,
                evidence,
                "under_review",
                confidence,
                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="Rule 265 source pass checked dictionary lines for bala sugar glider, darrba grass wallaby, jarrabina tree kangaroo, jayju kangaroo rat, jungarr marsupial native cat, kukur rat, mali small bat, murramu dingo, ngumbi bandicoot, and yawa possum; Patz grammar lines 2577-2603 for bare nominative ngali, ngaliny, yubal, nganjin, and ngana; lines 2682-2684 for inclusive/exclusive and second-person non-singular discussion; lines 2838-2845 for yaluy/yinyay; and lines 3043-3049 for nyaji-l.",
        artifact_path=PROCESS_LOG,
        created_at=NOW,
    )
    record_event(
        db,
        batch_label=LABEL,
        phase="planning",
        event_type="debt-sweep-scope",
        actor="claude-main",
        detail="Selected Rule 265 complementary mammal non-singular pronoun perception sweep after Rule 264: same ten checked Rule 219/253/254 mammal labels in zero-collision nominative-pronoun perception bands using ngali, ngaliny, yubal, nganjin, and ngana. All mammal behavior/use/habitat/story/body/source-prose, target-case, pronoun-case, demonstrative-case, and perception-compound semantics remain closed.",
        artifact_path=PROCESS_LOG,
        created_at=NOW,
    )
    record_event(
        db,
        batch_label=LABEL,
        phase="preflight",
        event_type="coverage-audit",
        actor="claude-main",
        detail="Pre-batch token-aware target coverage among accepted/revised rows after Rule 264: bala 24, darrba 53, jarrabina 25, jayju 23, jungarr 22, kukur 26, mali 30, murramu 43, ngumbi 23, and yawa 49. Support coverage: ngali 209, ngaliny 144, yubal 155, nganjin 161, ngana 909, yaluy 2473, yinyay 2726, nyajil 3067.",
        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 the final 50 Rule 265 Kuku surfaces returned zero accepted, revised, or under-review collisions before banking.",
        command="sqlite3 synthetic.db planned surface collision query",
        output_excerpt="0 rows",
        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 rows, one checked mammal target per row, one bare nominative non-singular pronoun per row, correct complementary yaluy/yinyay band, no loans, no denied Kuku tokens, no denied English behavior/use/habitat/story/body/source-prose fields, no target morphology, no support inflection.",
        command="python3 batch_scripts/bank_307.py scan_static",
        output_excerpt="static guard passed before insert",
        artifact_path="batch_scripts/bank_307.py",
        created_at=NOW,
    )
    record_event(
        db,
        batch_label=LABEL,
        phase="bank",
        event_type="bank-success",
        actor="claude-main",
        detail="Banked 50 hand-authored Rule 265 complementary mammal non-singular pronoun perception rows as under_review.",
        command="python3 batch_scripts/bank_307.py",
        artifact_path="batch_scripts/bank_307.py",
        created_at=NOW,
    )
    db.commit()
    count = db.execute("SELECT COUNT(*) FROM sentences WHERE batch_id=?", (batch_id,)).fetchone()[0]
    print(f"banked {LABEL}: {count} rows under_review")
    return 0


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