#!/usr/bin/env python3
"""Wan 2.2 - Slower romantic scene"""

import json
import urllib.request
import urllib.error
import uuid

COMFY_URL = "http://localhost:8188"
INPUT_IMAGE = "pool_party.png"

PROMPT = """Static camera shot. Two men stand beside a swimming pool at sunset. Very slowly and gently, the larger man on the left turns toward the smaller man. The smaller man turns to face him. They move closer together, wrapping their arms around each other in a warm embrace. They lean in and share a tender, loving kiss on the lips. All movements are slow, smooth, and romantic. Detailed natural hands. The pool and trees remain perfectly still. Camera stays completely static. No zooming, no panning."""

NEGATIVE = "blurry, distorted hands, malformed hands, extra fingers, jerky motion, fast motion, choppy, glitchy, violent, aggressive, artifacts, watermark, text"

workflow = {
    "1": {"class_type": "LoadImage", "inputs": {"image": INPUT_IMAGE, "upload": "image"}},
    "2": {"class_type": "ImageResizeKJv2", "inputs": {
        "image": ["1", 0], "width": 848, "height": 480,
        "upscale_method": "lanczos", "keep_proportion": "crop",
        "pad_color": "0, 0, 0", "crop_position": "center", "divisible_by": 32
    }},
    "8": {"class_type": "WanVideoBlockSwap", "inputs": {
        "blocks_to_swap": 20, "offload_img_emb": False, "offload_txt_emb": False
    }},
    "3": {"class_type": "WanVideoModelLoader", "inputs": {
        "model": "WanVideo/2_2/Wan2_2-I2V-A14B-HIGH_fp8_e4m3fn_scaled_KJ.safetensors",
        "base_precision": "fp16_fast", "quantization": "fp8_e4m3fn_scaled",
        "load_device": "offload_device", "attention_mode": "sdpa",
        "block_swap_args": ["8", 0]
    }},
    "4": {"class_type": "WanVideoModelLoader", "inputs": {
        "model": "WanVideo/2_2/Wan2_2-I2V-A14B-LOW_fp8_e4m3fn_scaled_KJ.safetensors",
        "base_precision": "fp16_fast", "quantization": "fp8_e4m3fn_scaled",
        "load_device": "offload_device", "attention_mode": "sdpa",
        "block_swap_args": ["8", 0]
    }},
    "5": {"class_type": "WanVideoVAELoader", "inputs": {
        "model_name": "wanvideo/Wan2_1_VAE_bf16.safetensors", "precision": "bf16"
    }},
    "6": {"class_type": "LoadWanVideoT5TextEncoder", "inputs": {
        "model_name": "umt5-xxl-enc-fp8_e4m3fn.safetensors",
        "precision": "bf16", "load_device": "offload_device", "quantization": "fp8_e4m3fn"
    }},
    "7": {"class_type": "WanVideoTextEncode", "inputs": {
        "positive_prompt": PROMPT, "negative_prompt": NEGATIVE,
        "t5": ["6", 0], "force_offload": True, "use_disk_cache": False
    }},
    "9": {"class_type": "WanVideoImageToVideoEncode", "inputs": {
        "width": ["2", 1], "height": ["2", 2], "num_frames": 81,
        "noise_aug_strength": 0, "start_latent_strength": 0.9,
        "end_latent_strength": 0.9, "force_offload": True,
        "vae": ["5", 0], "start_image": ["2", 0]
    }},
    "10": {"class_type": "WanVideoSampler", "inputs": {
        "model": ["3", 0], "image_embeds": ["9", 0],
        "steps": 10, "cfg": 1.0, "shift": 6, "seed": 42,
        "force_offload": True, "scheduler": "unipc",
        "riflex_freq_index": 0, "text_embeds": ["7", 0], "end_step": 5
    }},
    "11": {"class_type": "WanVideoSampler", "inputs": {
        "model": ["4", 0], "image_embeds": ["9", 0],
        "steps": 10, "cfg": 0.8, "shift": 6, "seed": 42,
        "force_offload": True, "scheduler": "unipc",
        "riflex_freq_index": 0, "text_embeds": ["7", 0],
        "samples": ["10", 0], "start_step": 5
    }},
    "12": {"class_type": "WanVideoDecode", "inputs": {
        "vae": ["5", 0], "samples": ["11", 0],
        "enable_vae_tiling": False, "enable_tiling": False,
        "tile_x": 272, "tile_y": 272, "tile_stride_x": 144, "tile_stride_y": 128
    }},
    "13": {"class_type": "VHS_VideoCombine", "inputs": {
        "images": ["12", 0], "frame_rate": 20, "loop_count": 0,
        "filename_prefix": "Wan22_Romantic", "format": "video/h264-mp4",
        "pix_fmt": "yuv420p", "crf": 16, "save_metadata": True,
        "trim_to_audio": False, "pingpong": False, "save_output": True
    }}
}

def submit():
    prompt_id = str(uuid.uuid4())
    data = {"prompt": workflow, "client_id": "selena"}
    req = urllib.request.Request(
        f"{COMFY_URL}/prompt",
        data=json.dumps(data).encode('utf-8'),
        headers={'Content-Type': 'application/json'}
    )
    try:
        with urllib.request.urlopen(req) as response:
            result = json.loads(response.read().decode('utf-8'))
            print(f"✅ Submitted! ID: {result.get('prompt_id', prompt_id)}")
            return result.get('prompt_id')
    except urllib.error.HTTPError as e:
        print(f"❌ Error: {e.read().decode('utf-8')[:500]}")
        return None

if __name__ == "__main__":
    print("🎬 Wan 2.2 Romantic Kiss:")
    print("   - Slower motion (lower shift 6)")
    print("   - Lower CFG (1.0/0.8)")
    print("   - latent_strength 0.9 (more motion)")
    print("   - 20fps playback")
    print()
    submit()
