"""Tests para ImageGenerator Protocol — runtime_checkable y structural subtyping."""

import sys

sys.path.insert(0, "python/functions/ml")

from image_gen_result import ImageGenResult
from image_generator import ImageGenerator


class MockGenerator:
    """Implementacion dummy que satisface ImageGenerator sin herencia explicita."""

    def generate(self, config):
        """Retorna un ImageGenResult sin imagen real."""
        return ImageGenResult(
            image=None,
            meta={"model": "mock", "seed_used": 0, "sampler": "euler"},
            duration_ms=1,
            vram_peak_mb=None,
        )


class NotAGenerator:
    """Clase que NO implementa generate — no satisface el Protocol."""

    def predict(self, x):
        return x


def test_dummy_satisface_protocol():
    """clase dummy que implementa generate satisface isinstance(x, ImageGenerator)"""
    gen = MockGenerator()
    assert isinstance(gen, ImageGenerator), (
        "MockGenerator debe satisfacer ImageGenerator Protocol (runtime_checkable)"
    )


def test_resultado_es_image_gen_result():
    """generate() retorna ImageGenResult"""
    gen = MockGenerator()
    result = gen.generate(config=None)
    assert isinstance(result, ImageGenResult)


def test_clase_sin_generate_no_satisface_protocol():
    """clase sin metodo generate NO satisface isinstance check"""
    not_gen = NotAGenerator()
    assert not isinstance(not_gen, ImageGenerator), (
        "NotAGenerator no debe satisfacer ImageGenerator Protocol"
    )


def test_multiples_instancias_satisfacen_protocol():
    """multiples instancias del mismo dummy satisfacen el Protocol"""
    for _ in range(3):
        gen = MockGenerator()
        assert isinstance(gen, ImageGenerator)


def test_lambda_con_callable_no_satisface_protocol():
    """un callable lambda no satisface el Protocol (no tiene metodo .generate)"""

    class LambdaLike:
        def __call__(self, config):
            return None

    obj = LambdaLike()
    # __call__ no es lo mismo que .generate — no debe satisfacer el protocol
    assert not isinstance(obj, ImageGenerator)
