"""
here4_gps.launch.py — CubePilot Here4 RTK GPS full stack
Issue: https://gitea.vayrette.com/seb/saltylab-firmware/issues/725

Launches:
  - dronecan_gps_node   (saltybot_dronecan_gps)
  - ntrip_client_node   (saltybot_ntrip_client)

Usage (minimal):
  ros2 launch saltybot_dronecan_gps here4_gps.launch.py \\
    ntrip_mount:=RTCM3_GENERIC ntrip_user:=you@email.com

Full options:
  ros2 launch saltybot_dronecan_gps here4_gps.launch.py \\
    can_interface:=can0 \\
    can_bitrate:=1000000 \\
    ntrip_caster:=rtk2go.com \\
    ntrip_port:=2101 \\
    ntrip_mount:=MYBASE \\
    ntrip_user:=you@email.com \\
    ntrip_password:=secret
"""

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:
    gps_cfg = os.path.join(
        get_package_share_directory('saltybot_dronecan_gps'),
        'config', 'dronecan_gps_params.yaml',
    )
    ntrip_cfg = os.path.join(
        get_package_share_directory('saltybot_ntrip_client'),
        'config', 'ntrip_params.yaml',
    )

    return LaunchDescription([
        # ── Shared CAN args ───────────────────────────────────────────────────
        DeclareLaunchArgument(
            'can_interface', default_value='can0',
            description='SocketCAN interface name',
        ),
        DeclareLaunchArgument(
            'can_bitrate', default_value='1000000',
            description='CAN bus bitrate — Here4 default is 1000000 (1 Mbps)',
        ),

        # ── NTRIP args ────────────────────────────────────────────────────────
        DeclareLaunchArgument(
            'ntrip_caster', default_value='rtk2go.com',
            description='NTRIP caster hostname',
        ),
        DeclareLaunchArgument(
            'ntrip_port', default_value='2101',
            description='NTRIP caster port',
        ),
        DeclareLaunchArgument(
            'ntrip_mount', default_value='',
            description='NTRIP mount point (REQUIRED)',
        ),
        DeclareLaunchArgument(
            'ntrip_user', default_value='',
            description='NTRIP username (rtk2go.com requires email address)',
        ),
        DeclareLaunchArgument(
            'ntrip_password', default_value='',
            description='NTRIP password',
        ),

        # ── DroneCAN GPS node ─────────────────────────────────────────────────
        Node(
            package='saltybot_dronecan_gps',
            executable='dronecan_gps_node',
            name='dronecan_gps',
            output='screen',
            parameters=[
                gps_cfg,
                {
                    'can_interface': LaunchConfiguration('can_interface'),
                    'can_bitrate':   LaunchConfiguration('can_bitrate'),
                },
            ],
        ),

        # ── NTRIP client node ─────────────────────────────────────────────────
        Node(
            package='saltybot_ntrip_client',
            executable='ntrip_client_node',
            name='ntrip_client',
            output='screen',
            parameters=[
                ntrip_cfg,
                {
                    'can_interface':  LaunchConfiguration('can_interface'),
                    'can_bitrate':    LaunchConfiguration('can_bitrate'),
                    'ntrip_caster':   LaunchConfiguration('ntrip_caster'),
                    'ntrip_port':     LaunchConfiguration('ntrip_port'),
                    'ntrip_mount':    LaunchConfiguration('ntrip_mount'),
                    'ntrip_user':     LaunchConfiguration('ntrip_user'),
                    'ntrip_password': LaunchConfiguration('ntrip_password'),
                },
            ],
        ),
    ])
