"""API SDK for image generation and other services"""

# Auto-load .env (if present) so users can drop a .env file with
# GEMINI_API_KEY without additional setup. No fallback/try-except here per policy.
from pathlib import Path as _Path  # pragma: no cover
from ._env import load_env_once  # pragma: no cover

# Build candidate .env locations in priority order and load the first existing.
_here = _Path(__file__).resolve()
_parents = list(_here.parents)
_repo_root = None
for p in _parents:
    if (p / "pyproject.toml").exists() or (p / ".git").exists():
        _repo_root = p
        break

_candidates = []
if _repo_root is not None:
    _candidates.append(_repo_root / ".env")

# Project-fixed canonical location requested by user (absolute path)
_canonical = _Path("/Users/junjiecai/Desktop/projects/scientith_projects/fundamental/api_sdk/.env")
_candidates.append(_canonical)

# Also consider package-local .env as a tertiary option
_candidates.append(_here.parent / ".env")

load_env_once(_candidates)  # pragma: no cover

__version__ = "0.1.0"

# Public exports for image generation
from .image_generator import (
    ImageGenerator,
    generate_image,
    generate_image_bytes,
    DEFAULT_IMAGE_MODEL,
    DEFAULT_JIMENG_IMAGE_MODEL,
)

# Public exports for video generation (Veo)
from .video_generator import (
    VideoGenerator,
    generate_video,
    generate_videos,
    generate_video_bytes,
    generate_videos_bytes,
    DEFAULT_VIDEO_MODEL,
    DEFAULT_JIMENG_VIDEO_MODEL,
)

# Optional: expose Jimeng client for advanced usage.
from .jimeng_video import JimengVideoClient
from .jimeng_image import JimengImageClient

# Public exports for detection
from .object_detector import (
    detect_objects,
    draw_bounding_boxes,
    detect_and_draw,
)

# Public exports for design report
from .design_report import (
    ImageGridSpec,
    DesignReportSpec,
    compose_design_report,
    save_design_report,
    build_design_report_from_config,
)

# Public export for background removal (ML-based via rembg)
def remove_background_ml(*args, **kwargs):
    # Lazy import to avoid requiring numpy/rembg unless used
    from .ml_background_remover import remove_background_ml as _impl
    return _impl(*args, **kwargs)
from .text_generator import generate_text
from .text_generator import generate_text_structured
try:
    from .tts import (
        synthesize_speech_bytes,
        generate_speech,
        generate_speech_bytes,
    )
except Exception:  # pragma: no cover - optional dependency path
    def _missing_tts(*args, **kwargs):
        raise ImportError(
            "iFlytek TTS requires 'websocket-client'. Install it or use provider='algieba' (Gemini)."
        )
    synthesize_speech_bytes = _missing_tts  # type: ignore[assignment]
    generate_speech = _missing_tts  # type: ignore[assignment]
    generate_speech_bytes = _missing_tts  # type: ignore[assignment]

from .tts_google import (
    synthesize_speech_google_bytes,
    DEFAULT_GOOGLE_TTS_MODEL,
)

from .tts_elevenlabs import (
    synthesize_speech_elevenlabs_bytes,
    DEFAULT_ELEVENLABS_TTS_MODEL,
    DEFAULT_ELEVENLABS_OUTPUT_FORMAT,
    DEFAULT_ELEVENLABS_VOICE_ID,
)

__all__ = [
    "__version__",
    # image generation
    "ImageGenerator",
    "generate_image",
    "generate_image_bytes",
    "DEFAULT_IMAGE_MODEL",
    "DEFAULT_JIMENG_IMAGE_MODEL",
    # video generation
    "VideoGenerator",
    "generate_video",
    "generate_videos",
    "generate_video_bytes",
    "generate_videos_bytes",
    "DEFAULT_VIDEO_MODEL",
    "DEFAULT_JIMENG_VIDEO_MODEL",
    "JimengVideoClient",
    "JimengImageClient",
    # detection
    "detect_objects",
    "draw_bounding_boxes",
    "detect_and_draw",
    # design report
    "ImageGridSpec",
    "DesignReportSpec",
    "compose_design_report",
    "save_design_report",
    "build_design_report_from_config",
    # background removal
    "remove_background_ml",
    # text generation
    "generate_text",
    "generate_text_structured",
    # tts (iFlytek; may raise if websocket-client missing)
    "synthesize_speech_bytes",
    "generate_speech",
    "generate_speech_bytes",
    # google tts
    "synthesize_speech_google_bytes",
    "DEFAULT_GOOGLE_TTS_MODEL",
    "synthesize_speech_elevenlabs_bytes",
    "DEFAULT_ELEVENLABS_TTS_MODEL",
    "DEFAULT_ELEVENLABS_OUTPUT_FORMAT",
    "DEFAULT_ELEVENLABS_VOICE_ID",
]
