"""
map_manager.launch.py — Launch the map manager node alongside RTAB-Map.

Usage:
    ros2 launch saltybot_mapping map_manager.launch.py
    ros2 launch saltybot_mapping map_manager.launch.py autosave_interval:=120.0
    ros2 launch saltybot_mapping map_manager.launch.py maps_dir:=/mnt/nvme/saltybot/maps

Map management commands (once running):
    # List saved maps
    ros2 service call /mapping/maps/list saltybot_mapping/srv/ListMaps

    # Save current map as 'home_garden'
    ros2 service call /mapping/maps/save_as saltybot_mapping/srv/SaveMap \\
        '{map_name: home_garden, overwrite: false}'

    # Load a saved map (restart rtabmap after)
    ros2 service call /mapping/maps/load saltybot_mapping/srv/LoadMap \\
        '{map_name: home_garden}'

    # Export 2D occupancy grid
    ros2 service call /mapping/export/occupancy saltybot_mapping/srv/ExportOccupancy \\
        '{output_dir: /tmp, map_name: home}'

    # Export 3D point cloud as PLY
    ros2 service call /mapping/export/pointcloud saltybot_mapping/srv/ExportPointCloud \\
        '{output_dir: /tmp, map_name: home, format: ply}'

    # Monitor map status
    ros2 topic echo /mapping/status
"""

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


def generate_launch_description():
    pkg_dir = get_package_share_directory('saltybot_mapping')
    default_config = os.path.join(pkg_dir, 'config', 'map_manager_params.yaml')

    return LaunchDescription([
        DeclareLaunchArgument(
            'maps_dir',
            default_value='/mnt/nvme/saltybot/maps',
            description='Directory for RTAB-Map .db files',
        ),
        DeclareLaunchArgument(
            'exports_dir',
            default_value='/mnt/nvme/saltybot/maps/exports',
            description='Directory for exported PGM/PLY/PCD files',
        ),
        DeclareLaunchArgument(
            'autosave_interval',
            default_value='300.0',
            description='Auto-save interval in seconds (default 5 min)',
        ),
        DeclareLaunchArgument(
            'keep_autosaves_n',
            default_value='5',
            description='Number of autosave files to retain (older pruned)',
        ),
        DeclareLaunchArgument(
            'max_db_size_mb',
            default_value='2048',
            description='Warn when current.db exceeds this size (MB)',
        ),

        Node(
            package='saltybot_mapping',
            executable='map_manager',
            name='map_manager',
            output='screen',
            parameters=[
                default_config,
                {
                    'maps_dir':          LaunchConfiguration('maps_dir'),
                    'exports_dir':       LaunchConfiguration('exports_dir'),
                    'autosave_interval': LaunchConfiguration('autosave_interval'),
                    'keep_autosaves_n':  LaunchConfiguration('keep_autosaves_n'),
                    'max_db_size_mb':    LaunchConfiguration('max_db_size_mb'),
                },
            ],
        ),
    ])
