#!/bin/bash
# IMPOSSIBLE-GEO Batch - CGA palettes + impossible geometry
# 6 variants across core geometries and both CGA palettes
# Usage: ./generate-batch.sh

set -e

COMFYUI="http://192.168.1.57:8188"
DATE=$(date +%Y%m%d)
TIMESTAMP=$(date +%H%M%S)

echo "IMPOSSIBLE-GEO Batch"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "CGA palettes + impossible geometry, 8 steps, 1920x1080"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""

PROMPTS=(
    # 1: Penrose triangle — CGA cyan/magenta/white/black
    "A Penrose impossible triangle rendered in CGA graphics aesthetic, cyan magenta white black palette. Pure impossible geometry with clean sharp edges, the triangle loops back on itself defying perspective. Mathematical precision, retro computing aesthetic, empty void background in pure black. Minimal atmospheric composition with crisp geometric lines. The impossible structure floats in digital emptiness, each face rendered in flat CGA color blocks. 1920x1080 widescreen, CGA masterpiece quality."

    # 2: Escher staircase — CGA red/cyan/white/black
    "An Escher impossible staircase rendered in CGA red cyan white black palette. Stairs ascend and descend simultaneously in an infinite loop, clean geometric lines with retro pixel precision. Mathematical impossibility in pure form, no texture, flat CGA colors. The structure occupies infinite void, each step a crisp rectangular form. Retro computing aesthetic, impossible architecture, empty background. 1920x1080 widescreen, CGA masterpiece quality."

    # 3: Tesseract unfolding — CGA cyan/magenta/white/black
    "A tesseract hypercube unfolding through 4D space rendered in CGA cyan magenta white black palette. Clean geometric cross-shaped unfolding pattern with inner cubes visible through outer faces, retro computing aesthetic. Mathematical precision, crisp edges, flat CGA color blocks. Pure geometric form in black void, each face clearly defined in distinct CGA colors. Angular dimensional structure. 1920x1080 widescreen, CGA masterpiece quality."

    # 4: Klein bottle — CGA green/black/white
    "A Klein bottle impossible surface rendered in CGA green black white palette. Tube curves back through itself with no inside or outside, pure mathematical topology. Clean smooth geometric form with crisp CGA outlines, retro computing aesthetic. The impossible surface in pure form, minimal atmospheric composition. Green outlines against black void, white highlights defining curvature. 1920x1080 widescreen, CGA masterpiece quality."

    # 5: Möbius strip — CGA cyan/magenta/white/black
    "A Möbius strip single-sided surface rendered in CGA cyan magenta white black palette. Twisted loop with one continuous surface, mathematical elegance in retro pixel form. Clean sharp edges, flat CGA color blocks, crisp geometric lines. The strip rotates through empty black void, each face alternating CGA colors. Pure topology, no clutter, desktop-friendly composition. 1920x1080 widescreen, CGA masterpiece quality."

    # 6: Hypercube projection — CGA red/cyan/white/black
    "A hypercube 4D projection wireframe rendered in CGA red cyan white black palette. Inner and outer cubes connected by dimensional edges, clean geometric precision, retro computing aesthetic. Mathematical structure in flat CGA colors, crisp lines against black void. Angular dimensional form with clear edge definition. Empty void, pure geometric form, minimal composition. 1920x1080 widescreen, CGA masterpiece quality."
)

DESCRIPTIONS=(
    "Penrose Triangle"
    "Escher Staircase"
    "Tesseract Unfolding"
    "Klein Bottle"
    "Möbius Strip"
    "Hypercube Projection"
)

for i in {0..5}; do
    NUM=$((i + 1))
    PROMPT="${PROMPTS[$i]}"
    DESC="${DESCRIPTIONS[$i]}"

    echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
    echo "[$NUM/6] $DESC"
    echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"

    WORKFLOW=$(cat <<EOF
{
  "prompt": {
    "1": {"inputs": {"unet_name": "z_image_turbo_bf16.safetensors", "weight_dtype": "default"}, "class_type": "UNETLoader"},
    "2": {"inputs": {"clip_name": "qwen_3_4b.safetensors", "type": "qwen_image"}, "class_type": "CLIPLoader"},
    "3": {"inputs": {"vae_name": "ae.safetensors"}, "class_type": "VAELoader"},
    "4": {"inputs": {"text": "$PROMPT", "clip": ["2", 0]}, "class_type": "CLIPTextEncode"},
    "5": {"inputs": {"text": "low quality, blurry, organic, characters, texture, photo realistic", "clip": ["2", 0]}, "class_type": "CLIPTextEncode"},
    "6": {"inputs": {"width": 1920, "height": 1080, "batch_size": 1}, "class_type": "EmptyLatentImage"},
    "7": {"inputs": {"seed": $(python3 -c "import random; print(random.randint(1, 2**32-1))"), "steps": 8, "cfg": 1.0, "sampler_name": "euler", "scheduler": "sgm_uniform", "denoise": 1, "model": ["1", 0], "positive": ["4", 0], "negative": ["5", 0], "latent_image": ["6", 0]}, "class_type": "KSampler"},
    "8": {"inputs": {"samples": ["7", 0], "vae": ["3", 0]}, "class_type": "VAEDecode"},
    "9": {"inputs": {"filename_prefix": "wp/geo_${DATE}_v${NUM}", "images": ["8", 0]}, "class_type": "SaveImage"}
  }
}
EOF
)

    RESPONSE=$(curl -s -X POST "$COMFYUI/prompt" -H "Content-Type: application/json" -d "$WORKFLOW")
    PROMPT_ID=$(echo "$RESPONSE" | python3 -c "import sys, json; print(json.load(sys.stdin).get('prompt_id', ''))" 2>/dev/null || echo "")

    if [ -n "$PROMPT_ID" ]; then
        echo "Queued: $PROMPT_ID"
    else
        echo "Failed to queue"
        echo "$RESPONSE"
    fi
    echo ""
done

echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "All 6 impossible-geo variants queued!"
echo ""
echo "Files: ComfyUI/output/wp/"
echo "  geo_${DATE}_v1_*.png - Penrose Triangle"
echo "  geo_${DATE}_v2_*.png - Escher Staircase"
echo "  geo_${DATE}_v3_*.png - Tesseract Unfolding"
echo "  geo_${DATE}_v4_*.png - Klein Bottle"
echo "  geo_${DATE}_v5_*.png - Möbius Strip"
echo "  geo_${DATE}_v6_*.png - Hypercube Projection"
