#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Cari desain berlatar HITAM di folder aset (untuk pertanyaan Bos "aku pernah kasih design background hitam")."""
import glob
import os
import sys
from PIL import Image

BASE = "/home/mantapman/.hermes/profiles/atun/workspace"
CACHE = "/home/mantapman/.hermes/profiles/atun/cache/images"

DIRS = [d for d in sorted(glob.glob(f"{BASE}/*")) if os.path.isdir(d)]
FOKUS = [d for d in DIRS if any(k in os.path.basename(d).lower()
                                for k in ("tslaw", "law", "mitos", "desain", "cache", "lib", "asset"))]


def gelap(path):
    try:
        im = Image.open(path).convert("RGB")
        w, h = im.size
        titik = [(8, 8), (w - 9, 8), (8, h - 9), (w - 9, h - 9), (w // 2, h // 10)]
        vals = [sum(im.getpixel(p)) / 3 for p in titik]
        return min(vals), max(vals), im.size
    except Exception as e:
        return None, None, str(e)


def pindai(label, folder, pola=("*.jpg", "*.jpeg", "*.png", "*.webp")):
    files = []
    for p in pola:
        files += glob.glob(os.path.join(folder, p))
    if not files:
        return
    hitam = []
    for f in sorted(files):
        mn, mx, size = gelap(f)
        if mn is None:
            continue
        if mx < 70:            # seluruh sudut gelap
            hitam.append((f, mn, mx, size))
    if hitam:
        print(f"\n### {label} — {len(hitam)}/{len(files)} file latar gelap")
        for f, mn, mx, size in hitam[:25]:
            print(f"  {os.path.basename(f):45} min={mn:5.1f} max={mx:5.1f} {size}")
        if len(hitam) > 25:
            print(f"  ... +{len(hitam)-25} lagi")


print("=== folder fokus ===")
for d in FOKUS:
    pindai(os.path.basename(d), d)
    # satu level di bawahnya
    for sub in sorted(glob.glob(f"{d}/*")):
        if os.path.isdir(sub):
            pindai(os.path.basename(d) + "/" + os.path.basename(sub), sub)

pindai("cache/images (kiriman Telegram)", CACHE)
