#!/bin/bash
# Generate mockups for designs using ImageMagick compositing
#
# This is a basic local mockup generator for quick previews.
# For production mockups, use the POD APIs (Printful has great mockup generation).
#
# Usage: ./scripts/generate-mockups.sh <design-path> [template]
# Example: ./scripts/generate-mockups.sh stickers/spore-spiral sticker-3x3

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_DIR="$(dirname "$SCRIPT_DIR")"
DESIGNS_DIR="$PROJECT_DIR/designs"
TEMPLATES_DIR="$PROJECT_DIR/templates/mockups"

DESIGN_PATH=${1:-}
TEMPLATE=${2:-}

if [ -z "$DESIGN_PATH" ]; then
    echo "Usage: $0 <design-path> [template]"
    echo "Example: $0 stickers/spore-spiral sticker-3x3"
    exit 1
fi

# Check for ImageMagick
if ! command -v convert &> /dev/null; then
    echo "Error: ImageMagick not found. Please install ImageMagick."
    echo "Ubuntu: sudo apt install imagemagick"
    exit 1
fi

DESIGN_DIR="$DESIGNS_DIR/$DESIGN_PATH"
EXPORT_DIR="$DESIGN_DIR/exports/300dpi"
MOCKUP_DIR="$DESIGN_DIR/exports/mockups"

# Find the exported PNG
DESIGN_PNG=$(find "$EXPORT_DIR" -name "*.png" -type f | head -1)

if [ -z "$DESIGN_PNG" ]; then
    echo "Error: No exported PNG found in $EXPORT_DIR"
    echo "Run 'mycopunk design export $DESIGN_PATH' first"
    exit 1
fi

echo "🍄 Generating mockups for: $DESIGN_PATH"
echo "Using: $(basename "$DESIGN_PNG")"
echo ""

mkdir -p "$MOCKUP_DIR"

# Simple sticker mockup - add drop shadow and slight rotation
echo "Creating sticker mockup..."
convert "$DESIGN_PNG" \
    \( +clone -background black -shadow 60x5+5+5 \) \
    +swap -background white -layers merge +repage \
    -rotate -5 \
    "$MOCKUP_DIR/sticker-mockup.png"
echo "  ✓ sticker-mockup.png"

# T-shirt mockup placeholder
# In production, use Printful's mockup API for photorealistic results
echo "Creating simple t-shirt preview..."
convert -size 500x600 xc:'#2a2a2a' \
    "$DESIGN_PNG" -resize 200x200 -gravity center -composite \
    -gravity north -fill white -pointsize 14 -annotate +0+10 "T-SHIRT PREVIEW" \
    "$MOCKUP_DIR/tshirt-preview.png"
echo "  ✓ tshirt-preview.png"

echo ""
echo "Mockups saved to: $MOCKUP_DIR"
echo ""
echo "Note: For production-quality mockups, use the POD provider APIs:"
echo "  mycopunk mockup generate $DESIGN_PATH --provider=printful"
