# -*- coding: utf-8 -*-
"""Full-lexicon vocabulary-{de,la}.el emitters (custom field mapping for the
German declension/gender API and the Latin case-paradigm API). Reuses the
chunked seed-fn writer from gen_elp_seed_full.
"""
import sys, importlib
from gen_elp_seed_full import write_seed

def uw(x):
    """Unwrap (form, source) tuples that some morphology fns return."""
    if isinstance(x, (tuple, list)):
        return x[0] if x else ""
    return x if x is not None else ""

def build_de():
    M = importlib.import_module("morphology_de_full")
    rows = []; st = {"verbs":0,"nouns":0,"adjs":0}
    # nouns: form0=nom-sg(lemma) form1=plural form2=gender
    for lem in sorted(M._NOUNS):
        if not lem: continue
        try:
            g = uw(M.noun_gender(lem))
            pl = uw(M.pluralize(lem))
        except Exception:
            continue
        rows.append([lem, "noun", lem, pl, g or "", "", "gender:lexicon"])
        st["nouns"] += 1
    # adjs: form0=positive form1=comparative form2=superlative
    for lem in sorted(M._ADJS):
        if not lem: continue
        try:
            cmpr = uw(M.comparative(lem))
            sprl = uw(M.superlative(lem))
        except Exception:
            continue
        rows.append([lem, "adj", lem, cmpr, sprl, "", "degree:lexicon"])
        st["adjs"] += 1
    # verbs (only the ~30 irregular/strong stems the cache carries):
    # form0=pres-3sg form1=past-3sg form2=past-participle
    if hasattr(M, "_VERBS"):
        for lem in sorted({k[0] if isinstance(k, tuple) else k for k in M._VERBS}):
            if not lem: continue
            try:
                f0 = uw(M.finite(lem, "present", "third", "singular"))
                f1 = uw(M.finite(lem, "past", "third", "singular"))
                pp = uw(M.past_participle(lem))
            except Exception:
                continue
            rows.append([lem, "verb", f0, f1, pp, "", "class:strong/irregular"])
            st["verbs"] += 1
    return rows, st

def build_la():
    M = importlib.import_module("morphology_lat_full")
    rows = []; st = {"verbs":0,"nouns":0,"adjs":0}
    def dn(lem, c, n):
        try:
            r = M.decline_noun(lem, c, n)
            return uw(r)
        except Exception:
            return ""
    # nouns: dictionary citation — form0=nom-sg form1=gen-sg form2=gender
    for lem in sorted(M._NOUNS):
        if not lem: continue
        nom = dn(lem, "NOM", "SG") or lem
        gen = dn(lem, "GEN", "SG")
        try: g = uw(M.noun_gender(lem))
        except Exception: g = ""
        rows.append([lem, "noun", nom, gen, g, "", "case-paradigm nom/gen-sg"])
        st["nouns"] += 1
    # adjs: three-gender nom-sg citation — form0=masc form1=fem form2=neut
    for lem in sorted(M._ADJS):
        if not lem: continue
        try:
            m = uw(M.decline_adj(lem, "NOM", "MASC", "SG")) or lem
            f = uw(M.decline_adj(lem, "NOM", "FEM", "SG"))
            nt = uw(M.decline_adj(lem, "NOM", "NEUT", "SG"))
        except Exception:
            continue
        rows.append([lem, "adj", m, f, nt, "", "3-gender nom-sg"])
        st["adjs"] += 1
    # verbs: principal parts — form0=pres-ind-1sg form1=pres-infinitive form2=perf-participle
    if hasattr(M, "_VERBS"):
        for lem in sorted({k[0] if isinstance(k, tuple) else k for k in M._VERBS}):
            if not lem: continue
            try:
                f0 = uw(M.conjugate(lem, "present", "indicative", "active", "first", "singular"))
                inf = uw(M.infinitive(lem, "present", "active"))
                pp = uw(M.participle(lem, "perfect", "nom", "m", "singular"))
            except Exception:
                continue
            rows.append([lem, "verb", f0, inf, pp, "", "principal-parts pres1sg/inf/pfppl"])
            st["verbs"] += 1
    return rows, st

if __name__ == "__main__":
    lang = sys.argv[1]; out = sys.argv[2]
    rows, st = build_de() if lang == "de" else build_la()
    total, _ = write_seed(lang, rows, st, out)
    print(f"{lang}: wrote {out} total={total} verbs={st['verbs']} nouns={st['nouns']} adjs={st['adjs']}")
