"""Every competition the winners file names has artwork for every tier it awards.

Until 10 Sep the four wine competitions had only their gold and double gold: a silver or
bronze from the New York, Berlin, Melbourne or Asia wine competition drew the text fallback
on cards while the spirits medals drew a badge. The files are the competitions' own 2025
medals, sized to the set's 160 px transparent format. No network, no database: it reads the
directory the app serves at /medals.
"""

import json
import pathlib

from PIL import Image

MEDALS = pathlib.Path(__file__).resolve().parents[1] / "web" / "public" / "medals"
WINNERS = pathlib.Path(__file__).resolve().parents[2] / "import" / "winners.json"
TIERS = ("double-gold", "gold", "silver", "bronze")


def competitions() -> set[str]:
    data = json.loads(WINNERS.read_text())
    rows = data if isinstance(data, list) else next(v for v in data.values() if isinstance(v, list))
    return {(r.get("competition_slug") or "").lower() for r in rows if r.get("competition_slug")}


def test_every_competition_has_all_four_tiers():
    slugs = competitions()
    assert slugs >= {"aisc", "bisc", "misc", "nyisc", "aiwc", "biwc", "miwc", "nyiwc"}
    missing = [f"{slug}-{tier}.png" for slug in sorted(slugs) for tier in TIERS if not (MEDALS / f"{slug}-{tier}.png").is_file()]
    assert not missing, missing


def test_artwork_is_the_sets_format():
    """160 by 160, transparent corners (a round medal, not a white tile)."""
    for path in sorted(MEDALS.glob("*.png")):
        im = Image.open(path).convert("RGBA")
        assert im.size == (160, 160), path.name
        w, h = im.size
        assert all(im.getpixel(p)[3] == 0 for p in ((0, 0), (w - 1, 0), (0, h - 1), (w - 1, h - 1))), path.name
