from __future__ import annotations

from pathlib import Path

from pptx import Presentation
from pptx.enum.shapes import MSO_SHAPE_TYPE


def main() -> None:
    base_dir = Path(__file__).resolve().parent
    src = base_dir / "resources" / "inputs" / "ai_work_source.pptx"
    out_dir = base_dir / "resources" / "assets" / "ai_work_source_assets"
    out_dir.mkdir(parents=True, exist_ok=True)

    prs = Presentation(str(src))
    for idx, slide in enumerate(prs.slides, start=1):
        pics = [sh for sh in slide.shapes if sh.shape_type == MSO_SHAPE_TYPE.PICTURE]
        if not pics:
            continue
        if len(pics) != 1:
            print(f"Slide {idx}: pictures={len(pics)} (expected 1). Extracting all.")

        for j, pic in enumerate(pics, start=1):
            image = pic.image
            ext = image.ext or "bin"
            out = out_dir / f"slide{idx:02d}_img{j:02d}.{ext}"
            out.write_bytes(image.blob)
            print(f"wrote {out} ({len(image.blob):,} bytes)")


if __name__ == "__main__":
    main()

