# -*- coding: utf-8 -*-
"""
工作流程路由器
根据客户说的话判断执行哪种工作流
"""
import logging
from typing import Tuple, Optional
import re

logger = logging.getLogger(__name__)

class WorkflowRouter:
    """工作流程路由器"""
    
    def __init__(self):
        # 查找图片关键词
        self.find_keywords = [
            "找一下", "找图", "找原图", "帮我找", "能找到吗", 
            "有吗", "有没有", "找到", "搜一下"
        ]
        
        # 处理图片关键词
        self.process_keywords = [
            "做一下", "处理一下", "安排", "开始做", 
            "弄一下", "修一下", "P 一下", "P 图"
        ]
        
        # 无法处理关键词
        self.unable_keywords = [
            "做不了", "处理不了", "弄不了", "无法处理",
            "做不到", "搞不定"
        ]
    
    def detect_workflow(self, message: str) -> Tuple[str, str]:
        """
        检测工作流程类型
        
        Args:
            message: 客户消息
            
        Returns:
            (workflow_type, confidence)
            workflow_type: "find_image" / "process_image" / "transfer_human"
            confidence: 置信度 0-1
        """
        message_lower = message.lower()
        
        # 1. 检测查找图片
        for kw in self.find_keywords:
            if kw in message_lower:
                logger.info(f"检测到查找图片意图：{kw}")
                return ("find_image", 0.9)
        
        # 2. 检测处理图片
        for kw in self.process_keywords:
            if kw in message_lower:
                logger.info(f"检测到处理图片意图：{kw}")
                return ("process_image", 0.9)
        
        # 3. 检测无法处理
        for kw in self.unable_keywords:
            if kw in message_lower:
                logger.info(f"检测到无法处理：{kw}")
                return ("transfer_human", 0.9)
        
        # 默认返回未知
        return ("unknown", 0.5)
    
    def should_find_image(self, message: str) -> bool:
        """是否应该查找图片"""
        workflow_type, _ = self.detect_workflow(message)
        return workflow_type == "find_image"
    
    def should_process_image(self, message: str) -> bool:
        """是否应该处理图片"""
        workflow_type, _ = self.detect_workflow(message)
        return workflow_type == "process_image"
    
    def should_transfer_human(self, message: str) -> bool:
        """是否应该转人工"""
        workflow_type, _ = self.detect_workflow(message)
        return workflow_type == "transfer_human"


# 单例
_router: Optional[WorkflowRouter] = None

def get_workflow_router() -> WorkflowRouter:
    """获取工作流程路由器单例"""
    global _router
    if _router is None:
        _router = WorkflowRouter()
    return _router
