#!/usr/bin/env python3
"""Bank Batch 390: Rule 348 bird-set-D non-singular past perception."""

from __future__ import annotations

import datetime as dt
import json
import re
import sqlite3

from bank_386 import SUPPORT_EVIDENCE as PRONOUN_EVIDENCE, source_audit as pronoun_source_audit
from bank_388 import (
    DENY_ENGLISH,
    EXPECTED_HASHES,
    SUPPORT_EVIDENCE as BIRD_SUPPORT_EVIDENCE,
    TARGET_CONFIDENCE,
    TARGET_EVIDENCE as BIRD_TARGET_EVIDENCE,
    TARGET_GLOSSES,
    TARGET_LABELS,
    TARGET_ORDER,
    count_words,
    source_audit as bird_source_audit,
)
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-390"
NOW = dt.datetime.now(dt.timezone.utc).replace(microsecond=0).isoformat().replace("+00:00", "Z")
PROCESS_LOG = "process_logs/batch-390-bird-set-d-nonsingular-past-perception-after-rule347-process.md"
EXPECTED_TARGET_COVERAGE = {
    "diwalngku": 15, "bujil-yiran": 15, "karrmbil": 15, "yiki": 15, "bajal": 16,
    "biru-biru": 16, "jangkan": 16, "kurranday": 16, "ngarkay": 16, "wakuka": 17,
}
EXPECTED_SUPPORT_COVERAGE = {
    "ngali": 673, "ngaliny": 615, "yubal": 625, "yurra": 610, "nganjin": 338,
    "ngana": 1115, "yaluy": 4582, "yinyay": 4754, "nyajin": 2450,
}
FRAME_SPECS = [
    ("ngali", "yaluy", "We two, not you, saw", "here", "1du.excl"),
    ("ngaliny", "yinyay", "You and I saw", "there", "1du.incl"),
    ("yubal", "yaluy", "You two saw", "here", "2du"),
    ("yurra", "yinyay", "You all saw", "there", "2pl"),
    ("nganjin", "yaluy", "We, not you, saw", "here", "1pl.excl"),
]
TARGET_EVIDENCE = {
    target: f"{BIRD_TARGET_EVIDENCE[target]} Rule 348 changes participant number/clusivity only and preserves every Rule 346-347 card and source boundary."
    for target in TARGET_ORDER
}
SUPPORT_EVIDENCE = (
    f"{BIRD_SUPPORT_EVIDENCE} {PRONOUN_EVIDENCE} Rule 348 combines these independently audited lexical and "
    "participant sources; deferred ngana remains grammatical and unselected."
)
DENY_KUKU = {
    "ngayu", "yundu", "nyulu", "bula", "jana", "ngana", "yulu", "iana", "biru", "bujil", "yiran",
    "ngalingan", "ngalinyan", "yubalan", "yurrangan", "nganjinan", "yalay", "yala", "yanyu", "yinya",
    "nyajil", "nyaji-l", "nyaji-nka", "nyaji-l-nyaji", "milka", "baykal", "jalngkay",
}


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


ROWS = [
    row(1, "We two, not you, saw the diwalngku bird here.", "Ngali diwalngku yaluy nyajin.", "ngali diwalngku yaluy nyajin | 1du.excl.NOM diwalngku.bird here see.PST", "r348_1du_excl_here_bird_d_saw_diwalngku", ["ngali", "diwalngku", "yaluy", "nyajin"]),
    row(2, "We two, not you, saw the bujil-yiran ibis here.", "Ngali bujil-yiran yaluy nyajin.", "ngali bujil-yiran yaluy nyajin | 1du.excl.NOM bujil-yiran.ibis here see.PST", "r348_1du_excl_here_bird_d_saw_bujil_yiran", ["ngali", "bujil-yiran", "yaluy", "nyajin"]),
    row(3, "We two, not you, saw the karrmbil parakeet here.", "Ngali karrmbil yaluy nyajin.", "ngali karrmbil yaluy nyajin | 1du.excl.NOM karrmbil.parakeet here see.PST", "r348_1du_excl_here_bird_d_saw_karrmbil", ["ngali", "karrmbil", "yaluy", "nyajin"]),
    row(4, "We two, not you, saw the yiki owl here.", "Ngali yiki yaluy nyajin.", "ngali yiki yaluy nyajin | 1du.excl.NOM yiki.owl here see.PST", "r348_1du_excl_here_bird_d_saw_yiki", ["ngali", "yiki", "yaluy", "nyajin"]),
    row(5, "We two, not you, saw the bajal bower bird here.", "Ngali bajal yaluy nyajin.", "ngali bajal yaluy nyajin | 1du.excl.NOM bajal.bower-bird here see.PST", "r348_1du_excl_here_bird_d_saw_bajal", ["ngali", "bajal", "yaluy", "nyajin"]),
    row(6, "We two, not you, saw the biru-biru rainbow bird here.", "Ngali biru-biru yaluy nyajin.", "ngali biru-biru yaluy nyajin | 1du.excl.NOM biru-biru.rainbow-bird here see.PST", "r348_1du_excl_here_bird_d_saw_biru_biru", ["ngali", "biru-biru", "yaluy", "nyajin"]),
    row(7, "We two, not you, saw the jangkan swift here.", "Ngali jangkan yaluy nyajin.", "ngali jangkan yaluy nyajin | 1du.excl.NOM jangkan.swift here see.PST", "r348_1du_excl_here_bird_d_saw_jangkan", ["ngali", "jangkan", "yaluy", "nyajin"]),
    row(8, "We two, not you, saw the kurranday seagull here.", "Ngali kurranday yaluy nyajin.", "ngali kurranday yaluy nyajin | 1du.excl.NOM kurranday.seagull here see.PST", "r348_1du_excl_here_bird_d_saw_kurranday", ["ngali", "kurranday", "yaluy", "nyajin"]),
    row(9, "We two, not you, saw the ngarkay waterhen here.", "Ngali ngarkay yaluy nyajin.", "ngali ngarkay yaluy nyajin | 1du.excl.NOM ngarkay.waterhen here see.PST", "r348_1du_excl_here_bird_d_saw_ngarkay", ["ngali", "ngarkay", "yaluy", "nyajin"]),
    row(10, "We two, not you, saw the wakuka bird here.", "Ngali wakuka yaluy nyajin.", "ngali wakuka yaluy nyajin | 1du.excl.NOM wakuka.bird here see.PST", "r348_1du_excl_here_bird_d_saw_wakuka", ["ngali", "wakuka", "yaluy", "nyajin"]),
    row(11, "You and I saw the diwalngku bird there.", "Ngaliny diwalngku yinyay nyajin.", "ngaliny diwalngku yinyay nyajin | 1du.incl.NOM diwalngku.bird there see.PST", "r348_1du_incl_there_bird_d_saw_diwalngku", ["ngaliny", "diwalngku", "yinyay", "nyajin"]),
    row(12, "You and I saw the bujil-yiran ibis there.", "Ngaliny bujil-yiran yinyay nyajin.", "ngaliny bujil-yiran yinyay nyajin | 1du.incl.NOM bujil-yiran.ibis there see.PST", "r348_1du_incl_there_bird_d_saw_bujil_yiran", ["ngaliny", "bujil-yiran", "yinyay", "nyajin"]),
    row(13, "You and I saw the karrmbil parakeet there.", "Ngaliny karrmbil yinyay nyajin.", "ngaliny karrmbil yinyay nyajin | 1du.incl.NOM karrmbil.parakeet there see.PST", "r348_1du_incl_there_bird_d_saw_karrmbil", ["ngaliny", "karrmbil", "yinyay", "nyajin"]),
    row(14, "You and I saw the yiki owl there.", "Ngaliny yiki yinyay nyajin.", "ngaliny yiki yinyay nyajin | 1du.incl.NOM yiki.owl there see.PST", "r348_1du_incl_there_bird_d_saw_yiki", ["ngaliny", "yiki", "yinyay", "nyajin"]),
    row(15, "You and I saw the bajal bower bird there.", "Ngaliny bajal yinyay nyajin.", "ngaliny bajal yinyay nyajin | 1du.incl.NOM bajal.bower-bird there see.PST", "r348_1du_incl_there_bird_d_saw_bajal", ["ngaliny", "bajal", "yinyay", "nyajin"]),
    row(16, "You and I saw the biru-biru rainbow bird there.", "Ngaliny biru-biru yinyay nyajin.", "ngaliny biru-biru yinyay nyajin | 1du.incl.NOM biru-biru.rainbow-bird there see.PST", "r348_1du_incl_there_bird_d_saw_biru_biru", ["ngaliny", "biru-biru", "yinyay", "nyajin"]),
    row(17, "You and I saw the jangkan swift there.", "Ngaliny jangkan yinyay nyajin.", "ngaliny jangkan yinyay nyajin | 1du.incl.NOM jangkan.swift there see.PST", "r348_1du_incl_there_bird_d_saw_jangkan", ["ngaliny", "jangkan", "yinyay", "nyajin"]),
    row(18, "You and I saw the kurranday seagull there.", "Ngaliny kurranday yinyay nyajin.", "ngaliny kurranday yinyay nyajin | 1du.incl.NOM kurranday.seagull there see.PST", "r348_1du_incl_there_bird_d_saw_kurranday", ["ngaliny", "kurranday", "yinyay", "nyajin"]),
    row(19, "You and I saw the ngarkay waterhen there.", "Ngaliny ngarkay yinyay nyajin.", "ngaliny ngarkay yinyay nyajin | 1du.incl.NOM ngarkay.waterhen there see.PST", "r348_1du_incl_there_bird_d_saw_ngarkay", ["ngaliny", "ngarkay", "yinyay", "nyajin"]),
    row(20, "You and I saw the wakuka bird there.", "Ngaliny wakuka yinyay nyajin.", "ngaliny wakuka yinyay nyajin | 1du.incl.NOM wakuka.bird there see.PST", "r348_1du_incl_there_bird_d_saw_wakuka", ["ngaliny", "wakuka", "yinyay", "nyajin"]),
    row(21, "You two saw the diwalngku bird here.", "Yubal diwalngku yaluy nyajin.", "yubal diwalngku yaluy nyajin | 2du.NOM diwalngku.bird here see.PST", "r348_2du_here_bird_d_saw_diwalngku", ["yubal", "diwalngku", "yaluy", "nyajin"]),
    row(22, "You two saw the bujil-yiran ibis here.", "Yubal bujil-yiran yaluy nyajin.", "yubal bujil-yiran yaluy nyajin | 2du.NOM bujil-yiran.ibis here see.PST", "r348_2du_here_bird_d_saw_bujil_yiran", ["yubal", "bujil-yiran", "yaluy", "nyajin"]),
    row(23, "You two saw the karrmbil parakeet here.", "Yubal karrmbil yaluy nyajin.", "yubal karrmbil yaluy nyajin | 2du.NOM karrmbil.parakeet here see.PST", "r348_2du_here_bird_d_saw_karrmbil", ["yubal", "karrmbil", "yaluy", "nyajin"]),
    row(24, "You two saw the yiki owl here.", "Yubal yiki yaluy nyajin.", "yubal yiki yaluy nyajin | 2du.NOM yiki.owl here see.PST", "r348_2du_here_bird_d_saw_yiki", ["yubal", "yiki", "yaluy", "nyajin"]),
    row(25, "You two saw the bajal bower bird here.", "Yubal bajal yaluy nyajin.", "yubal bajal yaluy nyajin | 2du.NOM bajal.bower-bird here see.PST", "r348_2du_here_bird_d_saw_bajal", ["yubal", "bajal", "yaluy", "nyajin"]),
    row(26, "You two saw the biru-biru rainbow bird here.", "Yubal biru-biru yaluy nyajin.", "yubal biru-biru yaluy nyajin | 2du.NOM biru-biru.rainbow-bird here see.PST", "r348_2du_here_bird_d_saw_biru_biru", ["yubal", "biru-biru", "yaluy", "nyajin"]),
    row(27, "You two saw the jangkan swift here.", "Yubal jangkan yaluy nyajin.", "yubal jangkan yaluy nyajin | 2du.NOM jangkan.swift here see.PST", "r348_2du_here_bird_d_saw_jangkan", ["yubal", "jangkan", "yaluy", "nyajin"]),
    row(28, "You two saw the kurranday seagull here.", "Yubal kurranday yaluy nyajin.", "yubal kurranday yaluy nyajin | 2du.NOM kurranday.seagull here see.PST", "r348_2du_here_bird_d_saw_kurranday", ["yubal", "kurranday", "yaluy", "nyajin"]),
    row(29, "You two saw the ngarkay waterhen here.", "Yubal ngarkay yaluy nyajin.", "yubal ngarkay yaluy nyajin | 2du.NOM ngarkay.waterhen here see.PST", "r348_2du_here_bird_d_saw_ngarkay", ["yubal", "ngarkay", "yaluy", "nyajin"]),
    row(30, "You two saw the wakuka bird here.", "Yubal wakuka yaluy nyajin.", "yubal wakuka yaluy nyajin | 2du.NOM wakuka.bird here see.PST", "r348_2du_here_bird_d_saw_wakuka", ["yubal", "wakuka", "yaluy", "nyajin"]),
    row(31, "You all saw the diwalngku bird there.", "Yurra diwalngku yinyay nyajin.", "yurra diwalngku yinyay nyajin | 2pl.NOM diwalngku.bird there see.PST", "r348_2pl_there_bird_d_saw_diwalngku", ["yurra", "diwalngku", "yinyay", "nyajin"]),
    row(32, "You all saw the bujil-yiran ibis there.", "Yurra bujil-yiran yinyay nyajin.", "yurra bujil-yiran yinyay nyajin | 2pl.NOM bujil-yiran.ibis there see.PST", "r348_2pl_there_bird_d_saw_bujil_yiran", ["yurra", "bujil-yiran", "yinyay", "nyajin"]),
    row(33, "You all saw the karrmbil parakeet there.", "Yurra karrmbil yinyay nyajin.", "yurra karrmbil yinyay nyajin | 2pl.NOM karrmbil.parakeet there see.PST", "r348_2pl_there_bird_d_saw_karrmbil", ["yurra", "karrmbil", "yinyay", "nyajin"]),
    row(34, "You all saw the yiki owl there.", "Yurra yiki yinyay nyajin.", "yurra yiki yinyay nyajin | 2pl.NOM yiki.owl there see.PST", "r348_2pl_there_bird_d_saw_yiki", ["yurra", "yiki", "yinyay", "nyajin"]),
    row(35, "You all saw the bajal bower bird there.", "Yurra bajal yinyay nyajin.", "yurra bajal yinyay nyajin | 2pl.NOM bajal.bower-bird there see.PST", "r348_2pl_there_bird_d_saw_bajal", ["yurra", "bajal", "yinyay", "nyajin"]),
    row(36, "You all saw the biru-biru rainbow bird there.", "Yurra biru-biru yinyay nyajin.", "yurra biru-biru yinyay nyajin | 2pl.NOM biru-biru.rainbow-bird there see.PST", "r348_2pl_there_bird_d_saw_biru_biru", ["yurra", "biru-biru", "yinyay", "nyajin"]),
    row(37, "You all saw the jangkan swift there.", "Yurra jangkan yinyay nyajin.", "yurra jangkan yinyay nyajin | 2pl.NOM jangkan.swift there see.PST", "r348_2pl_there_bird_d_saw_jangkan", ["yurra", "jangkan", "yinyay", "nyajin"]),
    row(38, "You all saw the kurranday seagull there.", "Yurra kurranday yinyay nyajin.", "yurra kurranday yinyay nyajin | 2pl.NOM kurranday.seagull there see.PST", "r348_2pl_there_bird_d_saw_kurranday", ["yurra", "kurranday", "yinyay", "nyajin"]),
    row(39, "You all saw the ngarkay waterhen there.", "Yurra ngarkay yinyay nyajin.", "yurra ngarkay yinyay nyajin | 2pl.NOM ngarkay.waterhen there see.PST", "r348_2pl_there_bird_d_saw_ngarkay", ["yurra", "ngarkay", "yinyay", "nyajin"]),
    row(40, "You all saw the wakuka bird there.", "Yurra wakuka yinyay nyajin.", "yurra wakuka yinyay nyajin | 2pl.NOM wakuka.bird there see.PST", "r348_2pl_there_bird_d_saw_wakuka", ["yurra", "wakuka", "yinyay", "nyajin"]),
    row(41, "We, not you, saw the diwalngku bird here.", "Nganjin diwalngku yaluy nyajin.", "nganjin diwalngku yaluy nyajin | 1pl.excl.NOM diwalngku.bird here see.PST", "r348_1pl_excl_here_bird_d_saw_diwalngku", ["nganjin", "diwalngku", "yaluy", "nyajin"]),
    row(42, "We, not you, saw the bujil-yiran ibis here.", "Nganjin bujil-yiran yaluy nyajin.", "nganjin bujil-yiran yaluy nyajin | 1pl.excl.NOM bujil-yiran.ibis here see.PST", "r348_1pl_excl_here_bird_d_saw_bujil_yiran", ["nganjin", "bujil-yiran", "yaluy", "nyajin"]),
    row(43, "We, not you, saw the karrmbil parakeet here.", "Nganjin karrmbil yaluy nyajin.", "nganjin karrmbil yaluy nyajin | 1pl.excl.NOM karrmbil.parakeet here see.PST", "r348_1pl_excl_here_bird_d_saw_karrmbil", ["nganjin", "karrmbil", "yaluy", "nyajin"]),
    row(44, "We, not you, saw the yiki owl here.", "Nganjin yiki yaluy nyajin.", "nganjin yiki yaluy nyajin | 1pl.excl.NOM yiki.owl here see.PST", "r348_1pl_excl_here_bird_d_saw_yiki", ["nganjin", "yiki", "yaluy", "nyajin"]),
    row(45, "We, not you, saw the bajal bower bird here.", "Nganjin bajal yaluy nyajin.", "nganjin bajal yaluy nyajin | 1pl.excl.NOM bajal.bower-bird here see.PST", "r348_1pl_excl_here_bird_d_saw_bajal", ["nganjin", "bajal", "yaluy", "nyajin"]),
    row(46, "We, not you, saw the biru-biru rainbow bird here.", "Nganjin biru-biru yaluy nyajin.", "nganjin biru-biru yaluy nyajin | 1pl.excl.NOM biru-biru.rainbow-bird here see.PST", "r348_1pl_excl_here_bird_d_saw_biru_biru", ["nganjin", "biru-biru", "yaluy", "nyajin"]),
    row(47, "We, not you, saw the jangkan swift here.", "Nganjin jangkan yaluy nyajin.", "nganjin jangkan yaluy nyajin | 1pl.excl.NOM jangkan.swift here see.PST", "r348_1pl_excl_here_bird_d_saw_jangkan", ["nganjin", "jangkan", "yaluy", "nyajin"]),
    row(48, "We, not you, saw the kurranday seagull here.", "Nganjin kurranday yaluy nyajin.", "nganjin kurranday yaluy nyajin | 1pl.excl.NOM kurranday.seagull here see.PST", "r348_1pl_excl_here_bird_d_saw_kurranday", ["nganjin", "kurranday", "yaluy", "nyajin"]),
    row(49, "We, not you, saw the ngarkay waterhen here.", "Nganjin ngarkay yaluy nyajin.", "nganjin ngarkay yaluy nyajin | 1pl.excl.NOM ngarkay.waterhen here see.PST", "r348_1pl_excl_here_bird_d_saw_ngarkay", ["nganjin", "ngarkay", "yaluy", "nyajin"]),
    row(50, "We, not you, saw the wakuka bird here.", "Nganjin wakuka yaluy nyajin.", "nganjin wakuka yaluy nyajin | 1pl.excl.NOM wakuka.bird here see.PST", "r348_1pl_excl_here_bird_d_saw_wakuka", ["nganjin", "wakuka", "yaluy", "nyajin"]),
]


def scan_static() -> list[str]:
    issues = []
    if len(ROWS) != 50 or [r[0] for r in ROWS] != list(range(1, 51)):
        issues.append("row count/sequence")
    if len({r[1] for r in ROWS}) != 50 or len({r[2] for r in ROWS}) != 50:
        issues.append("duplicate surface")
    for seq, english, kuku, analysis, frame, _tier, words_json, loans_json, evidence, confidence in ROWS:
        p, d, prefix, location, person = FRAME_SPECS[(seq - 1) // 10]
        target = TARGET_ORDER[(seq - 1) % 10]
        words = json.loads(words_json)
        if json.loads(loans_json): issues.append(f"{seq}: loans")
        denied = sorted(w for w in DENY_KUKU if re.search(rf"(^|\s){re.escape(w)}([\s\.]|$)", kuku.lower()))
        drift = [x for x in DENY_ENGLISH if re.search(rf"\b{re.escape(x)}\b", english.lower())]
        if denied: issues.append(f"{seq}: denied {denied}")
        if drift: issues.append(f"{seq}: drift {drift}")
        if kuku != f"{p.capitalize()} {target} {d} nyajin.": issues.append(f"{seq}: Kuku")
        if english != f"{prefix} the {TARGET_LABELS[target]} {location}.": issues.append(f"{seq}: English")
        if analysis != f"{p} {target} {d} nyajin | {person}.NOM {TARGET_GLOSSES[target]} {location} see.PST": issues.append(f"{seq}: analysis")
        if words != [p, target, d, "nyajin"] or kuku.lower().rstrip(".") != " ".join(words): issues.append(f"{seq}: words")
        if not frame.startswith("r348_"): issues.append(f"{seq}: frame")
        if TARGET_EVIDENCE[target] not in evidence or SUPPORT_EVIDENCE not in evidence: issues.append(f"{seq}: evidence")
        if confidence != TARGET_CONFIDENCE[target]: issues.append(f"{seq}: confidence")
    return issues


def main() -> int:
    bird_records, bird_hits = bird_source_audit()
    _pronoun_target_records, pronoun_records = pronoun_source_audit()
    issues = scan_static()
    if issues:
        raise SystemExit("\n".join(issues))
    db = sqlite3.connect(DB); db.execute("PRAGMA foreign_keys=ON"); db.execute("BEGIN"); ensure_schema(db)
    if db.execute("SELECT 1 FROM batches WHERE label=?", (LABEL,)).fetchone() or db.execute("SELECT COUNT(*) FROM sentences WHERE status='under_review'").fetchone()[0]:
        raise SystemExit("batch exists or another batch open")
    tc = count_words(db, TARGET_ORDER); sc = count_words(db, list(EXPECTED_SUPPORT_COVERAGE))
    if tc != EXPECTED_TARGET_COVERAGE or sc != EXPECTED_SUPPORT_COVERAGE:
        raise SystemExit(f"coverage changed {tc} {sc}")
    collisions = []
    for p in [x[0] for x in FRAME_SPECS]:
        for t in TARGET_ORDER:
            for d in ("yaluy", "yinyay"):
                surface = f"{p.capitalize()} {t} {d} nyajin."
                hits = db.execute("SELECT b.label,s.seq FROM sentences s JOIN batches b ON b.id=s.batch_id WHERE lower(s.kuku)=lower(?) AND s.status!='rejected'", (surface,)).fetchall()
                if hits: collisions.append((surface, hits))
    if collisions: raise SystemExit(f"matrix changed {collisions}")
    verified, words = db.execute("SELECT COUNT(*),SUM(LENGTH(kuku)-LENGTH(REPLACE(kuku,' ',''))+1) FROM sentences WHERE status IN('accepted','revised')").fetchone()
    if (verified, words) != (19547, 133547): raise SystemExit(f"baseline {verified}/{words}")
    notes = "BIRD SET-D NON-SINGULAR PAST PERCEPTION AFTER RULE 347 (Rule 348). 50 under-review rows; all 100 cells empty; target/card/Patz and pronoun audits passed; all boundaries closed; selected gap 172->162."
    batch_id = db.execute("INSERT INTO batches(label,cheatsheet_version,guide_version,notes,created_at) VALUES(?,?,?,?,?)", (LABEL, "Rule 348 under review", "operator-guide-2026-07-10", notes, NOW)).lastrowid
    db.executemany("""INSERT INTO sentences(batch_id,seq,english,kuku,analysis,frame,tier,words_used,loans_used,evidence,status,confidence,rights_status,author,created_at,updated_at) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)""", [(batch_id, seq, en, ku, an, fr, tier, w, loans, ev, "under_review", conf, "project_approved_synthetic_pending_elder_verification", "codex-main", NOW, NOW) for seq,en,ku,an,fr,tier,w,loans,ev,conf in ROWS])
    events = [
        ("source", "source-audit-pass", f"Bird records={bird_records}; Patz hits={bird_hits}; pronoun records={pronoun_records}; hashes={EXPECTED_HASHES}."),
        ("source", "pronoun-ruling", "Patz ngali exclusive / ngaliny inclusive controls over dictionary defect; ngana grammatical and deferred."),
        ("planning", "sense-preservation-audit", "All Rule 346-347 target card/taxonomy/whole-head/culture/property/habitat/place/event boundaries retained."),
        ("planning", "construction-gap-audit", "All 100 non-singular x target x deictic cells absent; both orientations unique."),
        ("planning", "balance-audit", "Gap 172; selected projects 162; inverse 182."),
        ("preflight", "coverage-audit", f"targets={tc}; support={sc}."),
        ("preflight", "duplicate-preflight", "50 selected surfaces unique and absent; full matrix empty."),
        ("preflight", "static-guard-pass", "50 explicit rows; all static/source/sense/clusivity contracts passed."),
    ]
    for phase, et, detail in events: record_event(db,batch_label=LABEL,phase=phase,event_type=et,actor="codex",detail=detail,artifact_path=PROCESS_LOG,created_at=NOW)
    record_event(db,batch_label=LABEL,phase="bank",event_type="bank-success",actor="codex",detail=f"Banked 50 under_review; verified {verified}; words {words}.",command="python3 batch_scripts/bank_390.py",artifact_path=PROCESS_LOG,created_at=NOW)
    db.commit(); print("banked batch-390: 50 rows under_review"); return 0


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