#!/usr/bin/env python3
"""Generate Play Store screenshots for Roomfit Studio.

Creates mockup screenshots for:
- Phone (1080x1920 portrait)
- 7-inch tablet (1536x2048 portrait, 3:4 ratio)
- 10-inch tablet (1600x2560 portrait, 5:8 ratio)

Each mockup shows a clean Studio-style UI card with a title and key feature bullets.
Real device screenshots should replace these in production.
"""
from PIL import Image, ImageDraw, ImageFont
from pathlib import Path

OUT = Path(__file__).parent

BG_DARK = (15, 15, 30)
INDIGO = (74, 78, 255)
INDIGO_LIGHT = (130, 135, 255)
WHITE = (255, 255, 255)
GRAY = (180, 190, 210)
CARD_BG = (28, 30, 52)

KOREAN_FONTS = [
    "/System/Library/Fonts/AppleSDGothicNeo.ttc",
    "/System/Library/Fonts/Supplemental/AppleGothic.ttf",
]
LATIN_FONTS = [
    "/System/Library/Fonts/HelveticaNeue.ttc",
    "/System/Library/Fonts/Helvetica.ttc",
]

def load_font(size, korean=False):
    paths = KOREAN_FONTS if korean else LATIN_FONTS
    for p in paths:
        try:
            return ImageFont.truetype(p, size=size, index=0)
        except OSError:
            continue
    return ImageFont.load_default()


def make_gradient(w, h):
    img = Image.new("RGB", (w, h), BG_DARK)
    draw = ImageDraw.Draw(img)
    for y in range(h):
        t = y / h
        r = int(15 + (35 - 15) * t)
        g = int(15 + (38 - 15) * t)
        b = int(30 + (75 - 30) * t)
        draw.line([(0, y), (w, y)], fill=(r, g, b))
    return img


def rounded_rect(draw, xy, radius, fill, outline=None, width=1):
    x0, y0, x1, y1 = xy
    draw.rounded_rectangle((x0, y0, x1, y1), radius=radius, fill=fill, outline=outline, width=width)


def draw_title_block(draw, w, y_start, title, subtitle, font_title, font_sub):
    bbox = draw.textbbox((0, 0), title, font=font_title)
    tw = bbox[2] - bbox[0]
    draw.text(((w - tw) // 2 - bbox[0], y_start - bbox[1]), title, fill=WHITE, font=font_title)
    sy = y_start + (bbox[3] - bbox[1]) + int(font_title.size * 0.4)
    bbox2 = draw.textbbox((0, 0), subtitle, font=font_sub)
    sw = bbox2[2] - bbox2[0]
    draw.text(((w - sw) // 2 - bbox2[0], sy - bbox2[1]), subtitle, fill=INDIGO_LIGHT, font=font_sub)
    return sy + (bbox2[3] - bbox2[1]) + int(font_sub.size * 1.0)


def draw_feature_card(draw, xy, title, lines, font_title, font_line):
    x0, y0, x1, y1 = xy
    rounded_rect(draw, (x0, y0, x1, y1), radius=28, fill=CARD_BG)
    pad = 40
    # Accent bar
    draw.rectangle((x0 + pad, y0 + pad, x0 + pad + 6, y0 + pad + 60), fill=INDIGO)
    # Title
    draw.text((x0 + pad + 24, y0 + pad), title, fill=WHITE, font=font_title)
    # Lines
    ly = y0 + pad + int(font_title.size * 1.9)
    for line in lines:
        draw.text((x0 + pad, ly), line, fill=GRAY, font=font_line)
        ly += int(font_line.size * 1.65)


SCREENS = [
    {
        "title": "회원 관리",
        "subtitle": "한 화면에서 회원권·출석·세션",
        "cards": [
            ("회원 목록", ["전체 42명", "활성 멤버십 38명", "만료 예정 4명"]),
            ("멤버십 현황", ["이번 주 만료 2건", "재등록 필요 3건", "오늘 체크인 18명"]),
        ],
    },
    {
        "title": "세션 & 출석",
        "subtitle": "PT 스케줄과 출석을 한번에",
        "cards": [
            ("오늘의 PT 세션", ["10:00 김영훈 / 박트레이너", "14:00 이지현 / 최트레이너", "19:00 정우진 / 박트레이너"]),
            ("자동 출석 체크", ["QR 체크인 24건", "노쇼 알림 자동 발송", "주간 출석률 92%"]),
        ],
    },
    {
        "title": "실시간 VBT",
        "subtitle": "Velocity-Based Training 데이터",
        "cards": [
            ("라이브 세트 추적", ["평균 속도 0.78 m/s", "피크 파워 685 W", "총 운동량 3,420 kg"]),
            ("회원별 성장 분석", ["MCV 기반 1RM 예측", "Concentric/Eccentric 구간", "주간 리포트 자동 생성"]),
        ],
    },
]


def draw_screen(size, data):
    w, h = size
    img = make_gradient(w, h)
    draw = ImageDraw.Draw(img)

    # Scale fonts based on width
    scale = w / 1080
    f_title = load_font(int(96 * scale), korean=True)
    f_sub = load_font(int(40 * scale), korean=True)
    f_card_title = load_font(int(52 * scale), korean=True)
    f_card_line = load_font(int(36 * scale), korean=True)

    # Header area
    title_y = int(180 * scale)
    content_y = draw_title_block(draw, w, title_y, data["title"], data["subtitle"], f_title, f_sub)

    # Feature cards
    margin = int(80 * scale)
    card_height = int(380 * scale)
    gap = int(60 * scale)
    card_x0 = margin
    card_x1 = w - margin
    cy = content_y + int(40 * scale)
    for t, lines in data["cards"]:
        draw_feature_card(
            draw,
            (card_x0, cy, card_x1, cy + card_height),
            t,
            lines,
            f_card_title,
            f_card_line,
        )
        cy += card_height + gap

    # Footer brand
    foot_font = load_font(int(32 * scale), korean=False)
    brand = "Roomfit Studio"
    bbox = draw.textbbox((0, 0), brand, font=foot_font)
    draw.text(((w - (bbox[2] - bbox[0])) // 2 - bbox[0], h - int(80 * scale) - bbox[1]), brand, fill=INDIGO_LIGHT, font=foot_font)
    return img


SIZES = {
    "phone": (1080, 1920),
    "tablet7": (1536, 2048),
    "tablet10": (1600, 2560),
}

def main():
    for device, size in SIZES.items():
        for i, data in enumerate(SCREENS, 1):
            img = draw_screen(size, data)
            out = OUT / f"screenshot_{device}_{i}.png"
            img.save(out, "PNG", optimize=True)
            print(f"Saved: {out} ({size[0]}x{size[1]})")


if __name__ == "__main__":
    main()
