"""Tests for the NRGkick number platform."""

from datetime import timedelta
from unittest.mock import AsyncMock

from freezegun.api import FrozenDateTimeFactory
from nrgkick_api import NRGkickCommandRejectedError
from nrgkick_api.const import (
    CONTROL_KEY_CURRENT_SET,
    CONTROL_KEY_ENERGY_LIMIT,
    CONTROL_KEY_PHASE_COUNT,
)
import pytest
from syrupy.assertion import SnapshotAssertion

from homeassistant.components.nrgkick.const import DEFAULT_SCAN_INTERVAL
from homeassistant.components.number import (
    ATTR_VALUE,
    DOMAIN as NUMBER_DOMAIN,
    SERVICE_SET_VALUE,
)
from homeassistant.const import ATTR_ENTITY_ID, Platform
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import entity_registry as er

from . import setup_integration

from tests.common import MockConfigEntry, async_fire_time_changed, snapshot_platform

SCAN_INTERVAL = timedelta(seconds=DEFAULT_SCAN_INTERVAL)

pytestmark = pytest.mark.usefixtures("entity_registry_enabled_by_default")


async def test_number_entities(
    hass: HomeAssistant,
    mock_config_entry: MockConfigEntry,
    mock_nrgkick_api: AsyncMock,
    entity_registry: er.EntityRegistry,
    snapshot: SnapshotAssertion,
) -> None:
    """Test number entities."""
    await setup_integration(hass, mock_config_entry, platforms=[Platform.NUMBER])

    await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)


async def test_set_charging_current(
    hass: HomeAssistant,
    mock_config_entry: MockConfigEntry,
    mock_nrgkick_api: AsyncMock,
) -> None:
    """Test setting charging current calls the API and updates state."""
    await setup_integration(hass, mock_config_entry, platforms=[Platform.NUMBER])

    entity_id = "number.nrgkick_test_charging_current"

    assert (state := hass.states.get(entity_id))
    assert state.state == "16.0"

    # Set current to 10A
    control_data = mock_nrgkick_api.get_control.return_value.copy()
    control_data[CONTROL_KEY_CURRENT_SET] = 10.0
    mock_nrgkick_api.get_control.return_value = control_data
    await hass.services.async_call(
        NUMBER_DOMAIN,
        SERVICE_SET_VALUE,
        {ATTR_ENTITY_ID: entity_id, ATTR_VALUE: 10.0},
        blocking=True,
    )
    assert (state := hass.states.get(entity_id))
    assert state.state == "10.0"

    mock_nrgkick_api.set_current.assert_awaited_once_with(10.0)


async def test_set_energy_limit(
    hass: HomeAssistant,
    mock_config_entry: MockConfigEntry,
    mock_nrgkick_api: AsyncMock,
) -> None:
    """Test setting energy limit calls the API and updates state."""
    await setup_integration(hass, mock_config_entry, platforms=[Platform.NUMBER])

    entity_id = "number.nrgkick_test_energy_limit"

    assert (state := hass.states.get(entity_id))
    assert state.state == "0"

    # Set energy limit to 5000 Wh
    control_data = mock_nrgkick_api.get_control.return_value.copy()
    control_data[CONTROL_KEY_ENERGY_LIMIT] = 5000
    mock_nrgkick_api.get_control.return_value = control_data
    await hass.services.async_call(
        NUMBER_DOMAIN,
        SERVICE_SET_VALUE,
        {ATTR_ENTITY_ID: entity_id, ATTR_VALUE: 5000},
        blocking=True,
    )
    assert (state := hass.states.get(entity_id))
    assert state.state == "5000"

    mock_nrgkick_api.set_energy_limit.assert_awaited_once_with(5000)


async def test_set_phase_count(
    hass: HomeAssistant,
    mock_config_entry: MockConfigEntry,
    mock_nrgkick_api: AsyncMock,
) -> None:
    """Test setting phase count calls the API and updates state."""
    await setup_integration(hass, mock_config_entry, platforms=[Platform.NUMBER])

    entity_id = "number.nrgkick_test_phase_count"

    assert (state := hass.states.get(entity_id))
    assert state.state == "3"

    # Set phase count to 1
    control_data = mock_nrgkick_api.get_control.return_value.copy()
    control_data[CONTROL_KEY_PHASE_COUNT] = 1
    mock_nrgkick_api.get_control.return_value = control_data
    await hass.services.async_call(
        NUMBER_DOMAIN,
        SERVICE_SET_VALUE,
        {ATTR_ENTITY_ID: entity_id, ATTR_VALUE: 1},
        blocking=True,
    )
    assert (state := hass.states.get(entity_id))
    assert state.state == "1"

    mock_nrgkick_api.set_phase_count.assert_awaited_once_with(1)


async def test_phase_count_filters_transient_zero_on_poll(
    hass: HomeAssistant,
    mock_config_entry: MockConfigEntry,
    mock_nrgkick_api: AsyncMock,
    freezer: FrozenDateTimeFactory,
) -> None:
    """Test that a transient phase count of 0 from a poll is filtered.

    During a phase-count switch the device briefly reports 0 phases.
    A coordinator refresh must not expose the transient value.
    """
    await setup_integration(hass, mock_config_entry, platforms=[Platform.NUMBER])

    entity_id = "number.nrgkick_test_phase_count"

    assert (state := hass.states.get(entity_id))
    assert state.state == "3"

    # One refresh happened during setup.
    assert mock_nrgkick_api.get_control.call_count == 1

    # Device briefly reports 0 during a phase switch.
    control_data = mock_nrgkick_api.get_control.return_value.copy()
    control_data[CONTROL_KEY_PHASE_COUNT] = 0
    mock_nrgkick_api.get_control.return_value = control_data
    freezer.tick(SCAN_INTERVAL)
    async_fire_time_changed(hass)
    await hass.async_block_till_done()

    # Verify the coordinator actually polled the device.
    assert mock_nrgkick_api.get_control.call_count == 2

    # The transient 0 must not surface; state stays at the previous value.
    assert (state := hass.states.get(entity_id))
    assert state.state == "3"

    # Once the device settles it reports the real phase count.
    control_data = mock_nrgkick_api.get_control.return_value.copy()
    control_data[CONTROL_KEY_PHASE_COUNT] = 1
    mock_nrgkick_api.get_control.return_value = control_data
    freezer.tick(SCAN_INTERVAL)
    async_fire_time_changed(hass)
    await hass.async_block_till_done()

    # Verify the coordinator polled again.
    assert mock_nrgkick_api.get_control.call_count == 3

    assert (state := hass.states.get(entity_id))
    assert state.state == "1"


async def test_phase_count_filters_transient_zero_on_service_call(
    hass: HomeAssistant,
    mock_config_entry: MockConfigEntry,
    mock_nrgkick_api: AsyncMock,
    freezer: FrozenDateTimeFactory,
) -> None:
    """Test that a service call keeps the cached value when refreshing returns 0.

    When the user sets a new phase count, the immediate refresh triggered
    by the service call may still see 0. The entity should keep the
    requested value instead.
    """
    await setup_integration(hass, mock_config_entry, platforms=[Platform.NUMBER])

    entity_id = "number.nrgkick_test_phase_count"

    assert (state := hass.states.get(entity_id))
    assert state.state == "3"

    # The refresh triggered by the service call will see 0.
    control_data = mock_nrgkick_api.get_control.return_value.copy()
    control_data[CONTROL_KEY_PHASE_COUNT] = 0
    mock_nrgkick_api.get_control.return_value = control_data

    await hass.services.async_call(
        NUMBER_DOMAIN,
        SERVICE_SET_VALUE,
        {ATTR_ENTITY_ID: entity_id, ATTR_VALUE: 1},
        blocking=True,
    )
    mock_nrgkick_api.set_phase_count.assert_awaited_once_with(1)

    # State must not show 0; the entity keeps the cached value.
    assert (state := hass.states.get(entity_id))
    assert state.state == "1"

    # Once the device settles it reports the real phase count again.
    control_data = mock_nrgkick_api.get_control.return_value.copy()
    control_data[CONTROL_KEY_PHASE_COUNT] = 1
    mock_nrgkick_api.get_control.return_value = control_data
    prior_call_count = mock_nrgkick_api.get_control.call_count
    freezer.tick(SCAN_INTERVAL)
    async_fire_time_changed(hass)
    await hass.async_block_till_done()

    # Verify that a periodic refresh actually occurred.
    assert mock_nrgkick_api.get_control.call_count > prior_call_count

    assert (state := hass.states.get(entity_id))
    assert state.state == "1"


async def test_number_command_rejected_by_device(
    hass: HomeAssistant,
    mock_config_entry: MockConfigEntry,
    mock_nrgkick_api: AsyncMock,
) -> None:
    """Test number entity surfaces device rejection messages."""
    await setup_integration(hass, mock_config_entry, platforms=[Platform.NUMBER])

    entity_id = "number.nrgkick_test_charging_current"

    mock_nrgkick_api.set_current.side_effect = NRGkickCommandRejectedError(
        "Current change blocked by solar-charging"
    )

    with pytest.raises(HomeAssistantError) as err:
        await hass.services.async_call(
            NUMBER_DOMAIN,
            SERVICE_SET_VALUE,
            {ATTR_ENTITY_ID: entity_id, ATTR_VALUE: 10.0},
            blocking=True,
        )

    assert err.value.translation_key == "command_rejected"
    assert err.value.translation_placeholders == {
        "reason": "Current change blocked by solar-charging"
    }

    # State should reflect actual device control data (unchanged).
    assert (state := hass.states.get(entity_id))
    assert state.state == "16.0"


async def test_charging_current_max_limited_by_connector(
    hass: HomeAssistant,
    mock_config_entry: MockConfigEntry,
    mock_nrgkick_api: AsyncMock,
) -> None:
    """Test that the charging current max is limited by the connector."""
    # Device rated at 32A, but connector only supports 16A.
    mock_nrgkick_api.get_info.return_value["general"]["rated_current"] = 32.0
    mock_nrgkick_api.get_info.return_value["connector"]["max_current"] = 16.0

    await setup_integration(hass, mock_config_entry, platforms=[Platform.NUMBER])

    entity_id = "number.nrgkick_test_charging_current"

    assert (state := hass.states.get(entity_id))
    assert state.attributes["max"] == 16.0
