#!/usr/bin/env python3
"""Close batch-274 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-274"
NOW = dt.datetime.now(dt.timezone.utc).replace(microsecond=0).isoformat().replace("+00:00", "Z")
PROCESS_LOG = "process_logs/batch-274-second-landform-geography-reference-lexical-expansion-after-rule231-process.md"
LENSES = ("morphology", "lexicon", "syntax", "fidelity", "register")

NOTES = (
    "SECOND LANDFORM/GEOGRAPHY REFERENCE LEXICAL EXPANSION AFTER RULE 231 (Rule 232). 50 rows. "
    "TARGETS: mabil swamp, karrirrangkal coral, jalngarr flat rock, jinali smooth rock, kangku valley, "
    "yalmba seashore, kambal-kambal mud, jalkar high bank, janku hole, and jamba hillside. SUPPORT: "
    "uninflected yinya/yaluy/yinyay, bare 1sg ngayu, and nonpast nyajil. FRAMES: TARGET yaluy, "
    "Yinya TARGET yaluy, TARGET yinya yinyay, Yinya TARGET yinyay, and Ngayu TARGET yaluy nyajil. "
    "REVIEW: all target nouns stayed bare landform/geography reference labels. Rule 185-186 targets and rejected "
    "near candidates stayed closed. No place-name, route/travel/crossing, water-use, burning/clearing, live-ant/"
    "insect behavior, ecology, reef/sea/collecting, climbing/falling/danger, ownership/dispute/legal/social, "
    "finding/peeping/causative, rubbish/debris/leaf-mould/growing-use, target case, target compound, productive "
    "reduplication, support inflection, or Rule 231 named-tree perception drift was opened."
)

LESSONS = [
    ("source", "Patz section 3.1 supports nominal semantic content; Rule 232 uses each selected landform/geography head as a bare nominal referent only.", "Rule 232: nominal source."),
    ("source", "Patz section 3.7 supports yinya as 'that/those' and yaluy/yinyay as local demonstratives; demonstrative support stayed bare and uninflected.", "Rule 232: deictic source."),
    ("source", "Patz examples and prior corpus law support nyaji-l perception with nonpast nyajil; Rule 232 uses only 1sg nonpast perception rows in the final band.", "Rule 232: perception source."),
    ("process", "Celestial was rejected as a Rule 232 field because the clean noun inventory was too thin without mixing in verb compounds and high-count sun/moon forms.", "Rule 232: celestial field rejected."),
    ("process", "Artefact was deferred because earlier object/artefact reference rules already used many obvious heads and the remaining pool carried loan/use/source traps.", "Rule 232: artefact field deferred."),
    ("process", "Exact preflight over all 50 planned Rule 232 Kuku surfaces returned no accepted, revised, or under-review collisions.", "Rule 232: duplicate preflight."),
    ("lexicon", "Mabil is admitted as swamp reference only; crossing, route, water-use, habitat, and event claims remain closed.", "Rule 232: mabil."),
    ("lexicon", "Karrirrangkal is admitted as coral reference only; lime, reef/sea, collecting, habitat, and material-use prose remain closed.", "Rule 232: karrirrangkal."),
    ("lexicon", "Jalngarr is admitted as flat-rock reference only; sitting, creek, water, climbing, and route readings remain closed.", "Rule 232: jalngarr."),
    ("lexicon", "Jinali is admitted as smooth-rock reference only; smooth-property predication and smoothing-use readings remain closed.", "Rule 232: jinali."),
    ("lexicon", "Kangku is admitted as valley reference only; mountain-pass route/travel and cheek/body-part homograph drift remain closed.", "Rule 232: kangku."),
    ("lexicon", "Yalmba is admitted as seashore reference only; sea, beach-collecting, tide, turtle, food, and warning/event rows remain closed.", "Rule 232: yalmba."),
    ("lexicon", "Kambal-kambal is admitted as mud reference only; crocodile, crab, turtle, nest, muddy-water, and event rows remain closed.", "Rule 232: kambal-kambal."),
    ("lexicon", "Jalkar is admitted as high-bank reference only; jalkar-jalkar steep-property rows and river/route/danger readings remain closed.", "Rule 232: jalkar."),
    ("lexicon", "Janku is admitted as hole reference only; waterhole, animal burrow, nest, digging, ground-oven, and body/event readings remain closed.", "Rule 232: janku."),
    ("lexicon", "Jamba is admitted as hillside reference only; root/under-tree drift, burning, social metaphor, and event readings remain closed.", "Rule 232: jamba."),
    ("morphology", "All Rule 232 target nouns stayed bare: no target LOC, DAT, ABL, PERL, ERG, INST, COMIT, POSS, PRIV, EXCESS, plural, derivation, or suffixing.", "Rule 232: bare targets."),
    ("morphology", "Kambal-kambal stayed a whole lexical reduplicated noun; no productive reduplication was opened.", "Rule 232: lexical reduplication."),
    ("morphology", "Jalkar stayed a noun 'high bank'; the related jalkar-jalkar steep-property adjective was not imported.", "Rule 232: jalkar contrast."),
    ("morphology", "Yinya, yaluy, yinyay, ngayu, and nyajil stayed uninflected support heads.", "Rule 232: support morphology."),
    ("syntax", "Rows 1-10 used TARGET yaluy.", "Rule 232: here frame."),
    ("syntax", "Rows 11-20 used Yinya TARGET yaluy.", "Rule 232: preposed that-here frame."),
    ("syntax", "Rows 21-30 used TARGET yinya yinyay.", "Rule 232: postposed that-there frame."),
    ("syntax", "Rows 31-40 used Yinya TARGET yinyay.", "Rule 232: preposed that-there frame."),
    ("syntax", "Rows 41-50 used Ngayu TARGET yaluy nyajil.", "Rule 232: 1sg nonpast perception-here frame."),
    ("fidelity", "English stayed literal reference prose: selected landform/geography label plus here/there/that or 1sg see.", "Rule 232: literal English."),
    ("fidelity", "The batch did not convert dictionary definitions into route, water-use, ecology, place-name, source-material, danger, ownership, legal/social, or event claims.", "Rule 232: source-prose restraint."),
    ("register", "Rows remained plain elicitation-style reference/perception statements, not travel instruction, ecological description, story prose, law/dispute talk, or resource-use narrative.", "Rule 232: register."),
    ("process", "Duplicate preflight, static guards, whole-corpus lint, five-lens review, lessons, lexeme logs, export, source-book update, render, and live docs are required before treating Rule 232 as complete.", "Rule 232: evidence standard."),
    ("handoff", "Next batch may continue from 13,797 if this closes; do not infer landform case, event, route, water-use, ecology, source, ownership, or danger grammar from Rule 232.", "Rule 232: next cadence."),
]

LEXEMES = [
    ("mabil", "noun", None, "NA", "swamp", "batch-274: swamp reference only; no crossing, route, water-use, or habitat row."),
    ("karrirrangkal", "noun", None, "NA", "coral", "batch-274: coral reference only; no lime, reef/sea, collecting, or material-use row."),
    ("jalngarr", "noun", None, "NA", "flat rock", "batch-274: flat-rock reference only."),
    ("jinali", "noun", None, "NA", "smooth rock", "batch-274: smooth-rock reference only; no smooth-property predication."),
    ("kangku", "noun", None, "NA", "valley", "batch-274: valley reference only; no route/pass event row."),
    ("yalmba", "noun", None, "NA", "seashore", "batch-274: seashore reference only; no sea/tide/collecting row."),
    ("kambal-kambal", "noun", None, "NA", "mud", "batch-274: mud reference only; lexical reduplication as whole headword."),
    ("jalkar", "noun", None, "NA", "high bank", "batch-274: high-bank reference only; jalkar-jalkar steep adjective remains closed."),
    ("janku", "noun", None, "NA", "hole", "batch-274: hole reference only; no waterhole, burrow, nest, or digging row."),
    ("jamba", "noun", None, "NA", "hillside", "batch-274: hillside reference only."),
    ("yinya", "demonstrative", None, "NA", "that; those", "batch-274: demonstrative support only."),
    ("yaluy", "demonstrative", None, "NA", "here", "batch-274: local demonstrative support only."),
    ("yinyay", "demonstrative", None, "NA", "there", "batch-274: local demonstrative support only."),
    ("ngayu", "personal-pronoun", None, "NA", "I", "batch-274: bare 1sg nominative A."),
    ("nyajil", "verb", "l", None, "perceive nonpast", "batch-274: nonpast nyaji-l perception predicate."),
]


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:
        raise SystemExit(f"{LABEL} must have 50 rows before close; found {total}")
    if accepted:
        raise SystemExit(f"{LABEL} already has {accepted} accepted/revised rows; refusing to overwrite")
    if under_review != 50:
        raise SystemExit(f"{LABEL} must have 50 under_review rows before close; found {under_review}")

    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.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-274-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 232 rows. Morphology checked bare landform nouns, lexical kambal-kambal, uninflected support heads, and no target case. Lexicon checked selected landform labels only. Syntax checked four deictic frame bands plus one 1sg nonpast perception band. Fidelity checked literal 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 Rule 185-186 target repeat, no rejected landform candidate, no place-name row, no route/travel/crossing row, no water-use row, no burning/clearing row, no live-ant or insect behavior row, no ecology/habitat row, no reef/sea/collecting row, no climbing/falling/danger row, no ownership/dispute/legal/social row, no finding/peeping/causative row, no rubbish/debris/leaf-mould/growing-use row, no target case, no productive compound, no productive reduplication, no support inflection, and no named-tree 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 232 lessons covering source checks, field rejection, lexical target senses, morphology, syntax, fidelity, register, and handoff.",
        artifact_path="batch_scripts/close_274.py",
        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 232 targets and support heads.",
        artifact_path="batch_scripts/close_274.py",
        created_at=NOW,
    )
    db.execute("UPDATE batches SET notes=? WHERE id=?", (NOTES, batch_id))
    db.execute("UPDATE sentences SET status='accepted', updated_at=? WHERE batch_id=?", (NOW, batch_id))
    record_event(
        db,
        batch_label=LABEL,
        phase="close",
        event_type="close-success",
        actor="claude-main",
        detail="Closed Batch 274: 50 accepted Rule 232 second landform/geography reference rows, five-lens reviews, lessons, lexeme/source logs, and process evidence stored.",
        command="python3 batch_scripts/close_274.py",
        artifact_path="batch_scripts/close_274.py",
        created_at=NOW,
    )
    db.commit()

    reviews = db.execute(
        "SELECT COUNT(*) FROM reviews WHERE sentence_id IN (SELECT id FROM sentences WHERE batch_id=?)",
        (batch_id,),
    ).fetchone()[0]
    verified_after, words_after = db.execute(
        """
        SELECT
          COUNT(*),
          SUM(LENGTH(kuku)-LENGTH(REPLACE(kuku,' ',''))+1)
        FROM sentences
        WHERE status IN ('accepted','revised')
        """
    ).fetchone()
    print(
        f"closed {LABEL}: accepted=50, reviews={reviews}, verified {verified_before}->{verified_after}, "
        f"kuku_words {words_before}->{words_after}"
    )
    return 0


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