"""battery.launch.py — Launch the battery management node (Issue #125).

Usage:
  ros2 launch saltybot_bridge battery.launch.py

  # Custom thresholds:
  ros2 launch saltybot_bridge battery.launch.py \
    warn_pct:=25.0 critical_pct:=12.0 emergency_pct:=6.0

  # Custom DB path:
  ros2 launch saltybot_bridge battery.launch.py \
    db_path:=/mnt/nvme/saltybot/battery.db
"""

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() -> LaunchDescription:
    pkg = get_package_share_directory("saltybot_bridge")
    params_file = os.path.join(pkg, "config", "battery_params.yaml")

    return LaunchDescription([
        DeclareLaunchArgument("db_path",               default_value="/var/log/saltybot/battery.db"),
        DeclareLaunchArgument("cell_count",            default_value="3"),
        DeclareLaunchArgument("warn_pct",              default_value="20.0"),
        DeclareLaunchArgument("critical_pct",          default_value="10.0"),
        DeclareLaunchArgument("emergency_pct",         default_value="5.0"),
        DeclareLaunchArgument("warn_speed_factor",     default_value="0.6"),
        DeclareLaunchArgument("critical_speed_factor", default_value="0.3"),
        DeclareLaunchArgument("charge_detect_ma",      default_value="-100"),
        DeclareLaunchArgument("publish_rate_hz",       default_value="1.0"),

        Node(
            package="saltybot_bridge",
            executable="battery_node",
            name="battery_node",
            output="screen",
            emulate_tty=True,
            parameters=[
                params_file,
                {
                    "db_path":               LaunchConfiguration("db_path"),
                    "cell_count":            LaunchConfiguration("cell_count"),
                    "warn_pct":              LaunchConfiguration("warn_pct"),
                    "critical_pct":          LaunchConfiguration("critical_pct"),
                    "emergency_pct":         LaunchConfiguration("emergency_pct"),
                    "warn_speed_factor":     LaunchConfiguration("warn_speed_factor"),
                    "critical_speed_factor": LaunchConfiguration("critical_speed_factor"),
                    "charge_detect_ma":      LaunchConfiguration("charge_detect_ma"),
                    "publish_rate_hz":       LaunchConfiguration("publish_rate_hz"),
                },
            ],
        ),
    ])
