"""Test DecoderProxy from proxy.py and full engine init with exception details"""
import sys, os, time, traceback
os.chdir("/models")
sys.path.insert(0, "/opt/qwen3-tts")
os.environ["PYTHONUNBUFFERED"] = "1"

print("=== Test DecoderProxy from proxy.py ===", flush=True)
try:
    from qwen3_tts_gguf.inference.proxy import DecoderProxy
    t0 = time.time()
    dec = DecoderProxy(
        "/models/qwen3-tts/qwen3_tts_decoder.fp16.onnx",
        onnx_provider="CPUExecutionProvider",
        chunk_size=2048
    )
    print(f"  Constructor OK: {time.time()-t0:.2f}s", flush=True)
    print(f"  Waiting for ready (25s)...", flush=True)
    ready = dec.wait_until_ready(timeout=25)
    print(f"  ready={ready}", flush=True)
    if hasattr(dec, 'ready_states'):
        print(f"  states: {dec.ready_states}", flush=True)
except Exception as e:
    print(f"  FAIL: {e}", flush=True)
    traceback.print_exc()

print("\n=== Test GGUF loading ===", flush=True)
try:
    from qwen3_tts_gguf.inference import llama
    t0 = time.time()
    talker = llama.LlamaModel("qwen3-tts/qwen3_tts_talker.q5_k.gguf", n_gpu_layers=-1)
    print(f"  Talker OK: {time.time()-t0:.2f}s", flush=True)
    predictor = llama.LlamaModel("qwen3-tts/qwen3_tts_predictor.q8_0.gguf", n_gpu_layers=-1)
    print(f"  Predictor OK", flush=True)
except Exception as e:
    print(f"  FAIL: {e}", flush=True)
    traceback.print_exc()

print("\n=== Full Engine (verbose, catch exception) ===", flush=True)
try:
    # Monkey-patch to see the actual exception
    import qwen3_tts_gguf.inference.engine as eng_mod
    orig_init = eng_mod.TTSEngine.__init__
    def patched_init(self, *args, **kwargs):
        try:
            orig_init(self, *args, **kwargs)
        except Exception as e:
            print(f"  !! Engine __init__ exception: {e}", flush=True)
            traceback.print_exc()
            raise
    eng_mod.TTSEngine.__init__ = patched_init

    from qwen3_tts_gguf.inference import TTSEngine
    t0 = time.time()
    engine = TTSEngine(model_dir="qwen3-tts", onnx_provider="CPUExecutionProvider")
    elapsed = time.time() - t0
    print(f"  Engine: ready={engine.ready}, took {elapsed:.2f}s", flush=True)
    print(f"  has talker_model: {hasattr(engine, 'talker_model')}", flush=True)
    print(f"  has predictor_model: {hasattr(engine, 'predictor_model')}", flush=True)
    print(f"  has decoder: {hasattr(engine, 'decoder')}", flush=True)
    if hasattr(engine, 'decoder'):
        print(f"  decoder type: {type(engine.decoder)}", flush=True)
        if hasattr(engine.decoder, 'ready_states'):
            print(f"  decoder states: {engine.decoder.ready_states}", flush=True)
except Exception as e:
    print(f"  FAIL: {e}", flush=True)
    traceback.print_exc()

print("\n=== DONE ===", flush=True)
