"""Test assist satellite triggers."""

from typing import Any

import pytest

from homeassistant.components.assist_satellite.entity import AssistSatelliteState
from homeassistant.core import HomeAssistant

from tests.components.common import (
    TriggerStateDescription,
    assert_trigger_behavior_any,
    assert_trigger_behavior_first,
    assert_trigger_behavior_last,
    assert_trigger_gated_by_labs_flag,
    assert_trigger_options_supported,
    other_states,
    parametrize_target_entities,
    parametrize_trigger_states,
    target_entities,
)


@pytest.fixture
async def target_assist_satellites(hass: HomeAssistant) -> dict[str, list[str]]:
    """Create multiple assist satellite entities associated with different targets."""
    return await target_entities(hass, "assist_satellite")


@pytest.mark.parametrize(
    "trigger_key",
    [
        "assist_satellite.idle",
        "assist_satellite.listening",
        "assist_satellite.processing",
        "assist_satellite.responding",
    ],
)
async def test_assist_satellite_triggers_gated_by_labs_flag(
    hass: HomeAssistant, caplog: pytest.LogCaptureFixture, trigger_key: str
) -> None:
    """Test the Assist satellite triggers are gated by the labs flag."""
    await assert_trigger_gated_by_labs_flag(hass, caplog, trigger_key)


@pytest.mark.usefixtures("enable_labs_preview_features")
@pytest.mark.parametrize(
    ("trigger_key", "base_options", "supports_behavior", "supports_duration"),
    [
        ("assist_satellite.idle", {}, True, True),
        ("assist_satellite.listening", {}, True, True),
        ("assist_satellite.processing", {}, True, True),
        ("assist_satellite.responding", {}, True, True),
    ],
)
async def test_assist_satellite_trigger_options_validation(
    hass: HomeAssistant,
    trigger_key: str,
    base_options: dict[str, Any] | None,
    supports_behavior: bool,
    supports_duration: bool,
) -> None:
    """Test that assist_satellite triggers support the expected options."""
    await assert_trigger_options_supported(
        hass,
        trigger_key,
        base_options,
        supports_behavior=supports_behavior,
        supports_duration=supports_duration,
    )


@pytest.mark.usefixtures("enable_labs_preview_features")
@pytest.mark.parametrize(
    ("trigger_target_config", "entity_id", "entities_in_target"),
    parametrize_target_entities("assist_satellite"),
)
@pytest.mark.parametrize(
    ("trigger", "trigger_options", "states"),
    [
        *parametrize_trigger_states(
            trigger="assist_satellite.idle",
            target_states=[AssistSatelliteState.IDLE],
            other_states=other_states(AssistSatelliteState.IDLE),
        ),
        *parametrize_trigger_states(
            trigger="assist_satellite.listening",
            target_states=[AssistSatelliteState.LISTENING],
            other_states=other_states(AssistSatelliteState.LISTENING),
        ),
        *parametrize_trigger_states(
            trigger="assist_satellite.processing",
            target_states=[AssistSatelliteState.PROCESSING],
            other_states=other_states(AssistSatelliteState.PROCESSING),
        ),
        *parametrize_trigger_states(
            trigger="assist_satellite.responding",
            target_states=[AssistSatelliteState.RESPONDING],
            other_states=other_states(AssistSatelliteState.RESPONDING),
        ),
    ],
)
async def test_assist_satellite_state_trigger_behavior_any(
    hass: HomeAssistant,
    target_assist_satellites: dict[str, list[str]],
    trigger_target_config: dict,
    entity_id: str,
    entities_in_target: int,
    trigger: str,
    trigger_options: dict[str, Any],
    states: list[TriggerStateDescription],
) -> None:
    """Test that the assist satellite state trigger fires when any assist satellite state changes to a specific state."""
    await assert_trigger_behavior_any(
        hass,
        target_entities=target_assist_satellites,
        trigger_target_config=trigger_target_config,
        entity_id=entity_id,
        entities_in_target=entities_in_target,
        trigger=trigger,
        trigger_options=trigger_options,
        states=states,
    )


@pytest.mark.usefixtures("enable_labs_preview_features")
@pytest.mark.parametrize(
    ("trigger_target_config", "entity_id", "entities_in_target"),
    parametrize_target_entities("assist_satellite"),
)
@pytest.mark.parametrize(
    ("trigger", "trigger_options", "states"),
    [
        *parametrize_trigger_states(
            trigger="assist_satellite.idle",
            target_states=[AssistSatelliteState.IDLE],
            other_states=other_states(AssistSatelliteState.IDLE),
        ),
        *parametrize_trigger_states(
            trigger="assist_satellite.listening",
            target_states=[AssistSatelliteState.LISTENING],
            other_states=other_states(AssistSatelliteState.LISTENING),
        ),
        *parametrize_trigger_states(
            trigger="assist_satellite.processing",
            target_states=[AssistSatelliteState.PROCESSING],
            other_states=other_states(AssistSatelliteState.PROCESSING),
        ),
        *parametrize_trigger_states(
            trigger="assist_satellite.responding",
            target_states=[AssistSatelliteState.RESPONDING],
            other_states=other_states(AssistSatelliteState.RESPONDING),
        ),
    ],
)
async def test_assist_satellite_state_trigger_behavior_first(
    hass: HomeAssistant,
    target_assist_satellites: dict[str, list[str]],
    trigger_target_config: dict,
    entity_id: str,
    entities_in_target: int,
    trigger: str,
    trigger_options: dict[str, Any],
    states: list[TriggerStateDescription],
) -> None:
    """Test that the assist satellite state trigger fires when the first assist satellite changes to a specific state."""
    await assert_trigger_behavior_first(
        hass,
        target_entities=target_assist_satellites,
        trigger_target_config=trigger_target_config,
        entity_id=entity_id,
        entities_in_target=entities_in_target,
        trigger=trigger,
        trigger_options=trigger_options,
        states=states,
    )


@pytest.mark.usefixtures("enable_labs_preview_features")
@pytest.mark.parametrize(
    ("trigger_target_config", "entity_id", "entities_in_target"),
    parametrize_target_entities("assist_satellite"),
)
@pytest.mark.parametrize(
    ("trigger", "trigger_options", "states"),
    [
        *parametrize_trigger_states(
            trigger="assist_satellite.idle",
            target_states=[AssistSatelliteState.IDLE],
            other_states=other_states(AssistSatelliteState.IDLE),
        ),
        *parametrize_trigger_states(
            trigger="assist_satellite.listening",
            target_states=[AssistSatelliteState.LISTENING],
            other_states=other_states(AssistSatelliteState.LISTENING),
        ),
        *parametrize_trigger_states(
            trigger="assist_satellite.processing",
            target_states=[AssistSatelliteState.PROCESSING],
            other_states=other_states(AssistSatelliteState.PROCESSING),
        ),
        *parametrize_trigger_states(
            trigger="assist_satellite.responding",
            target_states=[AssistSatelliteState.RESPONDING],
            other_states=other_states(AssistSatelliteState.RESPONDING),
        ),
    ],
)
async def test_assist_satellite_state_trigger_behavior_last(
    hass: HomeAssistant,
    target_assist_satellites: dict[str, list[str]],
    trigger_target_config: dict,
    entity_id: str,
    entities_in_target: int,
    trigger: str,
    trigger_options: dict[str, Any],
    states: list[TriggerStateDescription],
) -> None:
    """Test that the assist_satellite state trigger fires when the last assist_satellite changes to a specific state."""
    await assert_trigger_behavior_last(
        hass,
        target_entities=target_assist_satellites,
        trigger_target_config=trigger_target_config,
        entity_id=entity_id,
        entities_in_target=entities_in_target,
        trigger=trigger,
        trigger_options=trigger_options,
        states=states,
    )
