#!/bin/bash
# NEONCYBER - Pixel Sort / Data Glitch Variations
# Based on wp_2026-03-08_v6 aesthetic
# Usage: ./generate-pixel-glitch.sh

set -e

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

echo "🎨 NEONCYBER - Pixel Sort Glitch Series"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "Based on: wp_2026-03-08_v6 (pixel sorted alley)"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""

PROMPTS=(
    # 1: Vertical pixel sort street
    "Cyberpunk street with aggressive vertical pixel sorting glitch, anime aesthetic. Neon signs streak downward in magenta and cyan vertical columns. Reality sorted by brightness creating abstract glitch patterns. Buildings dissolve into vertical data streams. Wet pavement reflects sorted pixel columns. Empty atmosphere with maximum digital corruption. CGA color palette - magenta, cyan, black. Glitch art meets cyberpunk. 1920x1080 widescreen, data moshing masterpiece quality."

    # 2: Horizontal RGB shift alley
    "Narrow alley with extreme horizontal RGB color separation, minimal anime style. Every neon sign splits into red/green/blue offset copies. Reality sliding apart chromatic aberration. Stairs descend through color-separated fog. Wet surfaces triple the RGB ghost images. Purple base with cyan and magenta displacement. Empty glitch atmosphere. Digital corruption aesthetic. 1920x1080 widescreen, chromatic aberration quality."

    # 3: Databending convenience store
    "24-hour convenience store with databending corruption, cyberpunk glitch aesthetic. Store signage becomes abstract barcode patterns. Vending machines render as vertical scan line artifacts. Reality compressed and corrupted like JPEG artifacts. Magenta and cyan block compression errors. Wet floor reflects glitched data patterns. Empty digital decay. 1920x1080 widescreen, corrupted file masterpiece."

    # 4: Asemic pixel text cityscape
    "Cyberpunk city where all neon signs become asemic corrupted pixel text, anime aesthetic. Kanji and characters melt into abstract glitch symbols. Vertical text flows downward like digital waterfalls. CGA palette magenta and cyan letterforms corrupt and merge. Wet street reflects unreadable data. Empty mysterious atmosphere. Pixel sorting meets typography corruption. 1920x1080 widescreen, asemic glitch quality."

    # 5: Displacement map alley
    "Narrow alley warped by displacement map glitch, minimal cyberpunk style. Neon signs stretch and compress following invisible wave patterns. Reality bends like digital fabric under stress. Stair geometry distorts creating impossible perspectives. Magenta and cyan neon warps and flows. Wet surfaces mirror the displacement. Empty surreal atmosphere. 1920x1080 widescreen, displacement glitch masterpiece."

    # 6: Scan line cascade
    "Cyberpunk street disintegrating into cascading scan lines, anime glitch aesthetic. Upper portion stable, lower portion dissolves into horizontal bars of color. Neon signs fragment into stacked stripes - magenta, cyan, purple. CRT failure meets digital decay. Wet pavement becomes abstract color bands. Empty scene falling apart beautifully. 1920x1080 widescreen, scan line corruption quality."
)

DESCRIPTIONS=(
    "Vertical Pixel Sort"
    "Horizontal RGB Shift"
    "Databending Store"
    "Asemic Pixel Text"
    "Displacement Warp"
    "Scan Line Cascade"
)

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, clean, normal", "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_glitch_${TIMESTAMP}_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 pixel glitch variations queued!"
echo ""
echo "📁 Files: ComfyUI/output/wp/"
echo "  wp_glitch_${TIMESTAMP}_v1_*.png - Vertical Pixel Sort"
echo "  wp_glitch_${TIMESTAMP}_v2_*.png - Horizontal RGB Shift"
echo "  wp_glitch_${TIMESTAMP}_v3_*.png - Databending Store"
echo "  wp_glitch_${TIMESTAMP}_v4_*.png - Asemic Pixel Text"
echo "  wp_glitch_${TIMESTAMP}_v5_*.png - Displacement Warp"
echo "  wp_glitch_${TIMESTAMP}_v6_*.png - Scan Line Cascade"
echo ""
echo "🎨 More pixel sorting madness incoming!"
