"""
slam_toolbox_persistence.launch.py — Launch map persistence service for slam_toolbox.

Usage:
    ros2 launch saltybot_mapping slam_toolbox_persistence.launch.py

Parameters:
    maps_dir: Directory for saving/loading .posegraph files
    exports_dir: Directory for exported PGM/YAML files
    autosave_interval: Interval between auto-saves (seconds)
    autoload_on_startup: Whether to load most recent map on startup
    keep_autosaves_n: Number of autosave files to keep
"""

import os
from pathlib import Path
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')
    maps_dir = str(Path.home() / 'saltybot-data' / 'maps')
    exports_dir = str(Path.home() / 'saltybot-data' / 'maps' / 'exports')

    return LaunchDescription([
        DeclareLaunchArgument(
            'maps_dir',
            default_value=maps_dir,
            description='Directory for saving/loading maps',
        ),
        DeclareLaunchArgument(
            'exports_dir',
            default_value=exports_dir,
            description='Directory for exported maps (PGM/YAML/PLY)',
        ),
        DeclareLaunchArgument(
            'autosave_interval',
            default_value='300.0',
            description='Auto-save interval in seconds (default 5 min)',
        ),
        DeclareLaunchArgument(
            'autoload_on_startup',
            default_value='true',
            description='Auto-load most recent map on startup',
        ),
        DeclareLaunchArgument(
            'keep_autosaves_n',
            default_value='5',
            description='Number of autosaves to keep',
        ),

        Node(
            package='saltybot_mapping',
            executable='slam_toolbox_persistence.py',
            name='slam_toolbox_persistence',
            output='screen',
            parameters=[
                {
                    'maps_dir': LaunchConfiguration('maps_dir'),
                    'exports_dir': LaunchConfiguration('exports_dir'),
                    'autosave_interval': LaunchConfiguration('autosave_interval'),
                    'autoload_on_startup': LaunchConfiguration('autoload_on_startup'),
                    'keep_autosaves_n': LaunchConfiguration('keep_autosaves_n'),
                },
            ],
        ),
    ])
