"""장르별 그라데이션 배경 + 텍스트 오버레이 — cover/video 공용."""
from PIL import Image, ImageDraw, ImageFont

GENRE_COLORS = {
    "lo-fi":   ((26, 26, 46),  (22, 33, 62)),
    "phonk":   ((26, 10, 10),  (45, 0,  0)),
    "ambient": ((13, 33, 55),  (10, 22, 40)),
    "pop":     ((26, 10, 46),  (45, 27, 78)),
    "default": ((17, 24, 39),  (31, 41, 55)),
}


def make_gradient_with_title(genre: str, title: str, out_path: str,
                              size: tuple[int, int] = (1024, 1024),
                              quality: int = 92) -> None:
    w, h = size
    top, bot = GENRE_COLORS.get(genre.lower(), GENRE_COLORS["default"])
    with Image.new("RGB", (w, h)) as img:
        px = img.load()
        for y in range(h):
            t = y / h
            r = int(top[0] + (bot[0] - top[0]) * t)
            g = int(top[1] + (bot[1] - top[1]) * t)
            b = int(top[2] + (bot[2] - top[2]) * t)
            for x in range(w):
                px[x, y] = (r, g, b)

        if title:
            try:
                font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 64)
            except OSError:
                font = ImageFont.load_default()
            draw = ImageDraw.Draw(img)
            bbox = draw.textbbox((0, 0), title, font=font)
            tw, th = bbox[2] - bbox[0], bbox[3] - bbox[1]
            draw.text(((w - tw) // 2, (h - th) // 2), title, fill=(255, 255, 255), font=font)

        img.save(out_path, "JPEG", quality=quality)
