"""FAST FIX: patch offload devices, restart. No fluff."""
import paramiko, time, json

k = paramiko.Ed25519Key.from_private_key_file(r'C:\Users\fabia\.ssh\id_ed25519')
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect('192.168.178.150', username='fabian', pkey=k, timeout=10)
sftp = c.open_sftp()

def sh(cmd, t=30):
    ch = c.get_transport().open_session()
    ch.settimeout(t)
    ch.exec_command('/bin/bash -l -c ' + "'" + cmd.replace("'", "'\\''") + "'")
    o = b""
    while True:
        try:
            d = ch.recv(65536)
            if not d: break
            o += d
        except: break
    ch.close()
    return o.decode(errors='replace').strip()

# 1. KILL
sh('pkill -9 -f "python3.*main.py" 2>/dev/null; sleep 1')
print("Killed")

# 2. READ
with sftp.open('/home/fabian/ComfyUI/comfy/model_management.py', 'r') as f:
    code = f.read().decode()

# 3. PATCH: unet_offload_device - return GPU for SHARED too
# Find the function and add SHARED check
changed = False

# Patch unet_offload_device: "HIGH_VRAM" -> "HIGH_VRAM or SHARED"
if 'def unet_offload_device' in code:
    lines = code.split('\n')
    for i, line in enumerate(lines):
        if 'def unet_offload_device' in line:
            # Look at next few lines for the HIGH_VRAM check
            for j in range(i, min(i+8, len(lines))):
                if 'HIGH_VRAM' in lines[j] and 'SHARED' not in lines[j] and 'unet_offload' not in lines[j]:
                    old = lines[j]
                    lines[j] = old.replace('VRAMState.HIGH_VRAM', 'VRAMState.HIGH_VRAM or vram_state == VRAMState.SHARED')
                    print(f"Patched unet_offload L{j+1}: {lines[j].strip()}")
                    changed = True
                    break
            break
    code = '\n'.join(lines)

# Patch vae_offload_device: "args.gpu_only" -> "args.gpu_only or SHARED"  
if 'def vae_offload_device' in code:
    lines = code.split('\n')
    for i, line in enumerate(lines):
        if 'def vae_offload_device' in line:
            for j in range(i, min(i+8, len(lines))):
                if 'gpu_only' in lines[j] and 'SHARED' not in lines[j]:
                    old = lines[j]
                    lines[j] = old.replace('args.gpu_only', '(args.gpu_only or vram_state == VRAMState.SHARED)')
                    print(f"Patched vae_offload L{j+1}: {lines[j].strip()}")
                    changed = True
                    break
            break
    code = '\n'.join(lines)

# Also patch text_encoder_offload_device if it offloads to CPU
if 'def text_encoder_offload_device' in code:
    lines = code.split('\n')
    for i, line in enumerate(lines):
        if 'def text_encoder_offload_device' in line:
            for j in range(i, min(i+8, len(lines))):
                if 'gpu_only' in lines[j] and 'SHARED' not in lines[j]:
                    old = lines[j]
                    lines[j] = old.replace('args.gpu_only', '(args.gpu_only or vram_state == VRAMState.SHARED)')
                    print(f"Patched text_enc_offload L{j+1}: {lines[j].strip()}")
                    changed = True
                    break
            break
    code = '\n'.join(lines)

if changed:
    sh('cp /home/fabian/ComfyUI/comfy/model_management.py /home/fabian/ComfyUI/comfy/model_management.py.bak3')
    with sftp.open('/home/fabian/ComfyUI/comfy/model_management.py', 'w') as f:
        f.write(code)
    print("Written!")
else:
    print("Already patched or structure changed")

# 4. RESTART
sh('rm -f /tmp/comfyui.log')
sh('nohup /tmp/run_comfyui.sh > /tmp/comfyui.log 2>&1 &')
time.sleep(4)
print(f"PID: {sh('pgrep -f python3.*main.py')}")

# 5. WAIT FOR READY
for i in range(60):
    r = sh('curl -s -o /dev/null -w "%{http_code}" http://127.0.0.1:8188/ 2>/dev/null', t=5)
    if '200' in r: print(f"Ready ({i*2}s)"); break
    time.sleep(2)

# Quick check
with sftp.open('/tmp/comfyui.log', 'r') as f:
    log = f.read().decode(errors='replace')
for l in log.split('\n'):
    s = l.strip()
    if any(x in s for x in ['vram state', 'SHARED', 'Device:']): print(f"  {s}")

# 6. SUBMIT
wf = {"prompt": {
    "1": {"class_type": "UnetLoaderGGUF", "inputs": {"unet_name": "z_image_turbo-Q5_K_S.gguf"}},
    "2": {"class_type": "CLIPLoaderGGUF", "inputs": {"clip_name": "Qwen3-4B.i1-Q5_K_S.gguf", "type": "qwen_image"}},
    "3": {"class_type": "VAELoader", "inputs": {"vae_name": "ae.safetensors"}},
    "4": {"class_type": "CLIPTextEncode", "inputs": {"text": "A red fox in a snowy forest, photorealistic", "clip": ["2", 0]}},
    "5": {"class_type": "EmptyLatentImage", "inputs": {"width": 512, "height": 512, "batch_size": 1}},
    "6": {"class_type": "KSampler", "inputs": {"model": ["1", 0], "positive": ["4", 0], "negative": ["4", 0],
        "latent_image": ["5", 0], "seed": 99999, "steps": 8, "cfg": 1.0,
        "sampler_name": "euler", "scheduler": "simple", "denoise": 1.0}},
    "7": {"class_type": "VAEDecode", "inputs": {"samples": ["6", 0], "vae": ["3", 0]}},
    "8": {"class_type": "SaveImage", "inputs": {"images": ["7", 0], "filename_prefix": "ZImageTurbo_GPU"}}
}}
with sftp.open('/tmp/wf.json', 'w') as f:
    f.write(json.dumps(wf))
resp = sh('curl -s -X POST http://127.0.0.1:8188/prompt -H "Content-Type: application/json" -d @/tmp/wf.json')
print(f"Submitted: {resp[:100]}")

# 7. MONITOR - compact, fast checks
print("\nWaiting for image...")
t0 = time.time()
last_shown = ''
for i in range(180):
    el = int(time.time() - t0)
    try:
        with sftp.open('/tmp/comfyui.log', 'r') as f:
            log = f.read().decode(errors='replace')
    except: log = ''
    
    # Find latest status
    status = ''
    for l in log.split('\n'):
        s = l.strip()
        if any(x in s for x in ['/8', 'loaded', 'Requested', 'VAE', 'Prompt executed', 'Error']):
            if 'FETCH' not in s: status = s
    
    if status != last_shown:
        gpu = sh('cat /sys/class/drm/card0/device/gpu_busy_percent 2>/dev/null', t=5)
        print(f"  [{el:>3}s] GPU:{gpu}% | {status[-100:]}")
        last_shown = status
    
    imgs = sh('ls ~/ComfyUI/output/ZImageTurbo_GPU*.png 2>/dev/null', t=5)
    if imgs:
        print(f"\n*** DONE in {el}s! ***")
        for l in log.split('\n'):
            s = l.strip()
            if any(x in s for x in ['load device', 'offload device', 'loaded completely', 'Prompt executed']):
                print(f"  {s}")
        break
    
    if sh('pgrep -f "python3.*main.py" >/dev/null && echo Y || echo N', t=5) == 'N':
        print(f"\nCRASHED at {el}s!")
        for l in log.split('\n')[-15:]:
            if l.strip(): print(f"  {l.strip()}")
        break
    
    time.sleep(3)

sftp.close()
c.close()
