#!/usr/bin/env python3
"""컨셉 #18 - Playwright로 HTML 템플릿 캡처"""

from pathlib import Path
from playwright.sync_api import sync_playwright

WORK_DIR = Path("/home/jay/workspace/output/meta-ads/concept-catalog/18-portrait-text")
TEMPLATE_PATH = WORK_DIR / "template.html"
OUTPUT_PATH = WORK_DIR / "sample.png"


def main():
    print(f"[캡처] HTML 템플릿: {TEMPLATE_PATH}")

    with sync_playwright() as p:
        browser = p.chromium.launch()
        try:
            page = browser.new_page(viewport={"width": 1080, "height": 1080})

            # 파일 URL로 로드
            template_url = f"file://{TEMPLATE_PATH.resolve()}"
            page.goto(template_url, wait_until="networkidle")

            # 폰트 및 이미지 렌더링 대기
            page.wait_for_timeout(2500)

            # 스크린샷 캡처
            page.screenshot(
                path=str(OUTPUT_PATH),
                type="png",
                clip={"x": 0, "y": 0, "width": 1080, "height": 1080}
            )
            print(f"[완료] 저장: {OUTPUT_PATH}")
            size_kb = OUTPUT_PATH.stat().st_size / 1024
            print(f"       파일 크기: {size_kb:.0f} KB")

        finally:
            browser.close()


if __name__ == "__main__":
    main()
