"""
photo_mockups.py

Lets a client upload their OWN photographed blank mockup (a real tee, van,
spray bottle, etc.) instead of relying only on the flat-vector library.
Since there's no manual zone-marking step in this flow, each category has
a sensible default placement zone expressed as fractions of the image's
own dimensions -- resolved to real pixel coordinates once we know the
uploaded photo's actual size. Good enough for most product photography
shot reasonably straight-on; not a substitute for the zone-definition tool
when a mockup needs precise, photographer-specific placement.
"""

from pathlib import Path
from PIL import Image

from place_logo import place_rect_zone

# category -> (mockup_id this overwrites/creates, application-page type,
#              placement box as (x0,y0,x1,y1) fractions of image size,
#              which logo variant to use, blend mode, target width % of box)
CATEGORY_CONFIG = {
    "tee":            {"mockup_id": "tshirt-front",   "type": "uniform",    "box": (0.35, 0.28, 0.65, 0.48), "variant": "full-lockup", "blend": "multiply", "width_pct": 85},
    "polo":           {"mockup_id": "polo-front",     "type": "uniform",    "box": (0.28, 0.24, 0.55, 0.40), "variant": "full-lockup", "blend": "multiply", "width_pct": 85},
    "cap":            {"mockup_id": "cap-front",      "type": "uniform",    "box": (0.35, 0.30, 0.65, 0.50), "variant": "mark-only",   "blend": "multiply", "width_pct": 80},
    "sleeve":         {"mockup_id": "sleeve-mark",    "type": "uniform",    "box": (0.30, 0.30, 0.70, 0.70), "variant": "mark-only",   "blend": "multiply", "width_pct": 75},
    "van":            {"mockup_id": "van-side",       "type": "vehicle",    "box": (0.28, 0.32, 0.62, 0.58), "variant": "full-lockup", "blend": "normal",   "width_pct": 90},
    "sedan":          {"mockup_id": "sedan-side",     "type": "vehicle",    "box": (0.28, 0.32, 0.62, 0.58), "variant": "full-lockup", "blend": "normal",   "width_pct": 90},
    "letterhead":     {"mockup_id": "letterhead",     "type": "stationery", "box": (0.08, 0.06, 0.35, 0.16), "variant": "full-lockup", "blend": "normal",   "width_pct": 95},
    "business-card":  {"mockup_id": "business-card",  "type": "stationery", "box": (0.08, 0.10, 0.55, 0.42), "variant": "full-lockup", "blend": "normal",   "width_pct": 95},
    "envelope":       {"mockup_id": "envelope",       "type": "stationery", "box": (0.08, 0.35, 0.35, 0.55), "variant": "full-lockup", "blend": "normal",   "width_pct": 95},
    "social-avatar":  {"mockup_id": "social-avatar",  "type": "digital",    "box": (0.25, 0.25, 0.75, 0.75), "variant": "mark-only",   "blend": "normal",   "width_pct": 85},
    "social-banner":  {"mockup_id": "social-banner",  "type": "digital",    "box": (0.06, 0.25, 0.38, 0.65), "variant": "full-lockup", "blend": "normal",   "width_pct": 90},
    "spray-bottle":   {"mockup_id": "spray-bottle",   "type": "packaging",  "box": (0.25, 0.10, 0.75, 0.35), "variant": "full-lockup", "blend": "normal",   "width_pct": 90},
}


def composite_photo_mockup(photo_path, category, logo_full_path, logo_mark_path, out_path):
    """
    Places the client's logo onto their uploaded photo, using the default
    zone for this category. Returns the mockup_id this result should be
    filed under (so it overwrites the matching library-generated version
    when one exists, e.g. a real tee photo replaces the flat-vector one).
    """
    cfg = CATEGORY_CONFIG.get(category)
    if cfg is None:
        raise ValueError(f"Unknown mockup category '{category}'")

    canvas = Image.open(photo_path).convert("RGBA")
    W, H = canvas.size
    x0f, y0f, x1f, y1f = cfg["box"]
    zone = {
        "points": [[int(x0f * W), int(y0f * H)], [int(x1f * W), int(y0f * H)],
                   [int(x1f * W), int(y1f * H)], [int(x0f * W), int(y1f * H)]],
        "max_logo_width_pct": cfg["width_pct"],
        "blend": cfg["blend"],
    }

    logo_path = logo_mark_path if (cfg["variant"] == "mark-only" and logo_mark_path) else logo_full_path
    logo = Image.open(logo_path).convert("RGBA")

    place_rect_zone(canvas, logo, zone)
    canvas.save(out_path)

    return cfg["mockup_id"], cfg["type"]
