#!/usr/bin/env bash

set -e

if ! command -v magick &> /dev/null; then
  echo "ImageMagick (magick) is required. Install it first."
  exit 1
fi

mkdir -p bg blog

# Final exact dimensions (not just width anymore)
BG_WIDTH=1920
BG_HEIGHT=1080     # Hero (16:9)

BLOG_WIDTH=800
BLOG_HEIGHT=600    # 3-column cards (4:3 is a safe default)

# Quality
BG_QUALITY=82
BLOG_QUALITY=75

echo "Converting JPG → WebP with fixed dimensions + center crop..."

for img in *.jpg *.jpeg; do
  [ -e "$img" ] || continue

  filename=$(basename "$img")
  name="${filename%.*}"

  echo "→ Processing $img"

  # HERO / BG (cover + center crop)
  magick "$img" \
    -resize ${BG_WIDTH}x${BG_HEIGHT}^ \
    -gravity center \
    -extent ${BG_WIDTH}x${BG_HEIGHT} \
    -strip \
    -quality $BG_QUALITY \
    -define webp:method=6 \
    -define webp:autofilter=true \
    -define webp:thread-level=1 \
    -colorspace sRGB \
    "bg/${name}.webp"

  # BLOG (cover + center crop)
  magick "$img" \
    -resize ${BLOG_WIDTH}x${BLOG_HEIGHT}^ \
    -gravity center \
    -extent ${BLOG_WIDTH}x${BLOG_HEIGHT} \
    -strip \
    -quality $BLOG_QUALITY \
    -define webp:method=6 \
    -define webp:autofilter=true \
    -define webp:thread-level=1 \
    -colorspace sRGB \
    "blog/${name}.webp"

done

echo "Done! Images cropped + standardized in ./bg and ./blog"
