#!/bin/bash
# Find the GGUF file and check its metadata
echo "=== Finding GGUF file ==="
find /home/fabian/ComfyUI/models -name '*.gguf' 2>/dev/null

echo ""
echo "=== GGUF metadata ==="
GGUF_FILE=$(find /home/fabian/ComfyUI/models -name 'z_image_turbo*' 2>/dev/null | head -1)
echo "Found: $GGUF_FILE"

if [ -n "$GGUF_FILE" ]; then
    /home/fabian/ComfyUI/venv/bin/python3 -c "
import gguf
reader = gguf.GGUFReader('$GGUF_FILE')
print('GGUF fields:')
for key in sorted(reader.fields.keys()):
    field = reader.fields[key]
    # Show field type and value if small
    parts = field.parts
    tp = str(field.types)
    if hasattr(field, 'data') and len(field.data) < 100:
        try:
            val = list(parts[field.data[0]])[:5] if len(field.data) > 0 else 'empty'
        except:
            val = '?'
    else:
        val = f'data_len={len(field.data) if hasattr(field, \"data\") else \"?\"}'
    print(f'  {key}: types={tp} val={val}')
"
fi

echo ""
echo "=== Check supported_models for z_image ==="
grep -n 'z_image\|ZImage\|z-image\|Z_IMAGE' /home/fabian/ComfyUI/comfy/supported_models.py | head -20

echo ""
echo "=== Check what model arch the GGUF uses ==="
if [ -n "$GGUF_FILE" ]; then
    /home/fabian/ComfyUI/venv/bin/python3 -c "
import gguf
reader = gguf.GGUFReader('$GGUF_FILE')
# Get architecture-related fields
for key in reader.fields:
    if 'arch' in key or 'model' in key or 'type' in key or 'name' in key:
        field = reader.fields[key]
        try:
            parts = field.parts
            data_indices = field.data
            if len(data_indices) > 0:
                raw = parts[data_indices[0]]
                if hasattr(raw, 'tobytes'):
                    val = raw.tobytes().decode('utf-8', errors='replace')
                else:
                    val = str(raw)
            else:
                val = '<empty>'
        except:
            val = '<error>'
        print(f'  {key} = {val}')
"
fi
