"""Create leaudit OCR/LLM/VLM clients from docauditai's env.{port} config."""

from __future__ import annotations

import logging
from typing import TYPE_CHECKING

from fastapi_admin.config import (
    OCR_BASE_URL,
    OCR_TIMEOUT,
    LEAUDIT_LLM_REQUEST_TIMEOUT,
    LEAUDIT_LLM_RETRY_BACKOFF_BASE_SECONDS,
    LEAUDIT_LLM_RETRY_MAX_ATTEMPTS,
    LEAUDIT_OCR_VLM_CONCURRENCY,
    LEAUDIT_OCR_RETRY_BACKOFF_BASE_SECONDS,
    LEAUDIT_OCR_RETRY_MAX_ATTEMPTS,
    LEAUDIT_VLM_REQUEST_TIMEOUT,
    LEAUDIT_VLM_RETRY_BACKOFF_BASE_SECONDS,
    LEAUDIT_VLM_RETRY_MAX_ATTEMPTS,
    LLM_BASE_URL,
    LLM_MODEL,
    LLM_API_KEY,
    VLM_BASE_URL,
    VLM_MODEL,
    VLM_API_KEY,
)
from fastapi_modules.fastapi_leaudit.leaudit_bridge.resilient_clients import (
    ResilientChandraOCRClient,
    ResilientOpenAICompatibleClient,
    ResilientQwenVLMClient,
)

if TYPE_CHECKING:
    from leaudit.llm.base import BaseLLMClient
    from leaudit.llm.vlm_base import BaseVLMClient
    from leaudit.ocr.base import BaseOCRClient

log = logging.getLogger(__name__)


def create_ocr_client() -> BaseOCRClient:
    """Create a leaudit ChandraOCRClient from LEAUDIT_OCR_URL config."""
    import os

    base_url = os.getenv("LEAUDIT_OCR_URL", "").rstrip("/")
    if not base_url:
        base_url = OCR_BASE_URL.rstrip("/")
    timeout = float(OCR_TIMEOUT)

    client = ResilientChandraOCRClient(
        base_url=base_url,
        timeout=timeout,
        include_images=True,
        vlm_concurrency=LEAUDIT_OCR_VLM_CONCURRENCY,
        retry_max_attempts=LEAUDIT_OCR_RETRY_MAX_ATTEMPTS,
        retry_backoff_base_seconds=LEAUDIT_OCR_RETRY_BACKOFF_BASE_SECONDS,
    )
    log.info(
        "leaudit OCR client created: %s (timeout=%ss, vlm_concurrency=%s, retry_max_attempts=%s, retry_backoff_base_seconds=%s)",
        base_url,
        timeout,
        LEAUDIT_OCR_VLM_CONCURRENCY,
        LEAUDIT_OCR_RETRY_MAX_ATTEMPTS,
        LEAUDIT_OCR_RETRY_BACKOFF_BASE_SECONDS,
    )
    return client


def create_llm_client() -> BaseLLMClient:
    """Create a leaudit OpenAICompatibleClient from docauditai's LLM config."""
    base_url = LLM_BASE_URL
    model = LLM_MODEL
    api_key = LLM_API_KEY or "no-key"

    client = ResilientOpenAICompatibleClient(
        api_key=api_key,
        base_url=base_url,
        default_model=model,
        timeout=float(LEAUDIT_LLM_REQUEST_TIMEOUT),
        retry_max_attempts=LEAUDIT_LLM_RETRY_MAX_ATTEMPTS,
        retry_backoff_base_seconds=LEAUDIT_LLM_RETRY_BACKOFF_BASE_SECONDS,
    )
    log.info(
        "leaudit LLM client created: %s (model=%s, timeout=%ss, retry_max_attempts=%s, retry_backoff_base_seconds=%s)",
        base_url,
        model,
        LEAUDIT_LLM_REQUEST_TIMEOUT,
        LEAUDIT_LLM_RETRY_MAX_ATTEMPTS,
        LEAUDIT_LLM_RETRY_BACKOFF_BASE_SECONDS,
    )
    return client


def create_vlm_client() -> BaseVLMClient | None:
    """Create a leaudit QwenVLMClient from docauditai's VLM config."""
    base_url = VLM_BASE_URL
    model = VLM_MODEL
    api_key = VLM_API_KEY or LLM_API_KEY or "no-key"

    if not base_url or not model:
        log.info("leaudit VLM client skipped: no VLM config")
        return None

    client = ResilientQwenVLMClient(
        base_url=base_url,
        api_key=api_key,
        model=model,
        timeout=float(LEAUDIT_VLM_REQUEST_TIMEOUT),
        retry_max_attempts=LEAUDIT_VLM_RETRY_MAX_ATTEMPTS,
        retry_backoff_base_seconds=LEAUDIT_VLM_RETRY_BACKOFF_BASE_SECONDS,
    )
    log.info(
        "leaudit VLM client created: %s (model=%s, timeout=%ss, retry_max_attempts=%s, retry_backoff_base_seconds=%s)",
        base_url,
        model,
        LEAUDIT_VLM_REQUEST_TIMEOUT,
        LEAUDIT_VLM_RETRY_MAX_ATTEMPTS,
        LEAUDIT_VLM_RETRY_BACKOFF_BASE_SECONDS,
    )
    return client
