"""social_bot.launch.py — Launch the full social-bot pipeline.

Issue #89: End-to-end pipeline orchestrator launch file.

Brings up all social-bot nodes in dependency order:
  1. Orchestrator (state machine + watchdog)  — t=0s
  2. Speech pipeline (wake word + VAD + STT)  — t=2s
  3. Conversation engine (LLM)                — t=4s
  4. TTS node (Piper streaming)               — t=4s
  5. Person state tracker (already in social.launch.py — optional)

Launch args:
  enable_speech       (bool, true)
  enable_llm          (bool, true)
  enable_tts          (bool, true)
  enable_orchestrator (bool, true)
  voice_path          (str, /models/piper/en_US-lessac-medium.onnx)
  whisper_model       (str, small)
  llm_model_path      (str, /models/gguf/phi-3-mini-4k-instruct-q4_k_m.gguf)
  n_gpu_layers        (int, 20)
"""

import os
from ament_index_python.packages import get_package_share_directory
from launch import LaunchDescription
from launch.actions import (
    DeclareLaunchArgument, GroupAction, LogInfo, TimerAction
)
from launch.conditions import IfCondition
from launch.substitutions import LaunchConfiguration, PythonExpression
from launch_ros.actions import Node


def generate_launch_description() -> LaunchDescription:
    pkg = get_package_share_directory("saltybot_social")
    cfg = os.path.join(pkg, "config")

    # ── Launch arguments ─────────────────────────────────────────────────────
    args = [
        DeclareLaunchArgument("enable_speech",       default_value="true"),
        DeclareLaunchArgument("enable_llm",          default_value="true"),
        DeclareLaunchArgument("enable_tts",          default_value="true"),
        DeclareLaunchArgument("enable_orchestrator", default_value="true"),
        DeclareLaunchArgument("voice_path",
                              default_value="/models/piper/en_US-lessac-medium.onnx"),
        DeclareLaunchArgument("whisper_model",       default_value="small"),
        DeclareLaunchArgument("llm_model_path",
                              default_value="/models/gguf/phi-3-mini-4k-instruct-q4_k_m.gguf"),
        DeclareLaunchArgument("n_gpu_layers",        default_value="20"),
    ]

    # ── Orchestrator (t=0s) ──────────────────────────────────────────────────
    orchestrator = Node(
        package="saltybot_social",
        executable="orchestrator_node",
        name="orchestrator_node",
        parameters=[os.path.join(cfg, "orchestrator_params.yaml")],
        condition=IfCondition(LaunchConfiguration("enable_orchestrator")),
        output="screen",
        emulate_tty=True,
    )

    # ── Speech pipeline (t=2s) ───────────────────────────────────────────────
    speech = TimerAction(period=2.0, actions=[
        GroupAction(
            condition=IfCondition(LaunchConfiguration("enable_speech")),
            actions=[
                LogInfo(msg="[social_bot] Starting speech pipeline..."),
                Node(
                    package="saltybot_social",
                    executable="speech_pipeline_node",
                    name="speech_pipeline_node",
                    parameters=[
                        os.path.join(cfg, "speech_params.yaml"),
                        {"whisper_model": LaunchConfiguration("whisper_model")},
                    ],
                    output="screen",
                    emulate_tty=True,
                ),
            ]
        )
    ])

    # ── LLM conversation engine (t=4s) ───────────────────────────────────────
    llm = TimerAction(period=4.0, actions=[
        GroupAction(
            condition=IfCondition(LaunchConfiguration("enable_llm")),
            actions=[
                LogInfo(msg="[social_bot] Starting LLM conversation engine..."),
                Node(
                    package="saltybot_social",
                    executable="conversation_node",
                    name="conversation_node",
                    parameters=[
                        os.path.join(cfg, "conversation_params.yaml"),
                        {
                            "model_path": LaunchConfiguration("llm_model_path"),
                            "n_gpu_layers": LaunchConfiguration("n_gpu_layers"),
                        },
                    ],
                    output="screen",
                    emulate_tty=True,
                ),
            ]
        )
    ])

    # ── TTS node (t=4s, parallel with LLM) ──────────────────────────────────
    tts = TimerAction(period=4.0, actions=[
        GroupAction(
            condition=IfCondition(LaunchConfiguration("enable_tts")),
            actions=[
                LogInfo(msg="[social_bot] Starting TTS node..."),
                Node(
                    package="saltybot_social",
                    executable="tts_node",
                    name="tts_node",
                    parameters=[
                        os.path.join(cfg, "tts_params.yaml"),
                        {"voice_path": LaunchConfiguration("voice_path")},
                    ],
                    output="screen",
                    emulate_tty=True,
                ),
            ]
        )
    ])

    return LaunchDescription(args + [orchestrator, speech, llm, tts])
