from abc import ABC


# class   WorkflowDifyAPI和chatDifyApi和completionDifyApi
# dify_app_mode_type  ：workflow, chat, completion


class TaskInstance(ABC):
    def __init__(self, base_url, dify_app_sks, dify_app_mode_type):
        self.base_url = base_url
        self.dify_app_sks = dify_app_sks
        self.dify_app_mode_type = dify_app_mode_type

    def get_task_instance(self, task_id: str):
        """
        根据dify_app_mode_type返回相应的API实例

        Args:
            task_id: 任务ID

        Returns:
            返回对应的API实例

        Raises:
            ValueError: 当dify_app_mode_type无效时抛出异常
        """
        from src.workflow.workflow_server import WorkflowDifyAPI
        from src.completion.completion_server import CompletionDifyAPI
        from src.chat.chat_server import ChatDifyAPI

        # 使用字典映射提高代码灵活性和可维护性
        api_classes = {
            "workflow": WorkflowDifyAPI,
            "chat": ChatDifyAPI,
            "completion": CompletionDifyAPI,
        }

        # 检查mode_type是否有效
        if self.dify_app_mode_type.lower() not in api_classes:
            supported_types = ", ".join(api_classes.keys())
            raise ValueError(
                f"不支持的dify_app_mode_type: {self.dify_app_mode_type}，支持的类型: {supported_types}"
            )

        # 获取对应的API类
        api_class = api_classes[self.dify_app_mode_type.lower()]

        # 这里假设所有API类都接受相同的参数集
        # 如果各API类构造函数参数不同，需要针对每种类型单独处理
        return api_class(
            self.base_url,
            self.dify_app_sks,
        )
