# Cruza los 5 packs (PAWCHISE + Purah) contra biblioteca.
# Comparador para cerrar conclusiones (09-11-2026).

import pathlib
import re
import json
from collections import Counter, defaultdict

BASE = pathlib.Path(r"C:\Users\Usuario\Downloads\SD")
OUT = BASE / "Archivos" / "wd14" / "tags" / "pawchive"

data = {}
# scores principales por pack dentro de pawchive/scores.json
main = json.loads((OUT / "scores.json").read_text(encoding="utf-8"))
for k, v in main.items():
    data[k] = v
purah = json.loads((OUT / "purah" / "scores.json").read_text(encoding="utf-8"))
for k, v in purah.items():
    data["Purah/" + k] = v

# Alias de packs bonitos
def pack_of(key: str) -> str:
    name = key.split("/")[0]
    return {"Emma Frost Marvel Rivals": "Emma",
            "Marin Kitagawa 103": "Marin",
            "Nami 140": "Nami",
            "Phoebe Wuthering Waves": "Phoebe",
            "Purah": "Purah"}.get(name, name)

by_pack = defaultdict(Counter)
by_pack_n = Counter()
norm_count = Counter()
for key, pairs in data.items():
    pk = pack_of(key)
    by_pack_n[pk] += 1
    seen = set()
    for tag, score in pairs:
        if score >= 0.35:
            seen.add(tag)
            by_pack[pk][tag] += 1
            norm_count[tag] += 1

print("imágenes por pack:", dict(by_pack_n))

lib_text = ""
for p in (BASE / "Archivos" / "wildcards").glob("*.txt"):
    lib_text += p.read_text(encoding="utf-8").lower().replace("_", " ") + "\n"
for extra in [
    BASE / "Archivos/pose_bank/BIBLIOTECA_UNIFICADA.md",
    BASE / "Archivos/pose_bank/explicit1_acts_biblioteca.md",
    BASE / "Archivos/pose_bank/explicit2_positions_biblioteca.md",
    BASE / "Archivos/pose_bank/attire_biblioteca.md",
]:
    if extra.exists():
        lib_text += extra.read_text(encoding="utf-8").lower().replace("_", " ") + "\n"
lib_tokens = set(re.findall(r"[a-z0-9]+", lib_text))
lib_tags = set()
for p in (BASE / "Archivos" / "wildcards").glob("*.txt"):
    for line in p.read_text(encoding="utf-8").splitlines():
        for t in line.split(","):
            lib_tags.add(t.strip().lower())

def covered(tag: str) -> bool:
    t = tag.lower()
    if t in lib_tags:
        return True
    parts = set(re.findall(r"[a-z0-9]+", t.replace("_", " ")))
    return parts and parts.issubset(lib_tokens)

TOP = 35
for pk in ["Emma", "Marin", "Nami", "Phoebe", "Purah"]:
    cnt = by_pack[pk]
    print(f"\n=== {pk} top {TOP} ({by_pack_n[pk]} imgs) ===")
    for tag, c in cnt.most_common(TOP):
        print(f"{c:4d}  {tag}" + ("" if covered(tag) else "  <-- AUSENTE"))

print("\n=== TOP global ausentes (>= 15 imgs, sin tags de identidad) ===")
skip = {"blonde_hair", "black_hair", "brown_hair", "purple_hair", "white_hair",
        "pink_hair", "red_hair", "gray_hair", "orange_hair", "green_hair",
        "long_hair", "short_hair", "multicolored_hair", "gradient_hair",
        "blue_eyes", "red_eyes", "brown_eyes", "orange_eyes", "purple_eyes",
        "green_eyes", "hetero", "patreon_username", "teeth", "upper_teeth_only",
        "veins", "veiny_penis", "fingernails", "fingernails", "upper_teeth_only"}
missing = [(t, c) for t, c in norm_count.most_common() if not covered(t) and c >= 15 and t not in skip and "(" not in t]
for t, c in missing[:60]:
    print(f"{c:4d}  {t}")
