"""Local diagnostic only; thresholds are examples, not API limits."""
import sys
import warnings
from pathlib import Path
from PIL import Image

MAX_BYTES = 10 * 1024 * 1024
MAX_PIXELS = 20_000_000


def check(path):
    path = Path(path)
    size = path.stat().st_size
    if not 0 < size <= MAX_BYTES:
        raise ValueError("outside this diagnostic's 1-byte to 10-MiB range")
    with warnings.catch_warnings():
        warnings.simplefilter("error", Image.DecompressionBombWarning)
        with Image.open(path) as im:
            fmt, dimensions = im.format, im.size
            if im.width * im.height > MAX_PIXELS:
                raise ValueError("above this diagnostic's 20-million-pixel limit")
            if getattr(im, "n_frames", 1) != 1:
                raise ValueError("multi-frame file: check route support separately")
            im.verify()
        with Image.open(path) as im:
            im.load()
    return {"format": fmt, "dimensions": dimensions, "bytes": size}


if __name__ == "__main__":
    if len(sys.argv) != 2:
        raise SystemExit("Usage: python check_image.py image-body.bin")
    try:
        print(check(sys.argv[1]))
    except Exception as exc:
        raise SystemExit(f"CHECK FAILED: {type(exc).__name__}: {exc}")
