#!/bin/bash
# NEONCYBER Batch Generator v2 - Glitchy CGA Weirdness
# Saves to output/wp/ folder for wallpaper engine
# Usage: ./generate-batch-v2.sh

set -e

COMFYUI="http://192.168.1.57:8188"
DATE=$(date +%Y-%m-%d)

echo "🎨 NEONCYBER Batch v2 - Glitchy CGA Weirdness"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "Aesthetic: More crazy, weird, glitch, CGA vibes"
echo "Output: ComfyUI output/wp/ folder (wp_ prefix)"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""

# 6 variations with MORE weirdness
PROMPTS=(
    # Variation 1: Glitchy alley stairs (building on #2)
    "Narrow cyberpunk alley with wet stairs descending into purple digital glitch fog, anime aesthetic with CGA color aberration. Neon signs flicker and corrupt with RGB shift - pink ramen left, cyan kanji right. Digital scan lines across scene, VHS tracking errors. Chromatic aberration on edges. Empty atmospheric solitude with slight reality distortion. Purple and magenta color bleed, pixelated dithering effects. Rain creates glitchy bokeh. Minimal composition with digital artifacts. Retro computer graphics meets cyberpunk noir. 1920x1080 widescreen, glitch aesthetic masterpiece."

    # Variation 2: CGA dithered street
    "Empty cyberpunk street rendered in CGA-inspired 4-color dithering, anime style. Magenta, cyan, black, white palette with heavy crosshatch patterns. Vending machines glow with CGA color limitations. Wet pavement shows stipple pattern reflections. Digital grid overlay barely visible. Retro computer aesthetic meets neon noir. Geometric simplification, harsh contrast. Lo-fi pixel noise, scan line artifacts. Atmospheric depth through dithering density. 1920x1080 widescreen, retro digital masterpiece."

    # Variation 3: Glitch convenience store
    "24-hour convenience store with reality glitching at edges, cyberpunk anime aesthetic. Store interior bleeds purple and cyan light but edges fragment into digital noise. RGB color separation on signage. Corrupted pixels around doorframe. Vending machines phase between solid and wireframe. Wet pavement reflects distorted neon with chromatic aberration. Empty street with digital artifacts floating. VHS tracking errors, CRT phosphor glow. Minimal scene with maximum glitch weirdness. 1920x1080 widescreen, digital corruption quality."

    # Variation 4: Wireframe holographic city
    "Cyberpunk city transitioning between solid and wireframe hologram, minimal anime style. Buildings flicker between rendered purple neon and cyan wireframe geometry. CGA-style geometric primitives visible. Digital mesh grid overlay. Reality fragments showing code underneath. Rain renders as particle system glitches. Empty streets with holographic distortion. Magenta and cyan neon with black void. Retro 3D computer graphics aesthetic. 1920x1080 widescreen, digital liminal quality."

    # Variation 5: Corrupted train platform
    "Train platform with heavy digital corruption and glitch artifacts, anime aesthetic. Purple neon train phases in from static noise. Platform floor shows CGA dithering patterns and missing texture tiles. Warning lights stutter and duplicate. Cyan digital signs corrupt with missing characters. Chromatic aberration, scan lines, RGB shift. Empty atmosphere with reality breaking down. Retro computer errors as aesthetic. 1920x1080 widescreen, corrupted data masterpiece."

    # Variation 6: Pixel sorted alley
    "Cyberpunk alley with pixel sorting glitch effect, minimal anime style. Vertical neon signs streak downward in magenta and cyan pixel columns. Reality sorted by brightness creating abstract patterns. CGA color palette corruption. Wet ground reflects sorted pixel data. Atmospheric fog renders as noise bands. Empty scene with maximum digital weirdness. Glitch art meets cyberpunk noir. 1920x1080 widescreen, pixel glitch quality."
)

DESCRIPTIONS=(
    "Glitchy Alley Stairs"
    "CGA Dithered Street"
    "Glitch Convenience Store"
    "Wireframe City"
    "Corrupted Train Platform"
    "Pixel Sorted Alley"
)

# Generate all 6
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, people, photo realistic, 3d render", "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/wp_${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"
    fi

    echo ""
done

echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "✨ All 6 glitchy variations queued!"
echo "⏳ Total generation time: ~6 minutes"
echo ""
echo "📁 Files saving to: ComfyUI/output/wp/"
echo "  wp_${DATE}_v1_*.png - Glitchy Alley Stairs"
echo "  wp_${DATE}_v2_*.png - CGA Dithered Street"
echo "  wp_${DATE}_v3_*.png - Glitch Convenience Store"
echo "  wp_${DATE}_v4_*.png - Wireframe City"
echo "  wp_${DATE}_v5_*.png - Corrupted Train Platform"
echo "  wp_${DATE}_v6_*.png - Pixel Sorted Alley"
echo ""
echo "🎨 Glitchy CGA weirdness incoming!"
