#!/usr/bin/env python3
"""Fix workflow submission: write JSON to file, curl from file, monitor."""
import paramiko, json, time

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('192.168.178.150', username='fabian',
            key_filename=r'C:\Users\fabia\.ssh\id_ed25519', timeout=10)

def run(cmd, timeout=60):
    _, so, se = ssh.exec_command(cmd, timeout=timeout)
    return so.read().decode(), se.read().decode()

try:
    # Step 1: Write workflow JSON via SFTP (reliable, no shell escaping)
    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": "CLIPTextEncode",
                  "inputs": {"text": "", "clip": ["2", 0]}},
            "6": {"class_type": "EmptyLatentImage",
                  "inputs": {"width": 512, "height": 512, "batch_size": 1}},
            "7": {"class_type": "KSampler",
                  "inputs": {"model": ["1", 0], "seed": 42, "steps": 8, "cfg": 1.0,
                             "sampler_name": "euler", "scheduler": "simple",
                             "positive": ["4", 0], "negative": ["5", 0],
                             "latent_image": ["6", 0], "denoise": 1.0}},
            "8": {"class_type": "VAEDecode",
                  "inputs": {"samples": ["7", 0], "vae": ["3", 0]}},
            "9": {"class_type": "SaveImage",
                  "inputs": {"images": ["8", 0], "filename_prefix": "ZImageTurbo_test"}}
        }
    }

    sftp = ssh.open_sftp()
    with sftp.open('/tmp/wf.json', 'w') as f:
        f.write(json.dumps(workflow))
    sftp.close()
    print("Workflow JSON written via SFTP.")

    # Verify JSON is valid
    out, _ = run("python3 -c \"import json; d=json.load(open('/tmp/wf.json')); print('nodes:', sorted(d['prompt'].keys()))\"")
    print(f"Verify: {out.strip()}")

    # Step 2: Check startup script has --cpu-vae
    out, _ = run("cat /home/fabian/start_comfyui.sh")
    has_cpu_vae = '--cpu-vae' in out
    print(f"Startup has --cpu-vae: {has_cpu_vae}")
    print(f"Startup has --novram: {'--novram' in out}")
    print(f"Startup has --force-fp16: {'--force-fp16' in out}")

    # Step 3: Submit
    out, err = run("curl -s -X POST http://localhost:8188/prompt -H 'Content-Type: application/json' -d @/tmp/wf.json")
    print(f"\nSubmit response: {out.strip()[:500]}")

    try:
        resp = json.loads(out.strip())
    except:
        print(f"Failed to parse response!")
        raise SystemExit(1)

    if 'error' in resp:
        print(f"\nAPI ERROR: {resp['error']}")
        print(f"Details: {resp.get('details','')}")
        print(f"Node errors: {resp.get('node_errors',{})}")
        raise SystemExit(1)

    prompt_id = resp.get('prompt_id', 'unknown')
    print(f"Prompt ID: {prompt_id}")

    # Step 4: Monitor (15s intervals, up to 30 min)
    print("\nMonitoring generation (GPU sampling + CPU VAE)...")
    last_log = ""
    for i in range(120):
        time.sleep(15)

        stats, _ = run("bash -c 'PID=$(pgrep -f \"python3 main.py\" | head -1); "
            "if [ -n \"$PID\" ]; then "
            "  CPU=$(ps -p $PID -o %cpu --no-headers); "
            "  MEM=$(ps -p $PID -o rss --no-headers); "
            "  echo \"CPU:${CPU}% RSS:$((MEM/1024))M LOAD:$(cut -d\" \" -f1-3 /proc/loadavg)\"; "
            "else echo DEAD; fi'")

        log, _ = run("tail -8 /home/fabian/comfyui.log 2>/dev/null")

        m, s = divmod((i+1)*15, 60)
        print(f"  [{m}m{s:02d}s] {stats.strip()}")

        if log.strip() != last_log:
            for line in reversed(log.strip().split('\n')):
                l = line.strip()
                if l and not l.startswith('FETCH') and not l.startswith('[DEPRECATION') and not l.startswith('[ComfyUI-Manager]'):
                    print(f"    LOG: {l[:120]}")
                    break
            last_log = log.strip()

        if 'DEAD' in stats:
            print("\nPROCESS DIED!")
            out, _ = run("tail -50 /home/fabian/comfyui.log")
            print(out)
            break
        if 'Prompt executed in' in log:
            print("\nSUCCESS! Image generated!")
            out, _ = run("tail -20 /home/fabian/comfyui.log")
            print(out)
            break
        if 'Traceback' in log or 'RuntimeError' in log:
            print("\nERROR detected!")
            out, _ = run("tail -50 /home/fabian/comfyui.log")
            print(out)
            break

    # Output files
    print("\n=== Output files ===")
    out, _ = run("ls -lah /home/fabian/ComfyUI/output/")
    print(out.strip())

finally:
    ssh.close()
    print("\nSSH closed.")
