"""판정기가 「반복」을 본 이유가 **크기**인지 가른다 (A/B).

가설: 변환본이 원본의 4배(5456×3045 vs 1376×768)로 **축소 없이** 판정기에
간다(`multiroll_gemini.png_part`). 큰 이미지는 타일로 쪼개져 지각되고,
판정기가 그 타일을 「여러 가로 구간으로 반복」이라고 서술했다.

가르는 법: **같은 변환본**을 (가) 원본 그대로 (나) 원본 크기로 줄여서
각각 판정에 태운다. 내용은 한 비트도 안 바뀌었으므로, 판정이 뒤집히면
원인은 그림이 아니라 **크기**다.

★양성 대조 — 실제로 소품이 사라진 S2sh3 도 같이 태운다. 그것까지 통과로
 뒤집히면 「크기 때문」이 아니라 「줄이면 아무것도 안 보인다」는 뜻이다.
"""
from __future__ import annotations

import io
import json
import sys
from pathlib import Path

from PIL import Image

RECIPE = Path(
    "projects/f1cf927b-c0e2-4dc9-ae2e-e29b6c5bf913"
    "/images/8750a231-f2d4-4855-9a5a-8d7f9a3a0bc9/scene/recipe"
)
TAGS = ["S3sh1", "S2sh6", "S2sh3"]


def to_source_size(result_png: bytes, source_png: bytes) -> bytes:
    src = Image.open(io.BytesIO(source_png))
    res = Image.open(io.BytesIO(result_png))
    out = io.BytesIO()
    res.resize(src.size, Image.LANCZOS).save(out, format="PNG")
    return out.getvalue()


def main() -> None:
    root = Path(__file__).resolve().parents[1]
    recipe = root / RECIPE
    from app.modules.pipeline.cine_verify import verify_cine_result

    for tag in TAGS:
        src = (recipe / f"{tag}_sel.png").read_bytes()
        res = (recipe / f"{tag}_cine.png").read_bytes()
        small = to_source_size(res, src)
        print("=" * 78)
        print(f"{tag}  원본={Image.open(io.BytesIO(src)).size}  "
              f"변환본={Image.open(io.BytesIO(res)).size}  "
              f"축소본={Image.open(io.BytesIO(small)).size}")
        print("=" * 78)
        for label, payload in (("가) 원본 크기 그대로", res),
                               ("나) 원본 크기로 축소", small)):
            v = verify_cine_result(
                source_png=src, result_png=payload,
                step_tag=f"scale_ab_{tag}", rounds=2)
            print(f"  {label}: ok={v['ok']}  "
                  f"changed={v.get('changed')}  split={v.get('split')}")
            for axis, info in (v.get("axes") or {}).items():
                if not info.get("same"):
                    print(f"      [{axis}] {str(info.get('note'))[:110]}")
        print()


if __name__ == "__main__":
    sys.exit(main())
