#!/usr/bin/env python3
"""Deep engine init diagnostic — catch every failure point."""
import os, sys, traceback, logging
from pathlib import Path

# Configure ALL loggers to console
logging.basicConfig(level=logging.DEBUG, stream=sys.stderr, format='%(name)s %(levelname)s %(message)s')

MODEL_DIR = "/models/qwen3-tts"
parent = os.path.dirname(os.path.abspath(MODEL_DIR))
basename = os.path.basename(MODEL_DIR)
os.chdir(parent)
print(f"cwd={Path.cwd()}, basename={basename}", flush=True)

project_root = Path.cwd()
model_dir = project_root / basename
print(f"model_dir={model_dir}", flush=True)

# Step 1: AssetsManager
print("\n=== Step 1: AssetsManager ===", flush=True)
try:
    from qwen3_tts_gguf.inference.assets import AssetsManager
    assets = AssetsManager(str(model_dir))
    print(f"  OK: assets={assets}", flush=True)
except Exception as e:
    print(f"  FAIL: {e}", flush=True)
    traceback.print_exc()

# Step 2: Tokenizer
print("\n=== Step 2: Tokenizer ===", flush=True)
try:
    from tokenizers import Tokenizer
    tok = Tokenizer.from_file(str(model_dir / "tokenizer.json"))
    print(f"  OK: tokenizer loaded", flush=True)
except Exception as e:
    print(f"  FAIL: {e}", flush=True)
    traceback.print_exc()

# Step 3: CodecEncoder
print("\n=== Step 3: CodecEncoder ===", flush=True)
try:
    from qwen3_tts_gguf.inference.encoder import CodecEncoder
    codec = CodecEncoder(str(model_dir / "qwen3_tts_codec_encoder.fp16.onnx"))
    print(f"  OK: codec_encoder={codec}", flush=True)
except Exception as e:
    print(f"  FAIL: {e}", flush=True)
    traceback.print_exc()

# Step 4: SpeakerEncoder
print("\n=== Step 4: SpeakerEncoder ===", flush=True)
try:
    from qwen3_tts_gguf.inference.encoder import SpeakerEncoder
    spk = SpeakerEncoder(str(model_dir / "qwen3_tts_speaker_encoder.fp16.onnx"))
    print(f"  OK: speaker_encoder={spk}", flush=True)
except Exception as e:
    print(f"  FAIL: {e}", flush=True)
    traceback.print_exc()

# Step 5: DecoderProxy
print("\n=== Step 5: DecoderProxy ===", flush=True)
try:
    from qwen3_tts_gguf.inference.proxy import DecoderProxy
    decoder = DecoderProxy(str(model_dir / "qwen3_tts_decoder.fp16.onnx"), onnx_provider="CPUExecutionProvider", chunk_size=12)
    print(f"  OK: decoder={decoder}", flush=True)
    print("  Waiting for decoder ready (timeout=10)...", flush=True)
    is_ready = decoder.wait_until_ready(timeout=10)
    print(f"  decoder ready={is_ready}", flush=True)
    if hasattr(decoder, 'ready_states'):
        print(f"  ready_states={decoder.ready_states}", flush=True)
except Exception as e:
    print(f"  FAIL: {e}", flush=True)
    traceback.print_exc()

# Step 6: LlamaModel
print("\n=== Step 6: LlamaModel (GGUF) ===", flush=True)
try:
    from qwen3_tts_gguf.inference import llama
    t_path = (model_dir / "qwen3_tts_talker.q5_k.gguf").relative_to(project_root).as_posix()
    p_path = (model_dir / "qwen3_tts_predictor.q8_0.gguf").relative_to(project_root).as_posix()
    print(f"  talker_path={t_path}", flush=True)
    print(f"  predictor_path={p_path}", flush=True)
    talker = llama.LlamaModel(t_path, n_gpu_layers=-1)
    print(f"  OK: talker={talker}", flush=True)
    predictor = llama.LlamaModel(p_path, n_gpu_layers=-1)
    print(f"  OK: predictor={predictor}", flush=True)
except Exception as e:
    print(f"  FAIL: {e}", flush=True)
    traceback.print_exc()

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