"""
声纹服务 - 处理声纹同步相关业务逻辑
"""
import logging
from flask import current_app
from src.clients.voiceprint_client import voiceprint_client

logger = logging.getLogger(__name__)


def sync_voiceprint_to_platform(voiceprint, operation):
    """
    同步声纹数据到外部平台
    
    Args:
        voiceprint: 声纹对象，包含 name 和 audio_path 属性
        operation: 操作类型 ('add' 或 'delete')
    
    Raises:
        Exception: 同步失败时抛出异常
    """
    try:
        client = voiceprint_client()
        if operation == 'add':
            # 上传音频文件到声纹平台
            with open(voiceprint.audio_path, 'rb') as audio_file:
                client.add_voiceprint(voiceprint.name, audio_file)
            logger.info(f"成功同步添加声纹人到平台：{voiceprint.name}")
        
        elif operation == 'delete':
            client.delete_voiceprint(voiceprint.name)
            logger.info(f"成功同步删除声纹人从平台：{voiceprint.name}")
            
    except Exception as e:
        logger.error(f"同步声纹数据到平台失败：{str(e)}")
        raise
