"""
Decode Workflow.

解码工作流：合并 What 解构工作流和脚本理解工作流的完整流程。
流程：视频上传 → 灵感点提取 → 目的点提取 → 关键点提取 → 选题理解 → 
      段落划分 → 实质提取 → 形式提取 → 分离结果 → 结果汇总
"""

from typing import Dict, Any
from langgraph.graph import StateGraph, END

from src.components.agents.base import BaseGraphAgent
from src.components.agents.topic_selection_understanding_agent import TopicSelectionUnderstandingAgent
from src.components.functions.result_aggregation_function import ResultAggregationFunction
from src.components.functions.video_upload_function import VideoUploadFunction
# What解构相关Agent
from src.components.agents.inspiration_points_agent import InspirationPointsAgent
from src.components.agents.purpose_point_agent import PurposePointAgent
from src.components.agents.key_points_agent import KeyPointsAgent
# 脚本理解相关Agent
from src.components.agents.script_section_division_agent import ScriptSectionDivisionAgent
from src.components.agents.script_substance_extraction_agent import ScriptSubstanceExtractionAgent
from src.components.agents.script_form_extraction_agent import ScriptFormExtractionAgent
from src.utils.logger import get_logger

logger = get_logger(__name__)


class DecodeWorkflow(BaseGraphAgent):
    """解码工作流（合并 What 解构和脚本理解）

    功能：
    - 编排完整的解码流程（视频分析）
    - 流程：视频上传 → 灵感点提取 → 目的点提取 → 关键点提取 → 选题理解 → 
           段落划分 → 实质提取 → 形式提取 → 分离结果 → 结果汇总
    - 管理状态传递
    - 仅支持单视频输入

    实现方式：BaseGraphAgent (LangGraph)
    """

    def __init__(
        self,
        name: str = "decode_workflow",
        description: str = "解码工作流（合并 What 解构和脚本理解）",
        model_provider: str = "google_genai",
        max_depth: int = 10
    ):
        super().__init__(
            name=name,
            description=description,
            state_class=dict
        )

        self.max_depth = max_depth
        self.model_provider = model_provider

        # 初始化视频上传Function
        self.video_upload_func = VideoUploadFunction()

        # 初始化What解构相关Agent
        self.inspiration_points_agent = InspirationPointsAgent(
            model_provider=model_provider
        )
        self.purpose_point_agent = PurposePointAgent(
            model_provider=model_provider
        )
        self.key_points_agent = KeyPointsAgent(
            model_provider=model_provider
        )
        self.topic_selection_understanding_agent = TopicSelectionUnderstandingAgent(
            model_provider=model_provider
        )

        # 初始化脚本理解相关Agent
        self.section_agent = ScriptSectionDivisionAgent(
            model_provider=model_provider
        )
        self.substance_agent = ScriptSubstanceExtractionAgent(
            model_provider=model_provider
        )
        self.form_agent = ScriptFormExtractionAgent(
            model_provider=model_provider
        )

        # 初始化结果汇总Function
        self.result_aggregation_func = ResultAggregationFunction()

        logger.info(f"DecodeWorkflow 初始化完成")

    def _build_graph(self) -> StateGraph:
        """构建工作流图

        完整流程：
        START → 视频上传 → 灵感点提取 → 目的点提取 → 关键点提取 → 选题理解 → 
        段落划分 → 实质提取 → 形式提取 → 分离结果 → 结果汇总 → END
        """
        workflow = StateGraph(dict)  # 使用dict作为状态类型

        # 添加所有节点
        workflow.add_node("video_upload", self._video_upload_node)
        # What解构节点
        workflow.add_node("inspiration_points_extraction", self._inspiration_points_node)
        workflow.add_node("purpose_point_extraction", self._purpose_point_node)
        workflow.add_node("key_points_extraction", self._key_points_node)
        workflow.add_node("topic_selection_understanding", self._topic_selection_understanding_node)
        # 脚本理解节点
        workflow.add_node("section_division", self._section_division_node)
        workflow.add_node("substance_extraction", self._substance_extraction_node)
        workflow.add_node("form_extraction", self._form_extraction_node)
        workflow.add_node("merge_all_results", self._merge_all_results_node)
        workflow.add_node("result_aggregation", self._result_aggregation_node)

        # 定义流程的边
        workflow.set_entry_point("video_upload")
        # 视频上传后使用条件边：成功则继续，失败则终止
        workflow.add_conditional_edges(
            "video_upload",
            self._check_video_upload_success,
            {
                "success": "inspiration_points_extraction",
                "failure": END
            }
        )
        # What解构流程 - 在关键节点后添加错误检查
        workflow.add_conditional_edges(
            "inspiration_points_extraction",
            self._check_workflow_status,
            {
                "continue": "purpose_point_extraction",
                "terminate": END
            }
        )
        workflow.add_conditional_edges(
            "purpose_point_extraction",
            self._check_workflow_status,
            {
                "continue": "key_points_extraction",
                "terminate": END
            }
        )
        workflow.add_conditional_edges(
            "key_points_extraction",
            self._check_workflow_status,
            {
                "continue": "topic_selection_understanding",
                "terminate": END
            }
        )
        workflow.add_conditional_edges(
            "topic_selection_understanding",
            self._check_workflow_status,
            {
                "continue": "section_division",
                "terminate": END
            }
        )
        # 脚本理解流程
        workflow.add_conditional_edges(
            "section_division",
            self._check_workflow_status,
            {
                "continue": "substance_extraction",
                "terminate": END
            }
        )
        workflow.add_conditional_edges(
            "substance_extraction",
            self._check_workflow_status,
            {
                "continue": "form_extraction",
                "terminate": END
            }
        )
        workflow.add_conditional_edges(
            "form_extraction",
            self._check_workflow_status,
            {
                "continue": "merge_all_results",
                "terminate": END
            }
        )
        workflow.add_edge("merge_all_results", "result_aggregation")
        workflow.add_edge("result_aggregation", END)

        logger.info("工作流图构建完成 - 完整流程：视频上传 → What解构 → 脚本理解 → 结果汇总")

        return workflow

    def _check_video_upload_success(self, state: Dict[str, Any]) -> str:
        """检查视频上传是否成功
        
        Returns:
            "success" 如果上传成功，否则返回 "failure"
        """
        video_uri = state.get("video_uploaded_uri")
        video_error = state.get("video_upload_error")
        
        # 如果URI存在且没有错误，则认为成功
        if video_uri and not video_error:
            logger.info("视频上传成功，继续执行后续流程")
            return "success"
        else:
            error_msg = video_error or "视频上传失败：未获取到视频URI"
            logger.error(f"视频上传失败，终止workflow: {error_msg}")
            # 设置失败信息到状态中
            state["workflow_failed"] = True
            state["workflow_error"] = error_msg
            return "failure"

    def _check_critical_error(self, state: Dict[str, Any], error_source: str = "") -> bool:
        """检查关键错误，如果存在则设置失败标志
        
        Args:
            state: 状态字典
            error_source: 错误来源（用于日志）
            
        Returns:
            True 如果存在致命错误，False 否则
        """
        # 检查是否已经失败
        if state.get("workflow_failed"):
            return True
        
        # 检查视频文件是否可用
        from src.utils.llm_invoker import get_video_file_from_state
        video_file = get_video_file_from_state(state)
        if not video_file:
            error_msg = f"无法获取视频文件对象{('（' + error_source + '）' if error_source else '')}"
            logger.error(f"{error_msg}，终止workflow")
            state["workflow_failed"] = True
            state["workflow_error"] = error_msg
            return True
        
        # 检查视频URI是否存在
        video_uri = state.get("video_uploaded_uri")
        if not video_uri:
            video_error = state.get("video_upload_error", "未知错误")
            error_msg = f"视频URI不存在{('（' + error_source + '）' if error_source else '')}：{video_error}"
            logger.error(f"{error_msg}，终止workflow")
            state["workflow_failed"] = True
            state["workflow_error"] = error_msg
            return True
        
        return False

    def _check_agent_result_for_errors(self, result: Dict[str, Any], agent_name: str) -> bool:
        """检查Agent返回结果中是否包含关键错误
        
        Args:
            result: Agent返回的结果字典
            agent_name: Agent名称（用于日志）
            
        Returns:
            True 如果存在关键错误，False 否则
        """
        if not isinstance(result, dict):
            return False
        
        # 检查常见的错误字段
        error_fields = ["error", "错误", "video_upload_error"]
        for field in error_fields:
            error_value = result.get(field)
            if error_value:
                # 检查是否是关键错误（无法获取视频文件等）
                error_str = str(error_value)
                if "无法获取视频文件" in error_str or "无法获取视频文件对象" in error_str:
                    logger.error(f"{agent_name}返回关键错误: {error_str}")
                    return True
        
        # 检查metadata中的错误
        metadata = result.get("metadata", {})
        if isinstance(metadata, dict):
            error_value = metadata.get("error")
            if error_value:
                error_str = str(error_value)
                if "无法获取视频文件" in error_str or "无法获取视频文件对象" in error_str or "未找到视频URI" in error_str:
                    logger.error(f"{agent_name}返回关键错误: {error_str}")
                    return True
        
        return False

    def _check_workflow_status(self, state: Dict[str, Any]) -> str:
        """检查workflow状态，用于条件边
        
        Returns:
            "continue" 如果继续执行，否则返回 "terminate"
        """
        if state.get("workflow_failed"):
            return "terminate"
        return "continue"

    def _video_upload_node(self, state: Dict[str, Any]) -> Dict[str, Any]:
        """节点：视频上传（第一步）- 下载视频并上传至Gemini"""
        logger.info("=== 执行节点：视频上传 ===")

        try:
            # 初始化Function
            if not self.video_upload_func.is_initialized:
                self.video_upload_func.initialize()

            # 执行视频上传
            result = self.video_upload_func.execute(state)

            # 更新状态
            state.update(result)

            video_uri = result.get("video_uploaded_uri")
            if video_uri:
                logger.info(f"视频上传完成 - URI: {video_uri}")
            else:
                error = result.get("video_upload_error", "未知错误")
                logger.error(f"视频上传失败: {error}")
                # 确保失败信息被设置
                state.update({
                    "video_uploaded_uri": None,
                    "video_upload_error": error
                })

        except Exception as e:
            logger.error(f"视频上传失败: {e}", exc_info=True)
            state.update({
                "video_uploaded_uri": None,
                "video_upload_error": str(e)
            })

        return state

    def _inspiration_points_node(self, state: Dict[str, Any]) -> Dict[str, Any]:
        """节点：灵感点提取（What解构）"""
        logger.info("=== 执行节点：灵感点提取 ===")

        # 检查关键错误
        if self._check_critical_error(state, "灵感点提取"):
            return state

        try:
            # 初始化Agent
            if not self.inspiration_points_agent.is_initialized:
                self.inspiration_points_agent.initialize()

            # 执行Agent
            result = self.inspiration_points_agent.process(state)

            # 更新状态
            state.update(result)

            # 检查Agent返回结果中是否包含关键错误
            if self._check_agent_result_for_errors(result, "灵感点提取Agent"):
                error_msg = "灵感点提取失败：无法获取视频文件"
                state["workflow_failed"] = True
                state["workflow_error"] = error_msg
                return state

            # 安全地获取灵感点数量：total_count 在 metadata 中
            if isinstance(result, dict):
                metadata = result.get("metadata", {})
                inspiration_count = metadata.get("total_count", 0) if isinstance(metadata, dict) else 0
                # 如果 metadata 中没有，尝试从 inspiration_points 列表长度获取
                if inspiration_count == 0:
                    inspiration_points = result.get("inspiration_points", [])
                    if isinstance(inspiration_points, list):
                        inspiration_count = len(inspiration_points)
            else:
                # 如果 result 不是 dict（比如是列表），尝试获取长度
                inspiration_count = len(result) if isinstance(result, list) else 0
            
            logger.info(f"灵感点提取完成 - 共 {inspiration_count} 个灵感点")

        except Exception as e:
            logger.error(f"灵感点提取失败: {e}", exc_info=True)
            state["workflow_failed"] = True
            state["workflow_error"] = f"灵感点提取异常: {str(e)}"
            state.update({
                "inspiration_points": {
                    "error": str(e),
                    "points": [],
                    "total_count": 0
                }
            })

        return state

    def _purpose_point_node(self, state: Dict[str, Any]) -> Dict[str, Any]:
        """节点：目的点提取（What解构）"""
        logger.info("=== 执行节点：目的点提取 ===")

        # 检查关键错误
        if self._check_critical_error(state, "目的点提取"):
            return state

        try:
            # 初始化Agent
            if not self.purpose_point_agent.is_initialized:
                self.purpose_point_agent.initialize()

            # 执行Agent
            result = self.purpose_point_agent.process(state)

            # 更新状态
            state.update(result)

            # 检查Agent返回结果中是否包含关键错误
            if self._check_agent_result_for_errors(result, "目的点提取Agent"):
                error_msg = "目的点提取失败：无法获取视频文件"
                state["workflow_failed"] = True
                state["workflow_error"] = error_msg
                return state

            main_purpose = result.get("purpose_point", {}).get("main_purpose", "未知")
            logger.info(f"目的点提取完成 - 主要目的: {main_purpose}")

        except Exception as e:
            logger.error(f"目的点提取失败: {e}", exc_info=True)
            state["workflow_failed"] = True
            state["workflow_error"] = f"目的点提取异常: {str(e)}"
            state.update({
                "purpose_point": {
                    "error": str(e),
                    "main_purpose": "未知"
                }
            })

        return state

    def _key_points_node(self, state: Dict[str, Any]) -> Dict[str, Any]:
        """节点：关键点提取（What解构）"""
        logger.info("=== 执行节点：关键点提取 ===")

        # 检查关键错误
        if self._check_critical_error(state, "关键点提取"):
            return state

        try:
            # 初始化Agent
            if not self.key_points_agent.is_initialized:
                self.key_points_agent.initialize()

            # 执行Agent
            result = self.key_points_agent.process(state)

            # 更新状态
            state.update(result)

            # 检查Agent返回结果中是否包含关键错误
            if self._check_agent_result_for_errors(result, "关键点提取Agent"):
                error_msg = "关键点提取失败：无法获取视频文件"
                state["workflow_failed"] = True
                state["workflow_error"] = error_msg
                return state

            total_key_points = result.get("key_points", {}).get("total_count", 0)
            logger.info(f"关键点提取完成 - 共 {total_key_points} 个关键点")

        except Exception as e:
            logger.error(f"关键点提取失败: {e}", exc_info=True)
            state["workflow_failed"] = True
            state["workflow_error"] = f"关键点提取异常: {str(e)}"
            state.update({
                "key_points": {
                    "error": str(e),
                    "creator_perspective": {"key_points": [], "summary": ""},
                    "consumer_perspective": {"key_points": [], "summary": ""}
                }
            })

        return state

    def _topic_selection_understanding_node(self, state: Dict[str, Any]) -> Dict[str, Any]:
        """节点：选题理解（What解构）"""
        logger.info("=== 执行节点：选题理解 ===")

        # 检查关键错误
        if self._check_critical_error(state, "选题理解"):
            return state

        try:
            # 初始化Agent
            if not self.topic_selection_understanding_agent.is_initialized:
                self.topic_selection_understanding_agent.initialize()

            # 执行Agent
            result = self.topic_selection_understanding_agent.process(state)

            # 更新状态
            state.update(result)

            # 检查Agent返回结果中是否包含关键错误
            if self._check_agent_result_for_errors(result, "选题理解Agent"):
                error_msg = "选题理解失败：无法获取视频文件"
                state["workflow_failed"] = True
                state["workflow_error"] = error_msg
                return state

            logger.info(f"选题理解完成 - result: {result}")

        except Exception as e:
            logger.error(f"选题理解失败: {e}", exc_info=True)
            state["workflow_failed"] = True
            state["workflow_error"] = f"选题理解异常: {str(e)}"
            state.update({
                "topic_selection_understanding": {
                    "错误": str(e)
                }
            })

        return state

    def _section_division_node(self, state: Dict[str, Any]) -> Dict[str, Any]:
        """节点：脚本段落划分（脚本理解）"""
        logger.info("=== 执行节点：脚本段落划分 ===")

        # 检查关键错误
        if self._check_critical_error(state, "脚本段落划分"):
            return state

        try:
            # 初始化Agent
            if not self.section_agent.is_initialized:
                self.section_agent.initialize()

            # 执行Agent
            result = self.section_agent.process(state)

            # 更新状态
            state.update(result)

            # 检查Agent返回结果中是否包含关键错误（段落划分如果没有视频文件会返回空结果，不算致命错误）
            sections = result.get("段落列表", [])
            content_category = result.get("内容品类", "未知")
            logger.info(f"脚本段落划分完成 - 内容品类: {content_category}, 段落数: {len(sections)}")

        except Exception as e:
            logger.error(f"脚本段落划分失败: {e}", exc_info=True)
            state["workflow_failed"] = True
            state["workflow_error"] = f"脚本段落划分异常: {str(e)}"
            state.update({
                "内容品类": "未知品类",
                "段落列表": []
            })

        return state

    def _substance_extraction_node(self, state: Dict[str, Any]) -> Dict[str, Any]:
        """节点：实质元素提取（脚本理解）"""
        logger.info("=== 执行节点：实质元素提取 ===")

        # 检查关键错误
        if self._check_critical_error(state, "实质元素提取"):
            return state

        try:
            # 初始化Agent
            if not self.substance_agent.is_initialized:
                self.substance_agent.initialize()

            # 准备状态：将段落列表包装到section_division字段中
            sections = state.get("段落列表", [])
            state["section_division"] = {"段落列表": sections}

            # 验证三点解构信息是否可用
            inspiration_points = state.get("inspiration_points", [])
            purpose_point = state.get("purpose_point", {})
            key_points = state.get("key_points", {})
            
            # 统计三点解构信息
            inspiration_count = len(inspiration_points) if isinstance(inspiration_points, list) else 0
            if not inspiration_count and isinstance(inspiration_points, dict):
                inspiration_count = len(inspiration_points.get("points", []))
            
            purpose_count = 0
            if isinstance(purpose_point, dict):
                purpose_count = len(purpose_point.get("purposes", []))
            elif isinstance(purpose_point, list):
                purpose_count = len(purpose_point)
            
            key_points_count = 0
            if isinstance(key_points, dict):
                key_points_list = key_points.get("key_points", [])
                key_points_count = len(key_points_list) if isinstance(key_points_list, list) else 0
            elif isinstance(key_points, list):
                key_points_count = len(key_points)
            
            logger.info(
                f"实质提取节点 - 三点解构信息检查: "
                f"灵感点={inspiration_count}, 目的点={purpose_count}, 关键点={key_points_count}"
            )

            # 执行Agent
            result = self.substance_agent.process(state)

            # 更新状态
            state.update(result)

            # 检查Agent返回结果中是否包含关键错误
            if self._check_agent_result_for_errors(result, "实质元素提取Agent"):
                error_msg = "实质元素提取失败：无法获取视频文件"
                state["workflow_failed"] = True
                state["workflow_error"] = error_msg
                return state

            final_elements = result.get("substance_final_elements", [])
            logger.info(f"实质元素提取完成 - 最终元素数: {len(final_elements)}")

        except Exception as e:
            logger.error(f"实质元素提取失败: {e}", exc_info=True)
            state["workflow_failed"] = True
            state["workflow_error"] = f"实质元素提取异常: {str(e)}"
            state.update({
                "concrete_elements": [],
                "concrete_concepts": [],
                "abstract_concepts": [],
                "substance_elements": [],
                "substance_analyzed_result": [],
                "substance_scored_result": {},
                "substance_filtered_ids": [],
                "substance_categorized_result": {},
                "substance_final_elements": []
            })

        return state

    def _form_extraction_node(self, state: Dict[str, Any]) -> Dict[str, Any]:
        """节点：形式元素提取（脚本理解）"""
        logger.info("=== 执行节点：形式元素提取 ===")

        # 检查关键错误
        if self._check_critical_error(state, "形式元素提取"):
            return state

        try:
            # 初始化Agent
            if not self.form_agent.is_initialized:
                self.form_agent.initialize()

            # 验证三点解构信息是否可用
            inspiration_points = state.get("inspiration_points", [])
            purpose_point = state.get("purpose_point", {})
            key_points = state.get("key_points", {})
            
            # 统计三点解构信息
            inspiration_count = len(inspiration_points) if isinstance(inspiration_points, list) else 0
            if not inspiration_count and isinstance(inspiration_points, dict):
                inspiration_count = len(inspiration_points.get("points", []))
            
            purpose_count = 0
            if isinstance(purpose_point, dict):
                purpose_count = len(purpose_point.get("purposes", []))
            elif isinstance(purpose_point, list):
                purpose_count = len(purpose_point)
            
            key_points_count = 0
            if isinstance(key_points, dict):
                key_points_list = key_points.get("key_points", [])
                key_points_count = len(key_points_list) if isinstance(key_points_list, list) else 0
            elif isinstance(key_points, list):
                key_points_count = len(key_points)
            
            logger.info(
                f"形式提取节点 - 三点解构信息检查: "
                f"灵感点={inspiration_count}, 目的点={purpose_count}, 关键点={key_points_count}"
            )

            # 执行Agent（依赖实质元素）
            result = self.form_agent.process(state)

            # 更新状态
            state.update(result)

            # 检查Agent返回结果中是否包含关键错误
            if self._check_agent_result_for_errors(result, "形式元素提取Agent"):
                error_msg = "形式元素提取失败：无法获取视频文件"
                state["workflow_failed"] = True
                state["workflow_error"] = error_msg
                return state

            final_elements = result.get("form_final_elements", [])
            logger.info(f"形式元素提取完成 - 最终元素数: {len(final_elements)}")

        except Exception as e:
            logger.error(f"形式元素提取失败: {e}", exc_info=True)
            state["workflow_failed"] = True
            state["workflow_error"] = f"形式元素提取异常: {str(e)}"
            state.update({
                "concrete_element_forms": [],
                "concrete_concept_forms": [],
                "overall_forms": [],
                "form_elements": [],
                "form_analyzed_result": [],
                "form_scored_result": {},
                "form_weighted_result": {},
                "form_filtered_ids": [],
                "form_categorized_result": {},
                "form_final_elements": []
            })

        return state

    def _merge_all_results_node(self, state: Dict[str, Any]) -> Dict[str, Any]:
        """节点：分离实质和形式结果（脚本理解）"""
        logger.info("=== 执行节点：分离实质和形式结果 ===")

        try:
            # 获取实质和形式的最终元素
            substance_final_elements = state.get("substance_final_elements", [])
            form_final_elements = state.get("form_final_elements", [])

            # 分别存储实质列表和形式列表
            state["实质列表"] = substance_final_elements
            state["形式列表"] = form_final_elements

            logger.info(f"分离完成 - 实质元素: {len(substance_final_elements)}, 形式元素: {len(form_final_elements)}")

        except Exception as e:
            logger.error(f"分离结果失败: {e}", exc_info=True)
            state["实质列表"] = []
            state["形式列表"] = []

        return state

    def _result_aggregation_node(self, state: Dict[str, Any]) -> Dict[str, Any]:
        """节点：结果汇总"""
        logger.info("=== 执行节点：结果汇总 ===")

        try:
            # 初始化Function
            if not self.result_aggregation_func.is_initialized:
                self.result_aggregation_func.initialize()

            # 执行Function
            final_result = self.result_aggregation_func.execute(state)

            # 更新状态
            state["final_result"] = final_result

            logger.info("结果汇总完成")

        except Exception as e:
            logger.error(f"结果汇总失败: {e}", exc_info=True)
            state["final_result"] = {
                "视频信息": {},
                "三点解构": {
                    "灵感点": [],
                    "目的点": {},
                    "关键点": {}
                },
                "选题理解": {},
                "脚本理解": {
                    "内容品类": "未知",
                    "段落列表": [],
                    "实质列表": [],
                    "形式列表": []
                },
                "错误": f"汇总失败: {str(e)}"
            }

        return state

    def invoke(self, input_data: Dict[str, Any]) -> Dict[str, Any]:
        """执行工作流（公共接口）- 视频分析版本

        Returns:
            最终解码结果
        """
        logger.info("=== 开始执行解码工作流（视频分析） ===")

        # 确保工作流已初始化
        if not self.is_initialized:
            self.initialize()

        # 验证输入参数
        video_url = input_data.get("video", "")
        if not video_url:
            error_msg = "未提供视频URL，无法执行工作流"
            logger.error(error_msg)
            return {
                "error": error_msg,
                "workflow_status": "failed",
                "input_data": input_data
            }

        # 初始化状态（包含视频信息，供视频上传和后续Agent使用）
        initial_state = {
            "video": video_url,
            "channel_content_id": input_data.get("channel_content_id", ""),
            "title": input_data.get("title", ""),
            "current_depth": 0,
            "max_depth": self.max_depth,
            "task_id": input_data.get("task_id", ""),
        }

        # 执行工作流
        try:
            result = self.compiled_graph.invoke(initial_state)
        except Exception as e:
            error_msg = f"工作流执行异常: {str(e)}"
            logger.error(error_msg, exc_info=True)
            return {
                "error": error_msg,
                "workflow_status": "failed",
                "exception_type": type(e).__name__
            }

        # 检查是否因为错误而终止
        if result.get("workflow_failed"):
            error_msg = result.get("workflow_error", "工作流执行失败")
            logger.error(f"工作流因错误而终止: {error_msg}")
            return {
                "error": error_msg,
                "video_upload_error": result.get("video_upload_error"),
                "workflow_status": "failed",
                "failed_at": result.get("failed_at", "unknown")
            }

        # 检查是否有最终结果
        final_result = result.get("final_result")
        if not final_result:
            # 如果没有最终结果，检查是否有错误信息
            if result.get("workflow_error"):
                error_msg = result.get("workflow_error", "工作流执行失败，未生成结果")
                logger.error(f"工作流执行失败: {error_msg}")
                return {
                    "error": error_msg,
                    "workflow_status": "failed"
                }
            else:
                logger.warning("工作流执行完成，但未生成最终结果")
                return {
                    "error": "工作流执行完成，但未生成最终结果",
                    "workflow_status": "incomplete",
                    "state": result
                }

        logger.info("=== 解码工作流执行完成（视频分析） ===")

        return final_result

