#!/usr/bin/env python3
"""Close batch-236 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-236"
NOW = dt.datetime.now(dt.timezone.utc).replace(microsecond=0).isoformat().replace("+00:00", "Z")
PROCESS_LOG = "process_logs/batch-236-state-predicate-construction-consolidation-after-rule193-process.md"
LENSES = ("morphology", "lexicon", "syntax", "fidelity", "register")

NOTES = (
    "STATE-PREDICATE CONSTRUCTION CONSOLIDATION AFTER RULE 193 (Rule 194). 50 rows. RETAINED TARGETS: dakwuy hungry, "
    "bambay sick, dukul-wuri really angry, kala-burra weak, warngku-kaday sleepy, wawu-mini happy, wawu-buyun worried, "
    "milka-murruji ashamed, ngulkurr well, and juwamalmal lethargic. FRAMES: yundu affirmative state predicates, bula "
    "dual affirmative state predicates, nganjin kari exclusive negated state predicates, jana kari negated state "
    "predicates, and ngana affirmative state predicates. REVIEW: targets stayed bare; no new target heads, deferred "
    "verb-state candidates, body-part idiom analysis, food/cooking/eating, doctor/medicine/magic/healing, illness-cause, "
    "anger event, sleep event, making-happy/promise/play cause, worry/dread/travel/desire event, theft/promise/"
    "avoidance/shaming-causative row, food-good/moral-good ngulkurr reading, causative, inchoative, degree, case, "
    "plural, target compound, productive reduplication, or Rule 191-192 perception drift."
)

LESSONS = [
    ("process", "Rule 194 deliberately consolidated the bounded Rule 193 state-predicate field rather than adding targets.", "Rule 194: construction cadence after Rule 193."),
    ("process", "The source pass retained exactly ten Rule 193 heads and reopened no deferred verb-state or polysemous candidate.", "Rule 194: retained target inventory."),
    ("process", "`bula` and `nganjin` were admitted as pronoun support from Patz's personal-pronoun paradigm, not as new lexical targets.", "Rule 194: pronoun support."),
    ("process", "Exact duplicate preflight over the yundu/bula/nganjin-kari/jana-kari/ngana grid found no accepted, revised, or under-review duplicates.", "Rule 194: duplicate preflight."),
    ("process", "Rule 193 event-source and body-part traps remain closed while constructional coverage grows.", "Rule 194: trap carry-forward."),
    ("lexicon", "`dakwuy` was retained as hungry only; food, cooking, eating, and food-preparation rows stayed closed.", "Rule 194: dakwuy retained."),
    ("lexicon", "`bambay` was retained as sick only; illness-cause, doctor, medicine, magic, and healing rows stayed closed.", "Rule 194: bambay retained."),
    ("lexicon", "`dukul-wuri` was retained as really angry only; head/body-part analysis, nodding, fighting, yelling, throwing, and threat rows stayed closed.", "Rule 194: dukul-wuri retained."),
    ("lexicon", "`kala-burra` was retained as weak only; carrying, failing, work, hunting, and cause rows stayed closed.", "Rule 194: kala-burra retained."),
    ("lexicon", "`warngku-kaday` was retained as sleepy only; sleep-event, lying-down, bed, and habitual-sleeper rows stayed closed.", "Rule 194: warngku-kaday retained."),
    ("lexicon", "`wawu-mini` was retained as happy only; making-happy, promise, play, ceremony, and event-cause rows stayed closed.", "Rule 194: wawu-mini retained."),
    ("lexicon", "`wawu-buyun` was retained as worried only; consequence, dread-event, travel, missing, and desire rows stayed closed.", "Rule 194: wawu-buyun retained."),
    ("lexicon", "`milka-murruji` was retained as ashamed only; theft, promise, avoidance, ear/body-part, and shaming-causative rows stayed closed.", "Rule 194: milka-murruji retained."),
    ("lexicon", "`ngulkurr` was retained as well only; food-good, moral-good, good-things, and comparative rows stayed closed.", "Rule 194: ngulkurr retained."),
    ("lexicon", "`juwamalmal` was retained as lethargic only; illness-cause, sleeping, medical, and diagnosis rows stayed closed.", "Rule 194: juwamalmal retained."),
    ("morphology", "All Rule 194 target heads stayed bare with no case, degree, causative, inchoative, plural, target compound, or productive-reduplicated form.", "Rule 194: bare target morphology."),
    ("morphology", "`kari` remained the only added operator in negated rows and was glossed as NEG before the state predicate.", "Rule 194: kari negation."),
    ("morphology", "Lexicalized compounds such as `dukul-wuri`, `warngku-kaday`, `wawu-mini`, `wawu-buyun`, `milka-murruji`, and `kala-burra` remained dictionary heads, not productive formations.", "Rule 194: lexical compound distinction."),
    ("morphology", "No degree morphology was admitted, even though hungry has a grammar example with degree marking.", "Rule 194: degree restraint."),
    ("syntax", "Rows 1-10 used bare `yundu` plus one retained state predicate.", "Rule 194: yundu affirmative predicates."),
    ("syntax", "Rows 11-20 used bare third-person dual `bula` plus one retained state predicate.", "Rule 194: bula affirmative predicates."),
    ("syntax", "Rows 21-30 used exclusive `nganjin`, `kari`, and one retained state predicate.", "Rule 194: nganjin kari predicates."),
    ("syntax", "Rows 31-40 used bare `jana`, `kari`, and one retained state predicate.", "Rule 194: jana kari predicates."),
    ("syntax", "Rows 41-50 used inclusive `ngana` plus one retained state predicate.", "Rule 194: ngana affirmative predicates."),
    ("handoff", "Next cadence may return to lexical expansion, but only after a fresh source pass and with Rule 193-194 state-predicate traps carried forward.", "Rule 194: next cadence."),
]

LEXEMES = [
    ("dakwuy", "adjective", None, "NA", "hungry", "batch-236: retained as hungry state predicate only; no food/cooking/eating row."),
    ("bambay", "adjective", None, "NA", "sick", "batch-236: retained as sick state predicate only; no illness-cause, doctor, medicine, magic, or healing row."),
    ("dukul-wuri", "adjective", None, "NA", "really angry", "batch-236: retained as really angry state predicate only; no head/body-part analysis, nodding, fighting, yelling, throwing, or threat row."),
    ("kala-burra", "adjective", None, "NA", "weak", "batch-236: retained as weak state predicate only; no carrying, failing, work, hunting, or cause row."),
    ("warngku-kaday", "adjective", None, "NA", "sleepy", "batch-236: retained as sleepy state predicate only; no sleep-event, lying-down, bed, or habitual-sleeper row."),
    ("wawu-mini", "adjective", None, "NA", "happy", "batch-236: retained as happy state predicate only; no making-happy, promise, play, ceremony, or event-cause row."),
    ("wawu-buyun", "adjective", None, "NA", "worried", "batch-236: retained as worried state predicate only; no consequence, dread-event, travel, missing, or desire row."),
    ("milka-murruji", "adjective", None, "NA", "ashamed", "batch-236: retained as ashamed state predicate only; no theft, promise, avoidance, ear/body-part, or shaming-causative row."),
    ("ngulkurr", "adjective", None, "NA", "well", "batch-236: retained as bodily well state predicate only; no food-good, moral-good, good-things, or comparative row."),
    ("juwamalmal", "adjective", None, "NA", "lethargic", "batch-236: retained as lethargic state predicate only; no illness-cause, sleeping, medical, or diagnosis row."),
    ("kari", "particle", None, "NA", "not; NEG", "batch-236: retained as source-backed pre-adjectival negator for state predicates."),
    ("yundu", "pronoun", None, "NA", "you; 2sg nominative", "batch-236: bare pronoun S in affirmative state predicates."),
    ("bula", "pronoun", None, "NA", "they two; 3du nominative", "batch-236: bare dual pronoun S in affirmative state predicates."),
    ("nganjin", "pronoun", None, "NA", "we; 1pl exclusive", "batch-236: bare exclusive pronoun S in negated state predicates."),
    ("jana", "pronoun", None, "NA", "they; 3pl nominative", "batch-236: bare pronoun S in negated state predicates."),
    ("ngana", "pronoun", None, "NA", "we; 1pl inclusive", "batch-236: bare pronoun S in affirmative state predicates."),
]


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-236-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 by SQL token estimate)",
                 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 194 rows. Morphology checked bare retained state targets, no case/degree/causative/inchoative/plural/productive-reduplication forms, lexicalized compounds as dictionary heads only, and kari as NEG before the state predicate. Lexicon checked each selected sense and kept deferred verb-state candidates, body-part idiom expansions, event causes, food-good/moral-good ngulkurr readings, and Rule 191-192 perception traps closed. Syntax checked bare pronoun S plus state predicate, with yundu, bula, nganjin kari, jana kari, and ngana frames. Fidelity checked no source-event import. Register stayed ordinary stative description.",
                 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 target locative/dative/ablative/perlative/ergative/instrument/comitative/possessive/privative/excess/plural/productive-reduplication/compound form, no loan token, no deferred target, no causative/inchoative event, no body-part analysis, and no source-event 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 194 lessons covering source checks, construction scope, duplicate preflight, pronoun support, retained target senses, morphology, syntax, fidelity, register, and handoff.",
                 artifact_path="batch_scripts/close_236.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 194 retained state targets, kari, and participants.",
                 artifact_path="batch_scripts/close_236.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 236: 50 accepted Rule 194 state-predicate construction-consolidation rows, five-lens reviews, lessons, lexeme/source logs, and process evidence stored.",
                 command="python3 batch_scripts/close_236.py", artifact_path="batch_scripts/close_236.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 = db.execute("SELECT COUNT(*) FROM sentences WHERE status IN ('accepted','revised')").fetchone()[0]
    under_review_remaining = db.execute("SELECT COUNT(*) FROM sentences WHERE status='under_review'").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 LIKE 'batch-236%'").fetchone()[0]
    process_events = db.execute("SELECT COUNT(*) FROM process_events WHERE batch_label=?", (LABEL,)).fetchone()[0]
    print(f"closed {LABEL}: 50 accepted rows, {reviews} review passes, {lesson_count} lessons, {lexeme_logs} lexeme/source-log rows, {process_events} process events, under_review={under_review_remaining}, verified corpus={verified}")
    return 0


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