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

from __future__ import annotations

import datetime as dt
import json
import sqlite3
import sys

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-154"
NOW = dt.datetime.now(dt.timezone.utc).replace(microsecond=0).isoformat().replace("+00:00", "Z")
PROCESS_LOG = "process_logs/batch-154-clause-combining-process.md"
LENSES = ("morphology", "lexicon", "syntax", "fidelity", "register")

NOTES = (
    "CONSTRUCTION: CLAUSE-COMBINING AND DISCOURSE-RELATION FRAMES (Rule 112). 50 rows. DESIGN: "
    "hand-authored two-clause and discourse-framed sentences using Patz-attested particles kari, yamba, kaki, "
    "kunka, nganganjirrka, balu, and nguba. SOURCE CONTROLS: Patz 4.3.2 coordination particles, 4.4.4.4 "
    "hypothetical reason, 4.9.3 nguba/yala controls, 4.10 interjections, and 5.5.3 collateral discourse. "
    "PROCESS STORAGE: source pass, lexeme checks, avoided forms, duplicate-risk notes, row inventory, five-lens "
    "review, close, lint, export, and live-doc verification are stored in process_events and the process log."
)

LESSONS = [
    ("syntax", "Coordination/discourse particles stand at the beginning of the conjoined or dependent clause; do not bury kari, yamba, kaki, kunka, or nganganjirrka mid-clause as free adverbs.", "Rule 112: particle position."),
    ("syntax", "Kari is best for event hindrance: an intended or underway event is blocked by the following event.", "Rule 112: kari event hindrance."),
    ("syntax", "Yamba is best restrained to contrastive state or qualification frames; do not let English 'but' erase Patz's event/state distinction.", "Rule 112: yamba state contrast."),
    ("syntax", "Kaki conditionals must remain non-past/stative; Patz says past conditional/temporal relations use juxtaposed clauses instead of kaki.", "Rule 112: kaki tense guard."),
    ("syntax", "Kaki can occur in second position, but the clause still has conditional/temporal force and must not become a loose afterthought.", "Rule 112: kaki second position."),
    ("morphology", "Hypothetical reason uses verb plus -nya-mun-ku, often with kunka and less commonly nganganjirrka.", "Rule 112: hypothetical reason morphology."),
    ("syntax", "Kunka and nganganjirrka are in-case/hypothetical-reason markers, not generic synonyms of nguba.", "Rule 112: kunka versus nguba."),
    ("fidelity", "If the speaker knows the reason for sure, Patz rejects the kunka construction; use independent statements such as a yambakari statement instead.", "Rule 112: known reason guard."),
    ("syntax", "Patz explicitly recommends independent nguba possibility clauses as the less cumbersome discourse alternative to hypothetical-reason subordination.", "Rule 112: nguba alternative reason."),
    ("syntax", "Do not delete common A/S participants just to imitate English subordination; Patz notes pronoun reduction is preferred over deletion.", "Rule 112: participant retention."),
    ("fidelity", "Balu wrong-assumption is speaker-related discourse work, usually conversational or first-person; keep it separate from ordinary kari negation.", "Rule 112: balu wrong assumption."),
    ("syntax", "Balu wrong-assumption rows can be followed by a kari correction, making the discarded assumption and actual alternative explicit.", "Rule 112: balu plus kari correction."),
    ("morphology", "Source-shaped forms such as kada-nya-mun-ku, bambay-ma-nya-mun-ku, nyaji-ji-nya-mun-ku, and bayka-nya-mun-ku should remain traceable in evidence and lexeme logs.", "Rule 112: source-shaped dependent forms."),
    ("lexicon", "The strong-wind surface junkuji is source-shaped from Patz ex.388; related dictionary material is junkurr/junkurrji. Keep that provenance visible.", "Rule 112: junkuji provenance."),
    ("fidelity", "Yala remains excluded as a coordination particle; Rule 112 only permits yala inside the Patz comparison frame in the source-shaped yamba row.", "Rule 112: no yala coordination."),
    ("process", "For construction batches, store source-window notes, lexeme checks, avoided exact duplicates, five-lens criteria, lint/export output, and public-doc verification as first-order corpus artifacts.", "Rule 112: process as data."),
]

LEXEMES = [
    ("kari", "particle", None, "clause-initial contrast; negator", "but; no; not", "batch-154: event hindrance, collateral contrast, and correction clauses."),
    ("yamba", "particle", None, "clause-initial state/qualification contrast", "but; however", "batch-154: state contrast against kari event contrast."),
    ("kaki", "particle", None, "conditional/temporal dependent clause", "if; when", "batch-154: non-past first-position and second-position conditionals."),
    ("kunka", "particle", None, "hypothetical reason marker", "in case", "batch-154: in-case clauses with and without -nya-mun-ku."),
    ("nganganjirrka", "particle", None, "hypothetical reason marker", "just in case", "batch-154: just-in-case clauses parallel to kunka."),
    ("nguba", "particle", None, "preposed possibility marker", "maybe; perhaps", "batch-154: independent possibility clauses used as discourse reasons."),
    ("balu", "particle", None, "wrong-assumption/counterexpectation marker", "wrong assumption; contrary to fact thought", "batch-154: wrong-assumption rows with kari corrections."),
    ("yuwu", "interjection", None, "answer word", "yes", "batch-154: answer plus kaki conditional."),
    ("kaku", "interjection", None, "preposed utterance", "wait for me", "batch-154: interjection plus kunka in-case frame."),
    ("ngay", "interjection", None, "acknowledgement", "I see; oh yes", "batch-154: source-shaped kari collateral row from Patz ex.655."),
    ("yalada", "interjection", None, "settlement/closure", "settled then; alright", "batch-154: interjection plus yamba state contrast."),
    ("juyuy", "interjection", None, "dismissive interjection", "nonsense", "batch-154: interjection plus kari manubaja negation."),
    ("wumba", "interjection", None, "never-mind interjection", "never mind", "batch-154: interjection plus second-position kaki."),
    ("badur", "noun", None, "COMIT badur-iji", "hook and line; fishing line", "batch-154: fishing-line kari event-hindrance and nguba possibility rows."),
    ("junkuji", "adjective-form", None, "source-shaped strong predicate", "strong", "batch-154: Patz ex.388 strong-wind surface; cross-reference junkurr/junkurrji."),
    ("junkurrji", "adjective-form", None, "junkurr plus comitative -ji", "strong", "batch-154: conditional strong-wind row."),
    ("muyar", "noun", None, "weather noun", "wind", "batch-154: strong-wind kari/kaki/balu rows."),
    ("bambay", "adjective", None, "state predicate; inchoative bambay-ma-", "sick", "batch-154: kari hindrance and hypothetical-reason rows."),
    ("bambay-ma-nya-mun-ku", "verb-form", None, "sick-INCHO-SUB-mun-DAT", "in case he gets sick", "batch-154: source-shaped Patz ex.500 form."),
    ("kada-nya-mun-ku", "verb-form", None, "come-SUB-mun-DAT", "in case it/he comes", "batch-154: hypothetical reason source-shaped form."),
    ("nyaji-ji-nya-mun-ku", "verb-form", None, "see-ITR-SUB-mun-DAT", "in case she comes across", "batch-154: Patz ex.501 source-shaped form."),
    ("bayka-nya-mun-ku", "verb-form", None, "bite-SUB-mun-DAT", "in case it bites", "batch-154: Patz ex.502 source-shaped form."),
    ("bangkan", "noun", None, "ABS object", "medicinal root", "batch-154: replaces English medicine in Patz ex.500 frame."),
    ("baral", "noun", None, "DAT baral-ka; PERL baral-darr", "road; path", "batch-154: road in-case/no-water frames."),
    ("budmandu", "noun", None, "ABS subject", "suitcase; portmanteau", "batch-154: yamba state contrast source row."),
    ("kulngkul", "adjective", None, "state predicate", "heavy", "batch-154: yamba state contrast source row."),
    ("dayirr", "adjective", None, "state predicate; causative dayirr-bunga-l", "clean; clear; bright", "batch-154: house-clean and source ex.499 row."),
    ("kidal", "verb", "l", "NONPAST kida-l", "sweep", "batch-154: source ex.499 house-cleaning row."),
    ("dayirr-bungal", "verb-form", None, "clean-CAUS-NONPAST", "clean it", "batch-154: source ex.499 house-cleaning row."),
    ("jungkalu", "noun", None, "ABS object/subject", "billycan", "batch-154: water and quandong in-case rows."),
    ("kalka", "noun", None, "ABS object", "spear", "batch-154: spear-making, just-in-case, and wrong-assumption rows."),
    ("kuli-ji", "adjective-form", None, "anger-COMIT", "vicious; angry", "batch-154: black-goanna yamba row."),
    ("walkarr", "noun", None, "ABS subject", "black goanna", "batch-154: Patz 5.5.3 ex.656 yamba row."),
    ("mala-minya", "compound", None, "expert-meat", "expert hunter", "batch-154: Patz 5.5.3 ex.657 yamba row."),
    ("walu-yindu", "compound", None, "face-other", "different", "batch-154: Patz 5.5.3 ex.657 country-different row."),
    ("ngaykundu", "pronoun-form", None, "ngayku-ndu", "with/at me; my-locative", "batch-154: Patz ex.503 nguba possibility row."),
    ("ngakiji", "verb", None, "REF hide; PURP ngaki-ji-nka", "hide", "batch-154: kari hindrance and nguba possibility rows."),
    ("warngku-wunay", "verb", "y", "sleep-lie-NONPAST", "sleep", "batch-154: second-position kaki and balu wrong-assumption rows."),
    ("kinka-n-kinka-nka", "verb-form", None, "play-REDUP-PURP", "to play", "batch-154: wawu jirray/wrong-assumption rows."),
    ("dindal", "verb", "l", "PURP dinda-nka", "roast", "batch-154: balu wrong-assumption row."),
    ("wajul", "verb", "l", "NONPAST waju-l", "cook; boil", "batch-154: balu correction and cooking rows."),
]


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}")

    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-154-verify", note, 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 112 rows. Morphology lens checked past surface -n, "
            "non-past l/y endings, comitative/instrumental/locative case forms, and -nya-mun-ku dependent forms. "
            "Lexicon lens checked every function word and support lexeme against Patz or dictionary entries. "
            "Syntax lens enforced clause-initial coordination particles, non-past kaki, hypothetical-reason marking, "
            "participant retention, and nguba independent-clause strategy. Fidelity lens guarded kari/yamba/balu/kunka "
            "semantic distinctions. Register lens kept rows instructional and marked all public rows as pending elder verification."
        ),
        artifact_path=PROCESS_LOG,
        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 154: 50 accepted Rule 112 clause-combining rows, five-lens reviews, lessons, lexeme/source logs, and process evidence stored.",
        command="python3 batch_scripts/close_154.py",
        artifact_path="batch_scripts/close_154.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]
    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-154-verify'").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, "
        f"{lesson_count} lessons, {lexeme_logs} lexeme/source-log rows, "
        f"{process_events} process events, verified corpus={verified}"
    )
    return 0


if __name__ == "__main__":
    sys.exit(main())
