"""Test Tuya diagnostics platform."""

import pytest
from syrupy.assertion import SnapshotAssertion
from syrupy.filters import props
from tuya_sharing import CustomerDevice, Manager

from homeassistant.components.tuya.const import DOMAIN
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr

from . import initialize_entry

from tests.common import MockConfigEntry
from tests.components.diagnostics import (
    get_diagnostics_for_config_entry,
    get_diagnostics_for_device,
)
from tests.typing import ClientSessionGenerator


@pytest.mark.parametrize("mock_device_code", ["rqbj_4iqe2hsfyd86kwwc"])
async def test_entry_diagnostics(
    hass: HomeAssistant,
    mock_manager: Manager,
    mock_config_entry: MockConfigEntry,
    mock_device: CustomerDevice,
    hass_client: ClientSessionGenerator,
    snapshot: SnapshotAssertion,
) -> None:
    """Test config entry diagnostics."""
    await initialize_entry(hass, mock_manager, mock_config_entry, mock_device)

    result = await get_diagnostics_for_config_entry(
        hass, hass_client, mock_config_entry
    )

    # Sort the lists of entities by entity_id to ensure consistent ordering
    # for snapshot testing
    for device in result["devices"]:
        device["home_assistant"]["entities"] = sorted(
            device["home_assistant"]["entities"], key=lambda x: x["state"]["entity_id"]
        )
    assert result == snapshot(
        exclude=props("last_changed", "last_reported", "last_updated")
    )


@pytest.mark.parametrize(
    "mock_device_code",
    [
        "rqbj_4iqe2hsfyd86kwwc",
        "mal_gyitctrjj1kefxp2",  # with redacted dpcodes
        "tdq_9htyiowaf5rtdhrv",  # with bad enum warnings
    ],
)
async def test_device_diagnostics(
    hass: HomeAssistant,
    mock_manager: Manager,
    mock_config_entry: MockConfigEntry,
    mock_device: CustomerDevice,
    hass_client: ClientSessionGenerator,
    device_registry: dr.DeviceRegistry,
    snapshot: SnapshotAssertion,
) -> None:
    """Test device diagnostics."""
    await initialize_entry(hass, mock_manager, mock_config_entry, mock_device)

    device = device_registry.async_get_device(identifiers={(DOMAIN, mock_device.id)})
    assert device, repr(device_registry.devices)

    result = await get_diagnostics_for_device(
        hass, hass_client, mock_config_entry, device
    )
    # Sort the list of entities by entity_id to ensure consistent ordering
    # for snapshot testing
    result["home_assistant"]["entities"] = sorted(
        result["home_assistant"]["entities"], key=lambda x: x["state"]["entity_id"]
    )
    assert result == snapshot(
        exclude=props("last_changed", "last_reported", "last_updated")
    )
