#!/usr/bin/env python3
"""Bank batch-247: Rule 205 low-frequency property-adjective lexical expansion."""

from __future__ import annotations

import datetime as dt
import json
import re
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-247"
NOW = dt.datetime.now(dt.timezone.utc).replace(microsecond=0).isoformat().replace("+00:00", "Z")
PROCESS_LOG = "process_logs/batch-247-low-frequency-property-adjective-lexical-expansion-after-rule204-process.md"

TARGET_HEADS = {
    "barrangkul": "flat",
    "jalngka": "smooth",
    "kurru-kurru": "crooked",
    "darra": "narrow",
    "yalmbar": "wide",
    "kiray": "quiet",
    "murru": "round",
    "murru-murru": "lumpy",
    "muru-muru": "close together",
    "manun-manun": "kind",
}
SUBJECT_HEADS = {"bubu", "juku", "baral", "kambi", "kulji", "bama"}
SUPPORT = {"yinya", "yaluy", "yinyay"} | SUBJECT_HEADS
DENY_KUKU = {
    "kari", "nyaji-l", "nyajil", "nyajin", "bajaku", "baja",
    "nuka", "nukal", "nukan", "waju", "wajul", "wajun", "mani", "manil", "manin",
    "dunga", "dungan", "dungay", "warri", "warriny", "daka", "dakany", "dakay",
    "kida", "kidal", "kidan", "yaka", "yakan", "yakay", "kuni", "kunil", "kunin",
    "ngara", "ngaran", "bunday", "bundanda", "yala", "yanyu",
    "kayal", "kabu", "jumbun", "jika", "murrba",
}
DENY_ENGLISH = (
    "not", "without", "very", "more", "rather", "too", "hear", "heard", "listen", "see", "saw", "feel", "felt",
    "itch", "itchy", "eat", "eating", "cook", "cooking", "cooked", "raw", "unripe", "gather", "gathering",
    "repair", "travel", "walk", "go", "going", "move", "moving", "secret", "secretly", "country", "owner",
    "ownership", "money", "coin", "ill", "illness", "sick", "body", "pain", "source", "use", "using",
    "because", "make", "making", "put", "putting",
)


def js(values: list[str]) -> str:
    return json.dumps(values, ensure_ascii=False)


def row(
    seq: int,
    english: str,
    kuku: str,
    analysis: str,
    frame: str,
    words: list[str],
    evidence: str,
) -> tuple:
    return (seq, english, kuku, analysis, frame, 3, js(words), js([]), evidence, "0.86")


ROWS = [
    row(1, "The ground is flat.", "Bubu barrangkul.", "bubu barrangkul | ground flat", "r205_np_adj_bubu_barrangkul", ["bubu", "barrangkul"], "Patz adjective predicate; dictionary barrangkul flat."),
    row(2, "The wood is smooth.", "Juku jalngka.", "juku jalngka | wood smooth", "r205_np_adj_juku_jalngka", ["juku", "jalngka"], "Jalngka adjective smooth/level only; itchy-feeling noun homonym closed."),
    row(3, "The road is crooked.", "Baral kurru-kurru.", "baral kurru-kurru | road crooked", "r205_np_adj_baral_kurru_kurru", ["baral", "kurru-kurru"], "Baral road/path noun only; kurru-kurru physical crooked reading."),
    row(4, "The clothing is narrow.", "Kambi darra.", "kambi darra | clothing narrow", "r205_np_adj_kambi_darra", ["kambi", "darra"], "Dictionary darra includes narrow/too small with clothing example; no degree."),
    row(5, "The road is wide.", "Baral yalmbar.", "baral yalmbar | road wide", "r205_np_adj_baral_yalmbar", ["baral", "yalmbar"], "Yalmbar wide; no travel event."),
    row(6, "The person is quiet.", "Bama kiray.", "bama kiray | person quiet", "r205_np_adj_bama_kiray", ["bama", "kiray"], "Kiray selected as quiet property; no motion or secrecy adverb."),
    row(7, "The pebble is round.", "Kulji murru.", "kulji murru | pebble round", "r205_np_adj_kulji_murru", ["kulji", "murru"], "Kulji pebble/stone; murru round."),
    row(8, "The ground is lumpy.", "Bubu murru-murru.", "bubu murru-murru | ground lumpy", "r205_np_adj_bubu_murru_murru", ["bubu", "murru-murru"], "Murru-murru lexicalized adjective only."),
    row(9, "The stones are close together.", "Kulji muru-muru.", "kulji muru-muru | stones close-together", "r205_np_adj_kulji_muru_muru", ["kulji", "muru-muru"], "Kulji may be plural in context; muru-muru lexicalized adjective only."),
    row(10, "The person is kind.", "Bama manun-manun.", "bama manun-manun | person kind", "r205_np_adj_bama_manun_manun", ["bama", "manun-manun"], "Manun-manun character property only; no social event."),
    row(11, "The ground here is flat.", "Bubu barrangkul yaluy.", "bubu barrangkul yaluy | ground flat here", "r205_np_adj_here_bubu_barrangkul", ["bubu", "barrangkul", "yaluy"], "Yaluy support in proven property frame."),
    row(12, "The wood here is smooth.", "Juku jalngka yaluy.", "juku jalngka yaluy | wood smooth here", "r205_np_adj_here_juku_jalngka", ["juku", "jalngka", "yaluy"], "Smooth adjective; no smoothing verb."),
    row(13, "The road here is crooked.", "Baral kurru-kurru yaluy.", "baral kurru-kurru yaluy | road crooked here", "r205_np_adj_here_baral_kurru_kurru", ["baral", "kurru-kurru", "yaluy"], "Road/path noun only; no travel."),
    row(14, "The clothing here is narrow.", "Kambi darra yaluy.", "kambi darra yaluy | clothing narrow here", "r205_np_adj_here_kambi_darra", ["kambi", "darra", "yaluy"], "No degree or clothing event."),
    row(15, "The road here is wide.", "Baral yalmbar yaluy.", "baral yalmbar yaluy | road wide here", "r205_np_adj_here_baral_yalmbar", ["baral", "yalmbar", "yaluy"], "Wide property only."),
    row(16, "The person here is quiet.", "Bama kiray yaluy.", "bama kiray yaluy | person quiet here", "r205_np_adj_here_bama_kiray", ["bama", "kiray", "yaluy"], "Quiet property; no movement."),
    row(17, "The pebble here is round.", "Kulji murru yaluy.", "kulji murru yaluy | pebble round here", "r205_np_adj_here_kulji_murru", ["kulji", "murru", "yaluy"], "Pebble/stone only; no money sense."),
    row(18, "The ground here is lumpy.", "Bubu murru-murru yaluy.", "bubu murru-murru yaluy | ground lumpy here", "r205_np_adj_here_bubu_murru_murru", ["bubu", "murru-murru", "yaluy"], "Lexicalized lumpy adjective only."),
    row(19, "The stones here are close together.", "Kulji muru-muru yaluy.", "kulji muru-muru yaluy | stones close-together here", "r205_np_adj_here_kulji_muru_muru", ["kulji", "muru-muru", "yaluy"], "Plural reading by context only; no plural morphology."),
    row(20, "The person here is kind.", "Bama manun-manun yaluy.", "bama manun-manun yaluy | person kind here", "r205_np_adj_here_bama_manun_manun", ["bama", "manun-manun", "yaluy"], "Character property only."),
    row(21, "The ground there is flat.", "Bubu barrangkul yinyay.", "bubu barrangkul yinyay | ground flat there", "r205_np_adj_there_bubu_barrangkul", ["bubu", "barrangkul", "yinyay"], "Yinyay support only."),
    row(22, "The wood there is smooth.", "Juku jalngka yinyay.", "juku jalngka yinyay | wood smooth there", "r205_np_adj_there_juku_jalngka", ["juku", "jalngka", "yinyay"], "No itchy/body-state reading."),
    row(23, "The road there is crooked.", "Baral kurru-kurru yinyay.", "baral kurru-kurru yinyay | road crooked there", "r205_np_adj_there_baral_kurru_kurru", ["baral", "kurru-kurru", "yinyay"], "No route/travel event."),
    row(24, "The clothing there is narrow.", "Kambi darra yinyay.", "kambi darra yinyay | clothing narrow there", "r205_np_adj_there_kambi_darra", ["kambi", "darra", "yinyay"], "No bajaku degree."),
    row(25, "The road there is wide.", "Baral yalmbar yinyay.", "baral yalmbar yinyay | road wide there", "r205_np_adj_there_baral_yalmbar", ["baral", "yalmbar", "yinyay"], "Wide property only."),
    row(26, "The person there is quiet.", "Bama kiray yinyay.", "bama kiray yinyay | person quiet there", "r205_np_adj_there_bama_kiray", ["bama", "kiray", "yinyay"], "Quiet property only."),
    row(27, "The pebble there is round.", "Kulji murru yinyay.", "kulji murru yinyay | pebble round there", "r205_np_adj_there_kulji_murru", ["kulji", "murru", "yinyay"], "Round pebble; no secondary money sense."),
    row(28, "The ground there is lumpy.", "Bubu murru-murru yinyay.", "bubu murru-murru yinyay | ground lumpy there", "r205_np_adj_there_bubu_murru_murru", ["bubu", "murru-murru", "yinyay"], "Lexicalized lumpy adjective only."),
    row(29, "The stones there are close together.", "Kulji muru-muru yinyay.", "kulji muru-muru yinyay | stones close-together there", "r205_np_adj_there_kulji_muru_muru", ["kulji", "muru-muru", "yinyay"], "Close-together adjective only."),
    row(30, "The person there is kind.", "Bama manun-manun yinyay.", "bama manun-manun yinyay | person kind there", "r205_np_adj_there_bama_manun_manun", ["bama", "manun-manun", "yinyay"], "Kind property only."),
    row(31, "That ground is flat.", "Yinya bubu barrangkul.", "yinya bubu barrangkul | that ground flat", "r205_yinya_np_adj_bubu_barrangkul", ["yinya", "bubu", "barrangkul"], "Patz Yinya NP ADJ frame."),
    row(32, "That wood is smooth.", "Yinya juku jalngka.", "yinya juku jalngka | that wood smooth", "r205_yinya_np_adj_juku_jalngka", ["yinya", "juku", "jalngka"], "Smooth property only."),
    row(33, "That road is crooked.", "Yinya baral kurru-kurru.", "yinya baral kurru-kurru | that road crooked", "r205_yinya_np_adj_baral_kurru_kurru", ["yinya", "baral", "kurru-kurru"], "Road/path noun only."),
    row(34, "That clothing is narrow.", "Yinya kambi darra.", "yinya kambi darra | that clothing narrow", "r205_yinya_np_adj_kambi_darra", ["yinya", "kambi", "darra"], "Clothing narrow; no degree."),
    row(35, "That road is wide.", "Yinya baral yalmbar.", "yinya baral yalmbar | that road wide", "r205_yinya_np_adj_baral_yalmbar", ["yinya", "baral", "yalmbar"], "Wide property only."),
    row(36, "That person is quiet.", "Yinya bama kiray.", "yinya bama kiray | that person quiet", "r205_yinya_np_adj_bama_kiray", ["yinya", "bama", "kiray"], "Quiet property; no secrecy adverb."),
    row(37, "That pebble is round.", "Yinya kulji murru.", "yinya kulji murru | that pebble round", "r205_yinya_np_adj_kulji_murru", ["yinya", "kulji", "murru"], "Round pebble only."),
    row(38, "That ground is lumpy.", "Yinya bubu murru-murru.", "yinya bubu murru-murru | that ground lumpy", "r205_yinya_np_adj_bubu_murru_murru", ["yinya", "bubu", "murru-murru"], "Lumpy property only."),
    row(39, "Those stones are close together.", "Yinya kulji muru-muru.", "yinya kulji muru-muru | those stones close-together", "r205_yinya_np_adj_kulji_muru_muru", ["yinya", "kulji", "muru-muru"], "Yinya may imply plural by context; no plural morphology."),
    row(40, "That person is kind.", "Yinya bama manun-manun.", "yinya bama manun-manun | that person kind", "r205_yinya_np_adj_bama_manun_manun", ["yinya", "bama", "manun-manun"], "Kind property only."),
    row(41, "That ground there is flat.", "Yinya bubu barrangkul yinyay.", "yinya bubu barrangkul yinyay | that ground flat there", "r205_yinya_np_adj_there_bubu_barrangkul", ["yinya", "bubu", "barrangkul", "yinyay"], "Yinya plus yinyay support."),
    row(42, "That wood there is smooth.", "Yinya juku jalngka yinyay.", "yinya juku jalngka yinyay | that wood smooth there", "r205_yinya_np_adj_there_juku_jalngka", ["yinya", "juku", "jalngka", "yinyay"], "Smooth property; no body-state homonym."),
    row(43, "That road there is crooked.", "Yinya baral kurru-kurru yinyay.", "yinya baral kurru-kurru yinyay | that road crooked there", "r205_yinya_np_adj_there_baral_kurru_kurru", ["yinya", "baral", "kurru-kurru", "yinyay"], "No road travel event."),
    row(44, "That clothing there is narrow.", "Yinya kambi darra yinyay.", "yinya kambi darra yinyay | that clothing narrow there", "r205_yinya_np_adj_there_kambi_darra", ["yinya", "kambi", "darra", "yinyay"], "No degree/comparison."),
    row(45, "That road there is wide.", "Yinya baral yalmbar yinyay.", "yinya baral yalmbar yinyay | that road wide there", "r205_yinya_np_adj_there_baral_yalmbar", ["yinya", "baral", "yalmbar", "yinyay"], "Wide property only."),
    row(46, "That person there is quiet.", "Yinya bama kiray yinyay.", "yinya bama kiray yinyay | that person quiet there", "r205_yinya_np_adj_there_bama_kiray", ["yinya", "bama", "kiray", "yinyay"], "Quiet property only."),
    row(47, "That pebble there is round.", "Yinya kulji murru yinyay.", "yinya kulji murru yinyay | that pebble round there", "r205_yinya_np_adj_there_kulji_murru", ["yinya", "kulji", "murru", "yinyay"], "Round pebble; no money reading."),
    row(48, "That ground there is lumpy.", "Yinya bubu murru-murru yinyay.", "yinya bubu murru-murru yinyay | that ground lumpy there", "r205_yinya_np_adj_there_bubu_murru_murru", ["yinya", "bubu", "murru-murru", "yinyay"], "Lumpy property only."),
    row(49, "Those stones there are close together.", "Yinya kulji muru-muru yinyay.", "yinya kulji muru-muru yinyay | those stones close-together there", "r205_yinya_np_adj_there_kulji_muru_muru", ["yinya", "kulji", "muru-muru", "yinyay"], "Plural context only; no plural morphology."),
    row(50, "That person there is kind.", "Yinya bama manun-manun yinyay.", "yinya bama manun-manun yinyay | that person kind there", "r205_yinya_np_adj_there_bama_manun_manun", ["yinya", "bama", "manun-manun", "yinyay"], "Kind property only; no social event."),
]


def has_token(low: str, token: str) -> bool:
    return re.search(rf"(?<![a-z-]){re.escape(token)}(?![a-z-])", low) is not None


def has_target_morphology(low: str, head: str) -> bool:
    suffixes = "ngka|ngku|angka|nga|ngu|ba|mba|mbu|muny|darr|burr|bu|ka|nka|nku|ji|jirr|ku|mundu|amu|mu|iji|nda|ndu|anda|undu|unji|nji|kari|baka|baja|bungal|bungan|mal|man|rriny"
    return re.search(rf"(?<![a-z-]){re.escape(head)}-(?:{suffixes})\b", low) is not None


def english_has_denied(english_low: str, denied: str) -> bool:
    return denied in english_low if " " in denied else re.search(rf"\b{re.escape(denied)}\b", english_low) is not None


def scan_static() -> list[str]:
    issues: list[str] = []
    if len(ROWS) != 50:
        issues.append(f"{LABEL}: expected 50 rows, found {len(ROWS)}")
    seen_ku: set[str] = set()
    for expected, row_data in enumerate(ROWS, start=1):
        seq, english, kuku, analysis, frame, _tier, words_used, loans_used, evidence, confidence = row_data
        label = f"{LABEL}.{seq:02d}"
        low = kuku.lower()
        english_low = english.lower()
        words = json.loads(words_used)
        if seq != expected:
            issues.append(f"{label}: sequence mismatch; expected {expected}")
        if not english[:1].isupper() or english[-1:] not in ".!?":
            issues.append(f"{label}: English punctuation/capitalization failure")
        if " | " not in analysis:
            issues.append(f"{label}: analysis missing gloss separator")
        if not frame.startswith("r205_"):
            issues.append(f"{label}: Rule 205 frame must start with r205_")
        if low in seen_ku:
            issues.append(f"{label}: duplicate Kuku surface within batch")
        seen_ku.add(low)
        if json.loads(loans_used):
            issues.append(f"{label}: no loans are admitted in Rule 205")
        if "-kari" in low:
            issues.append(f"{label}: privative -kari is forbidden in Rule 205")
        for denied in DENY_KUKU:
            if has_token(low, denied):
                issues.append(f"{label}: denied carry-forward token {denied!r}")
        targets = [head for head in TARGET_HEADS if has_token(low, head)]
        if len(targets) != 1:
            issues.append(f"{label}: expected exactly one Rule 205 target adjective, found {targets}")
        subjects = [head for head in SUBJECT_HEADS if has_token(low, head)]
        if len(subjects) != 1:
            issues.append(f"{label}: expected exactly one support subject noun, found {subjects}")
        for head in targets:
            selected = TARGET_HEADS[head]
            if head not in words:
                issues.append(f"{label}: target head {head} missing from words_used")
            if selected not in english_low:
                issues.append(f"{label}: English must include selected sense {selected!r}")
            if selected.replace(" ", "-") not in analysis and selected not in analysis:
                issues.append(f"{label}: analysis must gloss {head} as {selected}")
            if has_target_morphology(low, head):
                issues.append(f"{label}: target head {head} has forbidden morphology")
        for support in SUPPORT:
            if has_token(low, support) and support not in words:
                issues.append(f"{label}: support {support} missing from words_used")
        if 1 <= seq <= 10 and (has_token(low, "yinya") or has_token(low, "yaluy") or has_token(low, "yinyay")):
            issues.append(f"{label}: rows 1-10 require NP ADJ")
        if 11 <= seq <= 20 and (has_token(low, "yinya") or has_token(low, "yinyay") or not low.endswith(" yaluy.")):
            issues.append(f"{label}: rows 11-20 require NP ADJ yaluy")
        if 21 <= seq <= 30 and (has_token(low, "yinya") or has_token(low, "yaluy") or not low.endswith(" yinyay.")):
            issues.append(f"{label}: rows 21-30 require NP ADJ yinyay")
        if 31 <= seq <= 40 and (not low.startswith("yinya ") or has_token(low, "yaluy") or has_token(low, "yinyay")):
            issues.append(f"{label}: rows 31-40 require Yinya NP ADJ")
        if 41 <= seq <= 50 and (not low.startswith("yinya ") or has_token(low, "yaluy") or not low.endswith(" yinyay.")):
            issues.append(f"{label}: rows 41-50 require Yinya NP ADJ yinyay")
        if any(english_has_denied(english_low, bad) for bad in DENY_ENGLISH):
            issues.append(f"{label}: denied action/event/source/body-state field in English")
        if not evidence or not confidence:
            issues.append(f"{label}: missing evidence/confidence")
    return issues


def main() -> int:
    issues = scan_static()
    if issues:
        print("pre-bank scan failed:")
        for issue in issues:
            print(f"  - {issue}")
        return 1

    db = sqlite3.connect(DB)
    db.execute("PRAGMA foreign_keys = ON")
    db.execute("BEGIN")
    ensure_schema(db)
    existing = db.execute("SELECT id FROM batches WHERE label=?", (LABEL,)).fetchone()
    if existing:
        batch_id = existing[0]
        accepted = db.execute(
            "SELECT COUNT(*) FROM sentences WHERE batch_id=? AND status IN ('accepted','revised')",
            (batch_id,),
        ).fetchone()[0]
        if accepted:
            raise SystemExit(f"{LABEL} already has accepted/revised rows; refusing to overwrite")
        db.execute("DELETE FROM reviews WHERE sentence_id IN (SELECT id FROM sentences WHERE batch_id=?)", (batch_id,))
        db.execute("DELETE FROM sentences WHERE batch_id=?", (batch_id,))
        db.execute("UPDATE batches SET notes=?, created_at=? WHERE id=?", ("rebanked under_review", NOW, batch_id))
    else:
        cur = db.execute(
            "INSERT INTO batches(label, cheatsheet_version, guide_version, notes, created_at) VALUES(?,?,?,?,?)",
            (
                LABEL,
                "Rule 205 draft: low-frequency property-adjective lexical expansion after Rule 204",
                "operator-guide-2026-07-08",
                "under_review",
                NOW,
            ),
        )
        batch_id = cur.lastrowid

    dupes: list[str] = []
    for seq, _english, kuku, *_rest in ROWS:
        hit = db.execute(
            """
            SELECT b.label, s.seq
            FROM sentences s JOIN batches b ON b.id=s.batch_id
            WHERE lower(trim(s.kuku))=lower(trim(?))
              AND s.status!='rejected'
              AND s.batch_id!=?
            """,
            (kuku, batch_id),
        ).fetchone()
        if hit:
            dupes.append(f"{LABEL}.{seq:02d} duplicates {hit[0]}.{hit[1]:02d}: {kuku}")
    if dupes:
        db.rollback()
        print("cross-corpus duplicate check failed:")
        for dupe in dupes:
            print(f"  - {dupe}")
        return 1

    db.executemany(
        """
        INSERT INTO sentences(
          batch_id, seq, english, kuku, analysis, frame, tier, words_used,
          loans_used, evidence, status, confidence, author, created_at
        )
        VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?)
        """,
        [
            (batch_id, *row_data[:-1], "under_review", row_data[-1], "claude", NOW)
            for row_data in ROWS
        ],
    )
    verified_before, words_before = db.execute(
        """
        SELECT COUNT(*), SUM(LENGTH(kuku)-LENGTH(REPLACE(kuku,' ',''))+1)
        FROM sentences
        WHERE status IN ('accepted','revised')
        """
    ).fetchone()
    record_event(
        db,
        batch_label=LABEL,
        phase="source",
        event_type="source-pass",
        actor="claude-main",
        detail="Checked Patz adjective-property grammar plus dictionary entries for barrangkul, jalngka, kurru-kurru, darra, yalmbar, kiray, murru, murru-murru, muru-muru, manun-manun, and support nouns bubu, juku, baral, kambi, kulji, bama, yinya, yaluy, yinyay.",
        command="rg/sed dictionary.yaml and grammar.md around target and support headwords",
        artifact_path=PROCESS_LOG,
        created_at=NOW,
    )
    record_event(
        db,
        batch_label=LABEL,
        phase="scope",
        event_type="lexical-scope",
        actor="claude-main",
        detail="Rule 205 admits only low-frequency non-eventive property adjectives in NP ADJ, NP ADJ yaluy, NP ADJ yinyay, Yinya NP ADJ, and Yinya NP ADJ yinyay frames; excludes negation, privative, degree, comparison, case, derivation, perception, events, source/use, illness/body-state, and productive reduplication.",
        artifact_path=PROCESS_LOG,
        created_at=NOW,
    )
    record_event(
        db,
        batch_label=LABEL,
        phase="preflight",
        event_type="duplicate-preflight",
        actor="claude-main",
        detail="Exact duplicate preflight over all 50 planned Kuku surfaces passed. Initial Baral darra collided with Batch 145 and was recomposed to Kambi darra before banking.",
        command="sqlite3 duplicate-preflight over planned surfaces",
        output_excerpt="No collisions after replacing Baral darra with Kambi darra.",
        artifact_path=PROCESS_LOG,
        created_at=NOW,
    )
    record_event(
        db,
        batch_label=LABEL,
        phase="preflight",
        event_type="static-guard-pass",
        actor="claude-main",
        detail="bank_247.py static guards passed: 50 explicit rows, exactly one target adjective and one support subject per row, no denied Kuku/English fields, no target morphology, correct frame bands, no loans.",
        artifact_path="batch_scripts/bank_247.py",
        created_at=NOW,
    )
    db.commit()
    print(f"banked {LABEL}: rows=50, under_review=50, verified_before_close={verified_before}, words_before={words_before}")
    return 0


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