#!/usr/bin/env python3
"""Close batch-316 after main-loop five-lens review."""

from __future__ import annotations

import datetime as dt
import json
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-316"
NOW = dt.datetime.now(dt.timezone.utc).replace(microsecond=0).isoformat().replace("+00:00", "Z")
PROCESS_LOG = "process_logs/batch-316-fire-reference-complementary-past-pronoun-perception-after-rule273-process.md"
LENSES = ("morphology", "lexicon", "syntax", "fidelity", "register")

NOTES = (
    "FIRE-REFERENCE COMPLEMENTARY PAST PRONOUN PERCEPTION SWEEP AFTER RULE 273 (Rule 274). 50 rows. TARGETS: "
    "baya fire, jimal fire stick, jirramar small sticks, kubu smoke, kunjin fire, miyilkura smouldering log, "
    "ngalku bush fire, ngaluri flames, ngunjil charcoal, and wula flames. SUPPORT: bare nominative ngayu, yundu, "
    "nyulu, bula, and ngana plus yaluy/yinyay and nyajin. FRAMES: Ngayu TARGET yaluy nyajin, Yundu TARGET yinyay "
    "nyajin, Nyulu TARGET yaluy nyajin, Bula TARGET yinyay nyajin, and Ngana TARGET yinyay nyajin. REVIEW: no new "
    "fire lexeme, fire-making, lighting, firewood, collecting, cooking, burning, clearing, spreading, leaping-flame "
    "event, hot-coal/ash extension, smoke-source, smoke-out, loan, ritual-smoke prose, body-fluid homograph, "
    "mullet/place/tadpole homograph, target case, target compound, productive reduplication, pronoun case, "
    "demonstrative case, perception compound, support inflection, nonpast drift, or recent insect/fish/bird/mammal/"
    "reptile/shellfish/body/landform/dwelling drift was opened."
)

LESSONS = [
    ("source", "Rule 274 retained the ten checked Rule 236/246/250/272/273 fire-reference noun heads: baya, jimal, jirramar, kubu, kunjin, miyilkura, ngalku, ngaluri, ngunjil, and wula.", "Rule 274: retained target inventory."),
    ("source", "Direct dictionary line readback was explicit: baya 1948-1962, jimal 9258-9268, jirramar 9863-9875, kubu 14707-14715, kunjin 15880-15888, miyilkura 19985-19993, ngalku 21918-21926, ngaluri 22014-22025, ngunjil 23679-23688, and wula 27719-27729.", "Rule 274: dictionary source lines."),
    ("source", "Patz grammar lines 2577-2586 and 2592-2603 supplied bare nominative ngayu, yundu, nyulu, bula, and ngana.", "Rule 274: pronoun source."),
    ("source", "Patz grammar lines 2682-2686 supported ngana as first plural inclusive and bula as third dual.", "Rule 274: ngana and bula source."),
    ("source", "Patz Table 3.12 lines 2838-2845 supplied Yalanji yaluy 'here' and yinyay 'there'; Rule 274 used only bare local demonstratives.", "Rule 274: demonstrative source."),
    ("source", "Patz lines 2568-2571 supplied nyaji-ny 'see-PAST' and lines 3043-3049 supplied nyaji-l 'see'; Rule 274 used corpus-conventional nyajin as the past perception surface only.", "Rule 274: past perception source."),
    ("process", "Rule 274 fills complementary zero-collision fire-reference past-perception bands after Rule 273, without reopening fire-use semantics.", "Rule 274: complementary past fire scope."),
    ("process", "Exact duplicate preflight over all 50 planned Rule 274 Kuku surfaces returned no accepted, revised, or under-review collisions.", "Rule 274: duplicate preflight."),
    ("process", "Token-aware target coverage before banking after Rule 273 was baya 483, jimal 58, jirramar 30, kubu 86, kunjin 36, miyilkura 30, ngalku 30, ngaluri 30, ngunjil 93, and wula 84.", "Rule 274: target coverage audit."),
    ("process", "Support coverage before banking after Rule 273 was ngayu 1341, yundu 1201, nyulu 1816, bula 244, ngana 990, yaluy 2704, yinyay 2948, and nyajin 1452.", "Rule 274: support coverage audit."),
    ("lexicon", "Baya remained fire only; firewood, baya wayu, lighting, collecting, cooking, and fire-use rows stayed closed.", "Rule 274: baya."),
    ("lexicon", "Jimal remained fire stick only; jimal damal fire-making, twirling, story, and jimalili rows stayed closed.", "Rule 274: jimal."),
    ("lexicon", "Jirramar remained small sticks only; kindling action, starting-fire use, collecting, and material-use rows stayed closed.", "Rule 274: jirramar."),
    ("lexicon", "Kubu remained smoke only; smoke-source, smoke-out, warning, motion, kubu-kubu tadpole, and use rows stayed closed.", "Rule 274: kubu."),
    ("lexicon", "Kunjin remained fire only; no contrast with baya and no kunjirr ritual-smoke/purification prose was imported.", "Rule 274: kunjin."),
    ("lexicon", "Miyilkura remained smouldering log only; burning, cooking, warming, ash, eye/miyil, and use rows stayed closed.", "Rule 274: miyilkura."),
    ("lexicon", "Ngalku remained bush fire only; clearing, spreading, grassfire, danger, warning, and ngalkun mullet rows stayed closed.", "Rule 274: ngalku."),
    ("lexicon", "Ngaluri remained flames only; the source usage with flames leaping up was not converted into a leap-up event predicate.", "Rule 274: ngaluri."),
    ("lexicon", "Ngunjil remained charcoal only; hot coals and hot ashes extensions stayed closed.", "Rule 274: ngunjil."),
    ("lexicon", "Wula remained flames only; dalngarri synonymy and nearby wulay verb drift stayed closed.", "Rule 274: wula."),
    ("morphology", "All Rule 274 target nouns stayed bare: no LOC, DAT, ABL, PERL, ERG, INST, COMIT, POSS, PRIV, EXCESS, plural, derivation, or target suffix was admitted.", "Rule 274: bare targets."),
    ("morphology", "The subject support forms stayed bare nominatives: ngayu, yundu, nyulu, bula, and ngana were not inflected for accusative, dative, locative, ablative, comitative, or possessive.", "Rule 274: bare nominative pronouns."),
    ("morphology", "Yaluy, yinyay, and nyajin stayed uninflected support heads; no demonstrative case, local-derived form, nonpast nyajil, purposive nyaji-nka, reduplicated watching form, or support suffix was admitted.", "Rule 274: uninflected support."),
    ("morphology", "No target compound, lexicalized trap compound, fire-use construction, smoke-source construction, or productive reduplication was built from the fire-reference heads.", "Rule 274: no compounds or reduplication."),
    ("morphology", "English lexical labels such as fire stick, small sticks, smouldering log, bush fire, flames, and charcoal remained target glosses, not Kuku morphology or new event predicates.", "Rule 274: label morphology guard."),
    ("syntax", "Rows 1-10 used Ngayu TARGET yaluy nyajin.", "Rule 274: 1sg here past perception band."),
    ("syntax", "Rows 11-20 used Yundu TARGET yinyay nyajin.", "Rule 274: 2sg there past perception band."),
    ("syntax", "Rows 21-30 used Nyulu TARGET yaluy nyajin.", "Rule 274: 3sg here past perception band."),
    ("syntax", "Rows 31-40 used Bula TARGET yinyay nyajin.", "Rule 274: 3du there past perception band."),
    ("syntax", "Rows 41-50 used Ngana TARGET yinyay nyajin.", "Rule 274: 1pl inclusive there past perception band."),
    ("fidelity", "English stayed literal past perception/reference prose: selected subject pronoun value, target label, here/there, and saw only.", "Rule 274: literal English."),
    ("fidelity", "The batch did not convert dictionary alternate glosses or source prose into fire-making, firewood, lighting, collecting, cooking, burning, clearing, spreading, smoke-source, smoke-out, leaping, hot-coal, ash, body-fluid, ritual, story, or use claims.", "Rule 274: source-prose restraint."),
    ("register", "Rows remained plain elicitation-style past perception clauses; no danger, warning, ceremony, story, environmental-management, cooking, or instruction register was introduced.", "Rule 274: register."),
    ("process", "Rules 149-273 carry-forward traps stayed closed, especially recent insect/fish/bird/mammal/reptile/shellfish source-prose traps plus older fire action/use/homograph traps.", "Rule 274: carry-forward closure."),
    ("handoff", "Next batch may continue from 15,897 if this closes; do not infer fire-making, firewood, cooking, burning, clearing, spreading, hot-coal/ash, smoke-source, body-fluid homograph, loan, target-case, pronoun-case, perception-compound, or support-inflection grammar from Rule 274.", "Rule 274: next cadence."),
]

LEXEMES = [
    ("baya", "noun", None, "NA", "fire", "batch-316: complementary past fire perception/reference only; firewood, baya wayu, lighting, collecting, cooking, and use rows closed."),
    ("jimal", "noun", None, "NA", "fire stick", "batch-316: complementary past fire-stick perception/reference only; jimal damal fire-making and jimalili story rows closed."),
    ("jirramar", "noun", None, "NA", "small sticks", "batch-316: complementary past small-sticks perception/reference only; starting-fire/kindling-use prose closed."),
    ("kubu", "noun", None, "NA", "smoke", "batch-316: complementary past smoke perception/reference only; smoke-source, smoke-out, kubu-kubu tadpole, and warning rows closed."),
    ("kunjin", "noun", None, "NA", "fire", "batch-316: complementary past fire perception/reference only; no asserted contrast with baya; kunjirr ritual-smoke prose closed."),
    ("miyilkura", "noun", None, "NA", "smouldering log", "batch-316: complementary past smouldering-log perception/reference only; burning, cooking, warming, ash, and eye/miyil drift closed."),
    ("ngalku", "noun", None, "NA", "bush fire", "batch-316: complementary past bush-fire perception/reference only; clearing, spreading, danger, warning, and ngalkun mullet closed."),
    ("ngaluri", "noun", None, "NA", "flames", "batch-316: complementary past flames perception/reference only; leap-up event predicate closed."),
    ("ngunjil", "noun", None, "NA", "charcoal", "batch-316: complementary past charcoal perception/reference only; hot coals and hot ashes extensions closed."),
    ("wula", "noun", None, "NA", "flames", "batch-316: complementary past flames perception/reference only; dalngarri synonymy and wulay verb drift closed."),
    ("ngayu", "pronoun", None, "NA", "first singular nominative", "batch-316: bare nominative subject support only."),
    ("yundu", "pronoun", None, "NA", "second singular nominative", "batch-316: bare nominative subject support only."),
    ("nyulu", "pronoun", None, "NA", "third singular nominative", "batch-316: bare nominative subject support only."),
    ("bula", "pronoun", None, "NA", "third dual nominative", "batch-316: bare nominative subject support only."),
    ("ngana", "pronoun", None, "NA", "first plural inclusive nominative", "batch-316: bare nominative subject support only."),
    ("yaluy", "demonstrative", None, "NA", "here", "batch-316: bare local demonstrative support only."),
    ("yinyay", "demonstrative", None, "NA", "there", "batch-316: bare local demonstrative support only."),
    ("nyajin", "verb", "l", "NA", "saw", "batch-316: past perception support only, corpus-conventional surface from nyaji-ny see-PAST."),
]


def main() -> int:
    db = sqlite3.connect(DB)
    db.execute("PRAGMA foreign_keys = ON")
    db.execute("BEGIN")
    ensure_schema(db)
    batch = db.execute("SELECT id FROM batches WHERE label=?", (LABEL,)).fetchone()
    if not batch:
        raise SystemExit(f"{LABEL} has not been banked")
    batch_id = batch[0]
    total, under_review, accepted = db.execute(
        """
        SELECT
          COUNT(*),
          COUNT(*) FILTER (WHERE status='under_review'),
          COUNT(*) FILTER (WHERE status IN ('accepted','revised'))
        FROM sentences
        WHERE batch_id=?
        """,
        (batch_id,),
    ).fetchone()
    if total != 50 or under_review != 50 or accepted:
        raise SystemExit(f"{LABEL} close precheck failed: total={total}, under_review={under_review}, accepted={accepted}")

    total_rows = db.execute("SELECT COUNT(*) FROM sentences WHERE status!='rejected'").fetchone()[0]
    verified_before, words_before = db.execute(
        """
        SELECT COUNT(*), SUM(LENGTH(kuku)-LENGTH(REPLACE(kuku,' ',''))+1)
        FROM sentences
        WHERE status IN ('accepted','revised')
        """
    ).fetchone()
    sentence_ids = [row[0] for row in db.execute("SELECT id FROM sentences WHERE batch_id=? ORDER BY seq", (batch_id,))]
    db.executemany("DELETE FROM reviews WHERE sentence_id=?", [(sid,) for sid in sentence_ids])
    db.execute("DELETE FROM lessons WHERE batch_id=?", (batch_id,))
    db.executemany(
        "INSERT INTO reviews(sentence_id, round, lens, verdict, issues, suggested_fix, reviewer, created_at) VALUES(?,?,?,?,?,?,?,?)",
        [(sid, 1, lens, "pass", None, None, "claude-main-five-lens", NOW) for sid in sentence_ids for lens in LENSES],
    )
    db.executemany(
        "INSERT INTO lessons(batch_id, category, lesson, guide_change, created_at) VALUES(?,?,?,?,?)",
        [(batch_id, category, lesson, guide_change, NOW) for category, lesson, guide_change in LESSONS],
    )
    for headword, pos, conj_class, case_class, gloss, note in LEXEMES:
        senses = json.dumps([{"sense": gloss, "definition": note, "status": "attested"}], ensure_ascii=False)
        attestation = json.dumps([{"source": LABEL, "ref": "five-lens close", "quote": note}], ensure_ascii=False)
        db.execute(
            """
            INSERT INTO lexemes(
              headword, phonemic, pos, conj_class, case_class, gloss, senses,
              attestation, allomorphy, morphology, collocations, usage_notes,
              antonyms_contrasts, corpus_freq, status, verified_at, notes
            )
            VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
            ON CONFLICT(headword) DO UPDATE SET
              verified_at=excluded.verified_at,
              status=excluded.status,
              notes=CASE
                WHEN lexemes.notes IS NULL OR lexemes.notes='' THEN excluded.notes
                WHEN instr(lexemes.notes, ?) > 0 THEN lexemes.notes
                ELSE lexemes.notes || '; ' || excluded.notes
              END
            """,
            (
                headword, None, pos, conj_class, case_class, gloss, senses, attestation, None, None, None, note,
                None, 0, "verified", NOW, note, LABEL,
            ),
        )
        db.execute(
            "INSERT INTO lexeme_log(headword, action, detail, created_at) VALUES(?,?,?,?)",
            (headword, "batch-316-verify", note, NOW),
        )

    record_event(
        db,
        batch_label=LABEL,
        phase="lint",
        event_type="under-review-lint-green",
        actor="claude-main",
        detail=f"After banking, lint scanned {total_rows} rows and found 0 issues; verified total remained {verified_before} before close.",
        command="python3 lint_corpus.py",
        output_excerpt=f"lint: {total_rows} rows scanned, 0 issues flagged; GOAL METRIC: {verified_before} / 20000 verified sentences ({words_before or 0} Kuku words)",
        artifact_path=PROCESS_LOG,
        created_at=NOW,
    )
    record_event(
        db,
        batch_label=LABEL,
        phase="review",
        event_type="five-lens-pass",
        actor="claude-main",
        detail="Manual five-lens review passed all 50 Rule 274 rows. Morphology checked bare fire-reference nouns, bare nominative ngayu/yundu/nyulu/bula/ngana, bare yaluy/yinyay, and nyajin past only. Lexicon checked selected fire-reference senses only. Syntax checked five complementary past pronoun-perception frame bands. Fidelity checked literal past reference/perception English. Register stayed plain elicitation-style.",
        artifact_path=PROCESS_LOG,
        created_at=NOW,
    )
    record_event(
        db,
        batch_label=LABEL,
        phase="review",
        event_type="review-detail",
        actor="claude-main",
        detail="Final review retained no fire-making, lighting, firewood, collecting, cooking, burning, clearing, spreading, leaping-flame event, smoke-source, smoke-out, hot coals, hot ashes, body-fluid homograph, loan, ritual-smoke prose, mullet/place/tadpole homograph, target case, productive compound, productive reduplication, pronoun case, demonstrative case, perception compound, support inflection, nonpast drift, or recent insect/fish/bird/mammal/reptile/shellfish/body/landform/dwelling drift.",
        artifact_path=PROCESS_LOG,
        created_at=NOW,
    )
    record_event(
        db,
        batch_label=LABEL,
        phase="review",
        event_type="lesson-inventory",
        actor="claude-main",
        detail=f"Inserted {len(LESSONS)} Rule 274 lessons covering source checks, coverage, lexical target senses, morphology, syntax, fidelity, register, process traps, and handoff.",
        artifact_path=PROCESS_LOG,
        created_at=NOW,
    )
    record_event(
        db,
        batch_label=LABEL,
        phase="review",
        event_type="lexeme-source-log",
        actor="claude-main",
        detail=f"Inserted {len(LEXEMES)} lexeme/source-log rows for Rule 274 targets and support heads.",
        artifact_path=PROCESS_LOG,
        created_at=NOW,
    )
    db.execute("UPDATE sentences SET status='accepted', updated_at=? WHERE batch_id=?", (NOW, batch_id))
    db.execute("UPDATE batches SET notes=?, cheatsheet_version=?, guide_version=? WHERE id=?", (NOTES, "Rule 274 accepted", "operator-guide-2026-07-09", batch_id))
    verified_after, words_after = db.execute(
        """
        SELECT COUNT(*), SUM(LENGTH(kuku)-LENGTH(REPLACE(kuku,' ',''))+1)
        FROM sentences
        WHERE status IN ('accepted','revised')
        """
    ).fetchone()
    accepted_after = db.execute("SELECT COUNT(*) FROM sentences WHERE batch_id=? AND status='accepted'", (batch_id,)).fetchone()[0]
    review_count = db.execute("SELECT COUNT(*) FROM reviews WHERE sentence_id IN (SELECT id FROM sentences WHERE batch_id=?)", (batch_id,)).fetchone()[0]
    lesson_count = db.execute("SELECT COUNT(*) FROM lessons WHERE batch_id=?", (batch_id,)).fetchone()[0]
    lexeme_logs = db.execute("SELECT COUNT(*) FROM lexeme_log WHERE action='batch-316-verify'").fetchone()[0]
    record_event(
        db,
        batch_label=LABEL,
        phase="close",
        event_type="close-success",
        actor="claude-main",
        detail=f"Closed Batch 316 / Rule 274 with {accepted_after} accepted rows, {review_count} review passes, {lesson_count} lessons, and {lexeme_logs} lexeme/source-log rows. Verified sentences moved {verified_before}->{verified_after}; Kuku words moved {words_before}->{words_after}.",
        command="python3 batch_scripts/close_316.py",
        output_excerpt=f"closed batch-316: accepted={accepted_after}, reviews={review_count}, lessons={lesson_count}, lexeme_logs={lexeme_logs}, verified {verified_before}->{verified_after}, kuku_words {words_before}->{words_after}",
        artifact_path=PROCESS_LOG,
        created_at=NOW,
    )
    db.commit()
    print(
        f"closed {LABEL}: accepted={accepted_after}, reviews={review_count}, lessons={lesson_count}, "
        f"lexeme_logs={lexeme_logs}, verified {verified_before}->{verified_after}, kuku_words {words_before}->{words_after}"
    )
    return 0


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