#!/usr/bin/env python3
"""Replicate exact server.py init_engine with chdir fix, max verbosity."""
import os, sys, traceback
sys.stdout = sys.stderr  # ensure all output goes to same stream
from pathlib import Path

MODEL_DIR = "/models/qwen3-tts"
parent = os.path.dirname(os.path.abspath(MODEL_DIR))
basename = os.path.basename(MODEL_DIR)

print(f"[diag] chdir to {parent}")
os.chdir(parent)
print(f"[diag] cwd now = {Path.cwd()}")
print(f"[diag] basename = {basename}")

# Check all files with expected names
model_path = Path.cwd() / basename
for f in sorted(model_path.iterdir()):
    print(f"  {f.name}  {'(link)' if f.is_symlink() else ''}")

print(f"\n[diag] Importing TTSEngine...")
from qwen3_tts_gguf.inference import TTSEngine

print(f"[diag] Creating TTSEngine(model_dir={basename!r}, onnx_provider='CPUExecutionProvider')...")
try:
    engine = TTSEngine(model_dir=basename, onnx_provider="CPUExecutionProvider")
    print(f"\n[diag] engine.ready = {engine.ready}")
    print(f"[diag] bool(engine) = {bool(engine)}")
    if hasattr(engine, 'decoder'):
        print(f"[diag] decoder = {engine.decoder}")
        if hasattr(engine.decoder, 'ready_states'):
            print(f"[diag] decoder.ready_states = {engine.decoder.ready_states}")
except Exception as e:
    print(f"[diag] EXCEPTION: {e}")
    traceback.print_exc()
