#!/usr/bin/env python3
"""
agint_memory.memory.launch - Memory Node 启动文件
"""

from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument, LogInfo, RegisterEventHandler
from launch.event_handlers import OnProcessExit
from launch.substitutions import LaunchConfiguration
from launch_ros.actions import Node


def generate_launch_description() -> LaunchDescription:

    # =========================================================================
    # Launch Arguments
    # =========================================================================
    input_asr_topic_arg = DeclareLaunchArgument(
        "input_asr_topic",
        default_value="/asr",
        description="ASR 语音识别输入 topic",
    )

    input_llm_response_topic_arg = DeclareLaunchArgument(
        "input_llm_response_topic",
        default_value="/llm_response",
        description="LLM 响应 topic（流式）",
    )

    input_plan_topic_arg = DeclareLaunchArgument(
        "input_plan_topic",
        default_value="/plan",
        description="Plan topic（可选）",
    )

    output_memory_context_topic_arg = DeclareLaunchArgument(
        "output_memory_context_topic",
        default_value="/memory/context",
        description="记忆上下文输出 topic",
    )

    session_id_arg = DeclareLaunchArgument(
        "session_id",
        default_value="default",
        description="会话 ID",
    )

    max_raw_memory_bytes_arg = DeclareLaunchArgument(
        "max_raw_memory_bytes",
        default_value="2097152",
        description="最大内存占用（2MB）",
    )

    short_memory_lines_arg = DeclareLaunchArgument(
        "short_memory_lines",
        default_value="15",
        description="短记忆行数",
    )

    assistant_flush_timeout_sec_arg = DeclareLaunchArgument(
        "assistant_flush_timeout_sec",
        default_value="1.0",
        description="助手回复 flush 超时（秒）",
    )

    publish_on_update_arg = DeclareLaunchArgument(
        "publish_on_update",
        default_value="true",
        description="是否每次更新后发布",
    )

    debug_log_arg = DeclareLaunchArgument(
        "debug_log",
        default_value="true",
        description="是否输出调试日志",
    )

    # =========================================================================
    # Memory Node
    # =========================================================================
    memory_node = Node(
        package="agint_memory",
        executable="memory_node",
        name="memory_node",
        output="screen",
        emulate_tty=True,
        parameters=[
            {
                "input_asr_topic": LaunchConfiguration("input_asr_topic"),
                "input_llm_response_topic": LaunchConfiguration("input_llm_response_topic"),
                "input_plan_topic": LaunchConfiguration("input_plan_topic"),
                "output_memory_context_topic": LaunchConfiguration("output_memory_context_topic"),
                "session_id": LaunchConfiguration("session_id"),
                "max_raw_memory_bytes": LaunchConfiguration("max_raw_memory_bytes"),
                "short_memory_lines": LaunchConfiguration("short_memory_lines"),
                "assistant_flush_timeout_sec": LaunchConfiguration("assistant_flush_timeout_sec"),
                "publish_on_update": LaunchConfiguration("publish_on_update"),
                "debug_log": LaunchConfiguration("debug_log"),
            }
        ],
    )

    # =========================================================================
    # Event Handlers
    # =========================================================================
    exit_event = RegisterEventHandler(
        event_handler=OnProcessExit(
            target_action=memory_node,
            on_exit=[LogInfo(msg="Memory Node 已退出")],
        )
    )

    # =========================================================================
    # Launch Description
    # =========================================================================
    ld = LaunchDescription([
        LogInfo(msg="========================================"),
        LogInfo(msg="正在启动 Memory Node..."),
        LogInfo(msg="========================================"),
        input_asr_topic_arg,
        input_llm_response_topic_arg,
        input_plan_topic_arg,
        output_memory_context_topic_arg,
        session_id_arg,
        max_raw_memory_bytes_arg,
        short_memory_lines_arg,
        assistant_flush_timeout_sec_arg,
        publish_on_update_arg,
        debug_log_arg,
        memory_node,
        exit_event,
    ])

    return ld
