"""Gera o app/estilos.json a partir de uma tabela compacta.

Escrever 60 estilos a mao em JSON e convite a erro de digitacao e a valores
inconsistentes entre eles. Aqui cada estilo declara so o que o diferencia; o
resto vem do padrao do grupo. Rode depois de mexer na tabela:

    .venv\\Scripts\\python.exe app\ferramentas\gerar_estilos.py
"""
import json
from pathlib import Path

RAIZ = Path(__file__).resolve().parents[2]
FONTS = json.loads((RAIZ / "app" / "fontes.json").read_text("utf-8"))

# Base dos estilos limpos: branco, sem contorno, sombra suave.
LIMPO = {
    "group": "limpo", "case": "lower", "mode": "highlight", "words": 5, "lines": 1,
    "color": "#FFFFFF", "hl": "#FFFFFF",
    "outline": {"color": "#000000", "w": 0.0},
    "shadow": {"color": "rgba(0,0,0,.5)", "d": 0.05, "soft": True},
    "glow": 0, "box": None, "hlScale": 1.0, "underline": False, "rotate": 0,
}
COR = {**LIMPO, "group": "cor", "shadow": {"color": "#000000", "d": 0.05},
       "outline": {"color": "#000000", "w": 0.045}, "hlScale": 1.08}
CAIXA = {**LIMPO, "group": "caixa", "shadow": {"color": "#000000", "d": 0},
         "box": {"color": "rgba(0,0,0,.85)", "hl": "rgba(0,0,0,.85)", "pad": 0.25}}

# (id, nome, etiqueta, base, ajustes)
TABELA = [
    # ---------- limpos: sem serifa, uma linha ----------
    ("minima", "Minima", "Limpa e grossa", LIMPO, {"font": "figtree", "size": 0.062}),
    ("basalto", "Basalto", "Larga e pesada", LIMPO, {"font": "archivo", "size": 0.056, "case": "upper", "words": 4, "hlScale": 1.06}),
    ("pilar", "Pilar", "Alta e estreita", LIMPO, {"font": "anton", "size": 0.084, "case": "upper", "words": 4, "hlScale": 1.05}),
    ("prisma", "Prisma", "Espaçada", LIMPO, {"font": "bebas", "size": 0.082, "case": "upper", "words": 4, "hlScale": 1.05}),
    ("brasa", "Brasa", "Seca", LIMPO, {"font": "chivo", "size": 0.058, "case": "upper", "words": 4}),
    ("norte", "Norte", "Arredondada", LIMPO, {"font": "manrope", "size": 0.058, "words": 6}),
    ("vertice", "Vértice", "Geométrica", LIMPO, {"font": "sora", "size": 0.054, "case": "upper", "words": 4}),
    ("lume", "Lume", "Macia", LIMPO, {"font": "gabarito", "size": 0.06, "words": 5}),
    ("seda", "Seda", "Fina e larga", LIMPO, {"font": "raleway", "size": 0.06, "words": 6}),
    ("orla", "Orla", "Moderna", LIMPO, {"font": "spacegrotesk", "size": 0.058, "words": 5}),
    ("muro", "Muro", "Cheia", LIMPO, {"font": "rubik", "size": 0.056, "case": "upper", "words": 4, "hlScale": 1.05}),
    ("quadro", "Quadro", "Contemporânea", LIMPO, {"font": "bricolage", "size": 0.058, "words": 5}),
    ("astro", "Astro", "Futurista", LIMPO, {"font": "unbounded", "size": 0.046, "case": "upper", "words": 3}),
    ("flexa", "Flexa", "Técnica", LIMPO, {"font": "anybody", "size": 0.056, "case": "upper", "words": 4}),
    ("epico", "Épico", "Angulosa", LIMPO, {"font": "epilogue", "size": 0.058, "case": "upper", "words": 4}),
    ("calmo", "Calmo", "Suave", LIMPO, {"font": "jakarta", "size": 0.056, "words": 6}),
    ("rota", "Rota", "Legível", LIMPO, {"font": "lexend", "size": 0.054, "words": 6}),
    ("ferro", "Ferro", "Compacta", LIMPO, {"font": "spartan", "size": 0.054, "case": "upper", "words": 4}),
    ("bruma", "Bruma", "Redonda", LIMPO, {"font": "nunito", "size": 0.06, "words": 6}),
    ("onda", "Onda", "Divertida", LIMPO, {"font": "fredoka", "size": 0.062, "words": 5}),

    ("runa", "Runa", "Estranha e bonita", LIMPO, {"font": "syne", "size": 0.056, "case": "upper", "words": 4}),
    ("cais", "Cais", "Neutra", LIMPO, {"font": "onest", "size": 0.058, "words": 6}),
    ("lastro", "Lastro", "Firme", LIMPO, {"font": "schibsted", "size": 0.058, "words": 5}),
    ("balao", "Balão", "Gorda e redonda", LIMPO, {"font": "bowlby", "size": 0.052, "case": "upper", "words": 3, "hlScale": 1.06}),
    ("clube", "Clube", "Retrô arredondada", LIMPO, {"font": "righteous", "size": 0.058, "case": "upper", "words": 4}),
    ("selo", "Selo", "Grossa e curta", LIMPO, {"font": "bakbak", "size": 0.056, "case": "upper", "words": 4}),
    ("nave", "Nave", "Bem larga", LIMPO, {"font": "michroma", "size": 0.042, "case": "upper", "words": 3}),

    # ---------- limpos: condensadas ----------
    ("lanca", "Lança", "Estreita", LIMPO, {"font": "antonio", "size": 0.072, "case": "upper", "words": 5}),
    ("torre", "Torre", "Bem estreita", LIMPO, {"font": "teko", "size": 0.086, "case": "upper", "words": 6}),
    ("trilho", "Trilho", "Longa", LIMPO, {"font": "barlowcond", "size": 0.07, "case": "upper", "words": 6}),
    ("ombro", "Ombro", "Alongada", LIMPO, {"font": "bigshoulders", "size": 0.076, "case": "upper", "words": 5}),
    ("siam", "Siam", "Fina condensada", LIMPO, {"font": "kanit", "size": 0.06, "case": "upper", "words": 5}),
    ("cume", "Cume", "Condensada", LIMPO, {"font": "oswald", "size": 0.062, "case": "upper", "words": 5, "lines": 2, "line2": {"sizeMul": 1.3}}),

    # ---------- limpos: duas linhas ----------
    ("million", "Million", "Fina + grossa", LIMPO, {"font": "montlight", "size": 0.058, "words": 6, "lines": 2,
                                                    "line2": {"font": "montblack", "sizeMul": 1.18},
                                                    "shadow": {"color": "rgba(0,0,0,.55)", "d": 0.05, "soft": True}}),
    ("garnet", "Garnet", "Duas alturas", LIMPO, {"font": "anton", "size": 0.07, "case": "upper", "words": 4, "lines": 2,
                                                 "line2": {"sizeMul": 1.42}}),
    ("aurora", "Aurora", "Fina em cima", LIMPO, {"font": "montlight", "size": 0.05, "words": 6, "lines": 2,
                                                 "line2": {"font": "anton", "sizeMul": 1.6, "case": "upper"}}),
    ("duna", "Duna", "Leve + firme", LIMPO, {"font": "poppinslight", "size": 0.052, "words": 6, "lines": 2,
                                             "line2": {"font": "poppins", "sizeMul": 1.22}}),
    ("eco", "Eco", "Pequena + enorme", LIMPO, {"font": "figtree", "size": 0.044, "words": 6, "lines": 2,
                                               "line2": {"sizeMul": 1.75}}),
    ("grafite", "Grafite", "Duas linhas iguais", LIMPO, {"font": "rubik", "size": 0.056, "case": "upper", "words": 6, "lines": 2,
                                                         "line2": {"sizeMul": 1.0}, "hlScale": 1.05}),
    ("editorial", "Editorial", "Serifa embaixo", LIMPO, {"font": "montlight", "size": 0.048, "words": 6, "lines": 2,
                                                         "line2": {"font": "playfair", "sizeMul": 1.5}}),
    ("contraste", "Contraste", "Fina + maiúscula", LIMPO, {"font": "ralewaylight", "size": 0.05, "words": 6, "lines": 2,
                                                           "line2": {"font": "archivo", "sizeMul": 1.4, "case": "upper"}}),
    ("sopro", "Sopro", "Leve + preta", LIMPO, {"font": "outfitlight", "size": 0.052, "words": 6, "lines": 2,
                                               "line2": {"font": "outfit", "sizeMul": 1.34}}),
    ("degrau", "Degrau", "Degradê de peso", LIMPO, {"font": "jakartalight", "size": 0.05, "words": 6, "lines": 2,
                                                    "line2": {"font": "jakarta", "sizeMul": 1.45}}),
    ("quebra", "Quebra", "Redonda + seca", LIMPO, {"font": "quicksand", "size": 0.052, "words": 6, "lines": 2,
                                                   "line2": {"font": "chivo", "sizeMul": 1.35, "case": "upper"}}),
    ("bloco", "Bloco", "Compacta", LIMPO, {"font": "passion", "size": 0.08, "case": "upper", "words": 5, "lines": 2,
                                           "line2": {"sizeMul": 1.0}, "hlScale": 1.05}),

    # ---------- limpos: serifadas ----------
    ("serifa", "Serifa", "Elegante", LIMPO, {"font": "playfair", "size": 0.064, "shadow": {"color": "rgba(0,0,0,.6)", "d": 0.05, "soft": True}}),
    ("linho", "Linho", "Serifa inclinada", LIMPO, {"font": "playfairit", "size": 0.07}),
    ("gazeta", "Gazeta", "De jornal", LIMPO, {"font": "dmserif", "size": 0.066}),
    ("classico", "Clássico", "Fina serifada", LIMPO, {"font": "instrument", "size": 0.078}),
    ("tomo", "Tomo", "Livro", LIMPO, {"font": "baskerville", "size": 0.056}),
    ("verso", "Verso", "Serifa em itálico", LIMPO, {"font": "loraital", "size": 0.06}),
    ("haste", "Haste", "Alta e fina", LIMPO, {"font": "cormorant", "size": 0.08}),
    ("moda", "Moda", "De revista", LIMPO, {"font": "bodoni", "size": 0.066, "case": "upper", "words": 4}),
    ("raiz", "Raiz", "Serifa macia", LIMPO, {"font": "fraunces", "size": 0.06}),
    ("peso", "Peso", "Serifa gorda", LIMPO, {"font": "abril", "size": 0.064}),
    ("laje", "Laje", "Serifa quadrada", LIMPO, {"font": "alfaslab", "size": 0.056, "case": "upper", "words": 4}),

    # ---------- limpos: manuscritas e monoespacadas ----------
    ("traco", "Traço", "À mão", LIMPO, {"font": "caveat", "size": 0.09, "hlScale": 1.06}),
    ("giz", "Giz", "Pincel", LIMPO, {"font": "marker", "size": 0.062, "case": "upper", "words": 4}),
    ("bilhete", "Bilhete", "Caderno", LIMPO, {"font": "kalam", "size": 0.066}),
    ("cartaz", "Cartaz", "Estreita à mão", LIMPO, {"font": "amatic", "size": 0.105, "case": "upper", "words": 5}),
    ("brilho", "Brilho", "Cursiva", LIMPO, {"font": "lobster", "size": 0.072}),
    ("charme", "Charme", "Cursiva macia", LIMPO, {"font": "pacifico", "size": 0.07}),
    ("terminal", "Terminal", "Monoespaçada", LIMPO, {"font": "mono", "size": 0.046, "words": 6}),
    ("codigo", "Código", "Máquina de escrever", LIMPO, {"font": "spacemono", "size": 0.046, "words": 6}),

    # ---------- limpos: jeitos diferentes de destacar ----------
    ("nevoa", "Névoa", "Só a palavra falada", LIMPO, {"font": "montlight", "size": 0.062, "mode": "karaoke",
                                                      "dim": "rgba(255,255,255,.35)"}),
    ("solo", "Solo", "Uma palavra grande", LIMPO, {"font": "anton", "size": 0.12, "case": "upper", "mode": "single", "words": 1}),
    ("foco", "Foco", "Uma palavra limpa", LIMPO, {"font": "figtree", "size": 0.1, "mode": "single", "words": 1}),
    ("surge", "Surge", "Vai aparecendo", LIMPO, {"font": "outfit", "size": 0.056, "mode": "reveal", "words": 6}),
    ("sublinha", "Sublinha", "Risco embaixo", LIMPO, {"font": "inter", "size": 0.056, "words": 6, "underline": True}),
    ("realce", "Realce", "Aumenta bastante", LIMPO, {"font": "interbold", "size": 0.052, "words": 5, "hlScale": 1.3}),

    # ---------- coloridos ----------
    ("hustle", "Hustle", "Torta e colorida", COR, {"font": "montblack", "size": 0.058, "case": "upper", "words": 6, "lines": 2,
                                                   "palette": ["#FFFFFF", "#FFE14D", "#FF5A4F"], "rotate": 3.5,
                                                   "outline": {"color": "#000000", "w": 0.03},
                                                   "shadow": {"color": "#000000", "d": 0.09}, "hlScale": 1.06}),
    ("impacto", "Impacto", "Viral", COR, {"font": "anton", "size": 0.07, "case": "upper", "hl": "#FFD23F",
                                          "outline": {"color": "#000000", "w": 0.055}, "hlScale": 1.12}),
    ("hormozi", "Hormozi", "Verde", COR, {"font": "montblack", "size": 0.06, "case": "upper", "words": 4, "hl": "#3CFF7A",
                                          "outline": {"color": "#000000", "w": 0.06}, "hlScale": 1.1}),
    ("chamada", "Chamada", "Vermelho e branco", COR, {"font": "passion", "size": 0.078, "case": "upper", "words": 5, "lines": 2,
                                                      "palette": ["#FFFFFF", "#FF3B30"], "shadow": {"color": "#000000", "d": 0.06}}),
    ("uma", "Uma por vez", "Palavra sozinha", COR, {"font": "anton", "size": 0.115, "case": "upper", "mode": "single", "words": 1,
                                                    "color": "#FFD23F", "hl": "#FFD23F", "hlScale": 1.0,
                                                    "outline": {"color": "#000000", "w": 0.06},
                                                    "shadow": {"color": "#000000", "d": 0.07}}),
    ("karaoke", "Karaokê", "Palavra a palavra", COR, {"font": "poppins", "size": 0.058, "mode": "karaoke", "hl": "#FFD23F",
                                                      "dim": "rgba(255,255,255,.42)", "outline": {"color": "#000000", "w": 0.04},
                                                      "shadow": {"color": "#000000", "d": 0.035}, "hlScale": 1.06}),
    ("titulo", "Título", "Grossa e larga", COR, {"font": "archivo", "size": 0.058, "case": "upper", "words": 4, "lines": 2,
                                                 "line2": {"font": "rubik", "sizeMul": 1.0}, "hl": "#FFD23F",
                                                 "outline": {"color": "#000000", "w": 0.04}, "hlScale": 1.06}),
    ("pop", "Pop", "Sombra dura", COR, {"font": "bungee", "size": 0.054, "case": "upper", "words": 4,
                                        "color": "#FFD23F", "hl": "#FFFFFF", "outline": {"color": "#1A1400", "w": 0.03},
                                        "shadow": {"color": "#E5322D", "d": 0.09}, "hlScale": 1.1}),
    ("neon", "Neon", "Brilho", COR, {"font": "bebas", "size": 0.078, "case": "upper", "words": 4, "hl": "#FF4FD8",
                                     "outline": {"color": "#4FE3FF", "w": 0.0}, "shadow": {"color": "#000000", "d": 0},
                                     "glow": 0.09, "hlScale": 1.08}),
    ("ciber", "Ciber", "Espacial", COR, {"font": "orbitron", "size": 0.05, "case": "upper", "words": 3, "hl": "#4FE3FF",
                                         "outline": {"color": "#4FE3FF", "w": 0.0}, "shadow": {"color": "#000000", "d": 0},
                                         "glow": 0.07}),
    ("manuscrito", "Manuscrito", "À mão, amarela", COR, {"font": "caveat", "size": 0.085, "hl": "#FFD23F",
                                                         "outline": {"color": "#000000", "w": 0.03},
                                                         "shadow": {"color": "#000000", "d": 0.04}, "hlScale": 1.08}),
    ("festa", "Festa", "Três cores", COR, {"font": "titan", "size": 0.058, "case": "upper", "words": 5, "lines": 2,
                                           "palette": ["#FFD23F", "#4FE3FF", "#FF4FD8"],
                                           "outline": {"color": "#000000", "w": 0.04},
                                           "shadow": {"color": "#000000", "d": 0.055}}),

    # ---------- com fundo ----------
    ("caixa", "Caixa", "Fundo escuro", CAIXA, {"font": "inter", "size": 0.05, "hl": "#1A1400",
                                               "box": {"color": "rgba(0,0,0,.82)", "hl": "#FFD23F", "pad": 0.26}}),
    ("caixabranca", "Caixa branca", "Fundo claro", CAIXA, {"font": "archivo", "size": 0.05, "case": "upper", "words": 4,
                                                           "color": "#111111", "hl": "#FFFFFF",
                                                           "box": {"color": "rgba(255,255,255,.92)", "hl": "#E5322D", "pad": 0.24}}),
    ("maquina", "Máquina", "Vai aparecendo", CAIXA, {"font": "mono", "size": 0.05, "mode": "reveal", "words": 6,
                                                     "color": "#F2F1EC", "hl": "#FFD23F",
                                                     "box": {"color": "rgba(0,0,0,.85)", "hl": "rgba(0,0,0,.85)", "pad": 0.26}}),
    ("retro", "Retrô", "Amarelo e preto", CAIXA, {"font": "oswald", "size": 0.062, "case": "upper", "mode": "karaoke",
                                                  "color": "#FFD23F", "hl": "#FFFFFF", "dim": "rgba(255,210,63,.45)",
                                                  "box": {"color": "rgba(0,0,0,.85)", "hl": "rgba(0,0,0,.85)", "pad": 0.24}}),
    ("fita", "Fita", "Faixa colorida", CAIXA, {"font": "figtree", "size": 0.052, "case": "upper", "words": 4,
                                               "color": "#FFFFFF", "hl": "#111111",
                                               "box": {"color": "rgba(20,20,26,.9)", "hl": "#3CFF7A", "pad": 0.26}}),
    ("nota", "Nota", "Papel", CAIXA, {"font": "kalam", "size": 0.058, "words": 5, "color": "#1A1400", "hl": "#B22222",
                                      "box": {"color": "rgba(255,250,235,.94)", "hl": "rgba(255,250,235,.94)", "pad": 0.26}}),
]

estilos, faltando = [], []
for sid, nome, tag, base, ajustes in TABELA:
    usadas = [ajustes.get("font")] + [ajustes.get("line2", {}).get("font")]
    ausentes = [f for f in usadas if f and f not in FONTS]
    if ausentes:
        faltando.append(f"{sid}: fonte {', '.join(ausentes)} não foi baixada")
        continue
    st = {"id": sid, "name": nome, "tag": tag}
    st.update({k: v for k, v in base.items()})
    st.update(ajustes)
    estilos.append(st)

ids = [s["id"] for s in estilos]
assert len(ids) == len(set(ids)), "id repetido: " + str([i for i in ids if ids.count(i) > 1])

(RAIZ / "app" / "estilos.json").write_text(json.dumps(estilos, ensure_ascii=False, indent=1), "utf-8")
from collections import Counter
print(f"{len(estilos)} estilos gravados em app/estilos.json", dict(Counter(s['group'] for s in estilos)))
for f in faltando:
    print("  fora:", f)
