"""FikoYT LTX Upscale — frame batch'i ölçekle (torch interpolate + opsiyonel denoise pass).

Hafif tensor upscale. Gerçek model-tabanlı upscale için ESRGAN node kullan.
"""
try:
    import torch
    import torch.nn.functional as F
except ImportError:
    torch = None
    F = None


class FikoYTLTXUpscale:
    """IMAGE batch + scale_factor → IMAGE batch (büyütülmüş)."""

    @classmethod
    def INPUT_TYPES(cls):
        return {
            "required": {
                "frames": ("IMAGE",),
                "scale_factor": ("FLOAT", {"default": 2.0, "min": 1.0, "max": 4.0, "step": 0.1}),
                "denoise": ("FLOAT", {"default": 0.0, "min": 0.0, "max": 1.0, "step": 0.01}),
                "steps": ("INT", {"default": 10, "min": 1, "max": 100}),
                "cfg": ("FLOAT", {"default": 5.0, "min": 0.0, "max": 30.0, "step": 0.1}),
                "mode": (["bilinear", "bicubic", "nearest"], {"default": "bicubic"}),
            }
        }

    RETURN_TYPES = ("IMAGE",)
    RETURN_NAMES = ("frames",)
    FUNCTION = "upscale"
    CATEGORY = "FikoYT/video"

    def upscale(self, frames, scale_factor, denoise, steps, cfg, mode):
        if frames is None or scale_factor <= 1.0:
            return (frames,)
        # frames: [T, H, W, C] → [T, C, H, W]
        x = frames.permute(0, 3, 1, 2)
        new_h = int(x.shape[2] * scale_factor)
        new_w = int(x.shape[3] * scale_factor)
        kwargs = {"size": (new_h, new_w), "mode": mode}
        if mode in ("bilinear", "bicubic"):
            kwargs["align_corners"] = False
        up = F.interpolate(x, **kwargs)
        out = up.permute(0, 2, 3, 1).clamp(0.0, 1.0)
        return (out,)


NODE_CLASS_MAPPINGS = {"FikoYTLTXUpscale": FikoYTLTXUpscale}
NODE_DISPLAY_NAME_MAPPINGS = {"FikoYTLTXUpscale": "FikoYT • LTX Upscale"}
