#!/usr/bin/env python3
"""
mqtt_bridge.launch.py — Launch the MQTT-to-ROS2 phone sensor bridge (Issue #601)

Usage:
  ros2 launch saltybot_phone mqtt_bridge.launch.py
  ros2 launch saltybot_phone mqtt_bridge.launch.py mqtt_host:=192.168.1.100
  ros2 launch saltybot_phone mqtt_bridge.launch.py use_phone_timestamp:=true warn_drift_s:=0.5
"""
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:
    args = [
        DeclareLaunchArgument(
            "mqtt_host", default_value="localhost",
            description="MQTT broker IP/hostname",
        ),
        DeclareLaunchArgument(
            "mqtt_port", default_value="1883",
            description="MQTT broker port",
        ),
        DeclareLaunchArgument(
            "reconnect_delay_s", default_value="5.0",
            description="Seconds between MQTT reconnect attempts",
        ),
        DeclareLaunchArgument(
            "use_phone_timestamp", default_value="false",
            description="Use phone epoch ts for ROS2 header stamp",
        ),
        DeclareLaunchArgument(
            "warn_drift_s", default_value="1.0",
            description="Clock drift threshold for warning (s)",
        ),
        DeclareLaunchArgument(
            "imu_accel_cov", default_value="0.01",
            description="IMU accelerometer diagonal covariance (m/s²)²",
        ),
        DeclareLaunchArgument(
            "imu_gyro_cov", default_value="0.001",
            description="IMU gyroscope diagonal covariance (rad/s)²",
        ),
    ]

    bridge_node = Node(
        package="saltybot_phone",
        executable="mqtt_ros2_bridge",
        name="mqtt_ros2_bridge",
        parameters=[{
            "mqtt_host":           LaunchConfiguration("mqtt_host"),
            "mqtt_port":           LaunchConfiguration("mqtt_port"),
            "reconnect_delay_s":   LaunchConfiguration("reconnect_delay_s"),
            "frame_id_imu":        "phone_imu",
            "frame_id_gps":        "phone_gps",
            "use_phone_timestamp": LaunchConfiguration("use_phone_timestamp"),
            "warn_drift_s":        LaunchConfiguration("warn_drift_s"),
            "imu_accel_cov":       LaunchConfiguration("imu_accel_cov"),
            "imu_gyro_cov":        LaunchConfiguration("imu_gyro_cov"),
            "gps_alt_cov_factor":  4.0,
            "status_hz":           0.2,
        }],
        output="screen",
        emulate_tty=True,
    )

    return LaunchDescription(args + [bridge_node])
