"""Launch health monitor node."""

import os
from ament_index_python.packages import get_package_share_directory
from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument
from launch.substitutions import LaunchConfiguration
from launch_ros.actions import Node


def generate_launch_description():
    """Generate launch description for health monitor."""

    package_dir = get_package_share_directory("saltybot_health_monitor")
    config_dir = os.path.join(package_dir, "config")

    # Launch arguments
    config_file_arg = DeclareLaunchArgument(
        "config_file",
        default_value=os.path.join(config_dir, "health_config.yaml"),
        description="Path to health monitor configuration YAML file",
    )

    heartbeat_timeout_arg = DeclareLaunchArgument(
        "heartbeat_timeout",
        default_value="5.0",
        description="Heartbeat timeout in seconds (node marked DOWN if silent longer)",
    )

    enable_auto_restart_arg = DeclareLaunchArgument(
        "enable_auto_restart",
        default_value="true",
        description="Enable automatic restart of dead nodes",
    )

    # Health monitor node
    health_monitor_node = Node(
        package="saltybot_health_monitor",
        executable="health_monitor_node",
        name="health_monitor",
        output="screen",
        parameters=[
            {
                "config_file": LaunchConfiguration("config_file"),
                "heartbeat_timeout": LaunchConfiguration("heartbeat_timeout"),
                "enable_auto_restart": LaunchConfiguration("enable_auto_restart"),
                "check_frequency": 1.0,  # Hz
            }
        ],
    )

    return LaunchDescription([
        config_file_arg,
        heartbeat_timeout_arg,
        enable_auto_restart_arg,
        health_monitor_node,
    ])
