"""Clean old output, re-submit, get REAL GPU timing."""
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, timeout=60):
    chan = c.get_transport().open_session()
    chan.settimeout(timeout)
    chan.exec_command('/bin/bash -l -c ' + "'" + cmd.replace("'", "'\\''") + "'")
    out = b""
    while True:
        try:
            chunk = chan.recv(65536)
            if not chunk: break
            out += chunk
        except: break
    chan.close()
    return out.decode(errors='replace').strip()

# Delete old output
print("Cleaning old output...")
sh('rm -f ~/ComfyUI/output/ZImageTurbo_GPU*.png')

# Truncate log to see fresh output only
sh('truncate -s 0 /tmp/comfyui.log; sleep 1')

# Submit fresh workflow
workflow = {
    "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": 123, "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(workflow))

print("Submitting fresh workflow (seed=123)...")
resp = sh('curl -s -X POST http://127.0.0.1:8188/prompt -H "Content-Type: application/json" -d @/tmp/wf.json')
print(f"  {resp[:120]}")

t0 = time.time()
print("\nMonitoring (NORMAL_VRAM = real GPU compute)...")

for i in range(200):
    elapsed = int(time.time() - t0)
    
    gpu_temp = sh('cat /sys/class/drm/card0/device/hwmon/hwmon*/temp1_input 2>/dev/null', timeout=5)
    temp_c = int(gpu_temp) // 1000 if gpu_temp.isdigit() else '?'
    
    try:
        with sftp.open('/tmp/comfyui.log', 'r') as f:
            log = f.read().decode(errors='replace')
    except: log = ''
    
    sampling = ''
    last = ''
    for line in log.split('\n'):
        s = line.strip()
        if '/8' in s and ('it/s' in s or 's/it' in s):
            sampling = s
        if s and 'FETCH' not in s and 'startup tasks' not in s and 'DEPRECATION' not in s:
            last = s
    
    display = sampling if sampling else last[-100:]
    print(f"  [{elapsed:>4}s] {temp_c}C | {display}")
    
    imgs = sh('ls ~/ComfyUI/output/ZImageTurbo_GPU*.png 2>/dev/null', timeout=5)
    if imgs:
        exec_time = ''
        for line in log.split('\n'):
            if 'Prompt executed in' in line:
                exec_time = line.strip()
        print(f"\n  *** IMAGE GENERATED! ***")
        print(f"  File: {imgs}")
        print(f"  {exec_time}")
        print(f"  Wall time: {elapsed}s")
        for line in log.split('\n'):
            s = line.strip()
            if any(k in s for k in ['loaded completely', 'loaded partially', '/8', 'Prompt executed']):
                print(f"  {s}")
        break
    
    q = sh('curl -s http://127.0.0.1:8188/queue 2>/dev/null', timeout=5)
    try:
        qd = json.loads(q)
        if not qd.get('queue_running') and not qd.get('queue_pending') and elapsed > 20:
            time.sleep(2)
            imgs = sh('ls ~/ComfyUI/output/ZImageTurbo_GPU*.png 2>/dev/null', timeout=5)
            if imgs:
                print(f"\n  *** IMAGE: {imgs} ***")
            else:
                print(f"\n  Queue empty, no image:")
                for line in log.split('\n')[-20:]:
                    if line.strip(): print(f"  {line.strip()}")
            break
    except: pass
    
    alive = sh('pgrep -f "python3.*main.py" >/dev/null && echo Y || echo N', timeout=5)
    if alive == 'N':
        print(f"\n  CRASHED!")
        for line in log.split('\n')[-25:]:
            if line.strip(): print(f"  {line.strip()}")
        break
    
    time.sleep(10)

sftp.close()
c.close()
print("\nDone.")
