# API 蓝图统一导出和注册
# 所有蓝图文件在此统一导出，并提供统一的注册函数

from .auth import auth_bp, init_auth_bp
from .recording import recording_bp, init_recording_bp
from .recording_download import recording_download_bp
from .share import share_bp, init_share_bp
from .tag import tag_bp, init_tag_bp
from .speaker import speaker_bp, init_speaker_bp
from .admin import admin_bp, init_admin_bp
from .search import search_bp, init_search_bp
from .voiceprint import voiceprint_bp, init_voiceprint_bp
from .config import config_bp, init_config_bp
from .hotword import hotword_bp, init_hotword_bp
from .template import template_bp, init_template_bp
from .event import event_bp
from .main import main_bp, init_main_bp
from .draft_api import draft_bp
from . import asr_ws  # noqa: F401

# 从 flask_ext 导入扩展实例（不再由 app.py 传入）
from src.flask_ext import csrf, limiter, bcrypt

# 从 clients 导入 LLM 客户端
from src.clients import client


def register_blueprints(app):
    """
    统一注册所有蓝图到 Flask 应用
    
    扩展实例（csrf、limiter、bcrypt）和 LLM 客户端（client）
    直接从 flask_ext 和 clients 模块导入，无需外部传入。
    
    Args:
        app: Flask 应用实例
    """
    # 所有蓝图列表
    blueprints = [
        auth_bp, recording_bp, recording_download_bp, share_bp, tag_bp, speaker_bp,
        search_bp, voiceprint_bp, config_bp, hotword_bp,
        template_bp, event_bp, main_bp, admin_bp
    ]
    
    # 注册所有蓝图
    for bp in blueprints:
        app.register_blueprint(bp)
        csrf.exempt(bp)

    app.register_blueprint(draft_bp, url_prefix='/api/draft')
    csrf.exempt(draft_bp)
    
    # 初始化所有蓝图（传入各自需要的参数）
    init_auth_bp(
        limiter_instance=limiter,
        csrf_instance=csrf,
        bcrypt_instance=bcrypt,
        use_asr_endpoint=app.config.get('USE_ASR_ENDPOINT', False),
        asr_diarize=app.config.get('ASR_DIARIZE', False)
    )
    
    init_recording_bp(
        limiter_instance=limiter,
        csrf_instance=csrf,
        bcrypt_instance=bcrypt,
        logger=app.logger,
        flask_app_instance=app,
        openai_client=client,
        use_asr_endpoint=app.config.get('USE_ASR_ENDPOINT', False),
        asr_base_url=app.config.get('ASR_BASE_URL', ''),
        asr_diarize=app.config.get('ASR_DIARIZE', False),
        asr_min_speakers=app.config.get('ASR_MIN_SPEAKERS', 1),
        asr_max_speakers=app.config.get('ASR_MAX_SPEAKERS', 10),
        enable_chunking=app.config.get('ENABLE_CHUNKING', False),
        chunking_svc=app.config.get('chunking_service'),
        enable_inquire_mode=app.config.get('ENABLE_INQUIRE_MODE', False)
    )
    
    init_share_bp(csrf_instance=csrf)
    
    init_tag_bp(
        limiter_instance=limiter,
        csrf_instance=csrf
    )
    
    init_speaker_bp(csrf_instance=csrf)
    
    init_search_bp(
        limiter_instance=limiter,
        csrf_instance=csrf,
        enable_inquire=app.config.get('ENABLE_INQUIRE_MODE', False),
        use_asr_endpoint=app.config.get('USE_ASR_ENDPOINT', False)
    )
    
    init_voiceprint_bp(
        limiter_instance=limiter,
        csrf_instance=csrf,
        bcrypt_instance=bcrypt
    )
    
    init_config_bp(
        limiter_instance=limiter,
        csrf_instance=csrf,
        bcrypt_instance=bcrypt,
        use_asr_endpoint=app.config.get('USE_ASR_ENDPOINT', False),
        enable_chunking=app.config.get('ENABLE_CHUNKING', False),
        chunking_svc=app.config.get('chunking_service')
    )
    
    init_hotword_bp(csrf_instance=csrf)
    
    init_template_bp(csrf_instance=csrf)
    
    init_main_bp(
        limiter_instance=limiter,
        csrf_instance=csrf,
        bcrypt_instance=bcrypt,
        use_asr_endpoint=app.config.get('USE_ASR_ENDPOINT', False),
        asr_diarize=app.config.get('ASR_DIARIZE', False),
        enable_inquire_mode=app.config.get('ENABLE_INQUIRE_MODE', False),
        valid_token=app.config.get('VALID_TOKEN', '')
    )
    
    init_admin_bp(
        limiter_instance=limiter,
        csrf_instance=csrf,
        bcrypt_instance=bcrypt,
        enable_inquire_mode=app.config.get('ENABLE_INQUIRE_MODE', False),
        embeddings_available=app.config.get('EMBEDDINGS_AVAILABLE', False),
        use_asr_endpoint=app.config.get('USE_ASR_ENDPOINT', False),
        asr_base_url=app.config.get('ASR_BASE_URL', ''),
        text_model_base_url=app.config.get('TEXT_MODEL_BASE_URL', ''),
        text_model_name=app.config.get('TEXT_MODEL_NAME', ''),
        transcription_base_url=app.config.get('TRANSCRIPTION_BASE_URL', 'https://api.openai.com/v1'),
        valid_token=app.config.get('VALID_TOKEN', '')
    )


__all__ = [
    # 蓝图实例
    'auth_bp', 'recording_bp', 'share_bp', 'tag_bp', 'speaker_bp',
    'admin_bp', 'search_bp', 'voiceprint_bp', 'config_bp', 'hotword_bp',
    'template_bp', 'event_bp', 'main_bp', 'draft_bp',
    # 注册函数
    'register_blueprints',
]
