"""Tests for the Growatt Server number platform."""

from collections.abc import AsyncGenerator
from unittest.mock import patch

from freezegun.api import FrozenDateTimeFactory
from growattServer import GrowattV1ApiError, GrowattV1ApiErrorCode
import pytest
from syrupy.assertion import SnapshotAssertion

from homeassistant.components.growatt_server.coordinator import SCAN_INTERVAL
from homeassistant.components.number import (
    ATTR_VALUE,
    DOMAIN as NUMBER_DOMAIN,
    SERVICE_SET_VALUE,
)
from homeassistant.const import STATE_UNKNOWN, Platform
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import entity_registry as er

from tests.common import MockConfigEntry, async_fire_time_changed, snapshot_platform

DOMAIN = "growatt_server"


@pytest.fixture(autouse=True)
async def number_only() -> AsyncGenerator[None]:
    """Enable only the number platform."""
    with patch(
        "homeassistant.components.growatt_server.PLATFORMS",
        [Platform.NUMBER],
    ):
        yield


@pytest.mark.usefixtures("entity_registry_enabled_by_default", "init_integration")
async def test_number_entities(
    hass: HomeAssistant,
    snapshot: SnapshotAssertion,
    mock_config_entry: MockConfigEntry,
    entity_registry: er.EntityRegistry,
) -> None:
    """Test that number entities are created for MIN devices."""
    await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)


@pytest.mark.usefixtures("entity_registry_enabled_by_default", "init_integration")
async def test_set_number_value_api_error(
    hass: HomeAssistant,
    mock_growatt_v1_api,
) -> None:
    """Test handling API error when setting number value."""
    # Mock API to raise error
    mock_growatt_v1_api.min_write_parameter.side_effect = GrowattV1ApiError(
        message="API Error",
        error_code=GrowattV1ApiErrorCode.NO_PRIVILEGE,
        error_msg="API Error",
    )

    with pytest.raises(HomeAssistantError) as excinfo:
        await hass.services.async_call(
            NUMBER_DOMAIN,
            SERVICE_SET_VALUE,
            {
                "entity_id": "number.min123456_battery_charge_power_limit",
                ATTR_VALUE: 75,
            },
            blocking=True,
        )
    assert excinfo.value.translation_domain == "growatt_server"
    assert excinfo.value.translation_key == "api_error"
    assert "error" in excinfo.value.translation_placeholders


@pytest.mark.usefixtures("entity_registry_enabled_by_default", "init_integration")
async def test_all_number_entities_service_calls(
    hass: HomeAssistant,
    mock_growatt_v1_api,
) -> None:
    """Test service calls work for all number entities."""
    # Test all five number entities
    test_cases = [
        ("number.min123456_battery_charge_power_limit", "charge_power", 75),
        ("number.min123456_battery_charge_soc_limit", "charge_stop_soc", 85),
        ("number.min123456_battery_discharge_power_limit", "discharge_power", 90),
        (
            "number.min123456_battery_discharge_soc_limit_off_grid",
            "discharge_stop_soc",
            25,
        ),
        (
            "number.min123456_battery_discharge_soc_limit_on_grid",
            "on_grid_discharge_stop_soc",
            30,
        ),
    ]

    for entity_id, expected_write_key, test_value in test_cases:
        mock_growatt_v1_api.reset_mock()

        await hass.services.async_call(
            NUMBER_DOMAIN,
            SERVICE_SET_VALUE,
            {"entity_id": entity_id, ATTR_VALUE: test_value},
            blocking=True,
        )

        # Verify API was called with correct parameters
        mock_growatt_v1_api.min_write_parameter.assert_called_once_with(
            "MIN123456", expected_write_key, test_value
        )


@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_number_missing_data(
    hass: HomeAssistant,
    mock_growatt_v1_api,
    mock_config_entry: MockConfigEntry,
) -> None:
    """Test number entity when coordinator data is missing."""
    # Set up API with missing data for one entity
    mock_growatt_v1_api.min_detail.return_value = {
        "deviceSn": "MIN123456",
        # Missing 'chargePowerCommand' key to test None case
        "wchargeSOCLowLimit": 10,
        "disChargePowerCommand": 80,
        "wdisChargeSOCLowLimit": 20,
        "onGridDischargeStopSOC": 15,
    }

    mock_config_entry.add_to_hass(hass)

    assert await hass.config_entries.async_setup(mock_config_entry.entry_id)
    await hass.async_block_till_done()

    # Entity should exist but have unknown state due to missing data
    state = hass.states.get("number.min123456_battery_charge_power_limit")
    assert state is not None
    assert state.state == STATE_UNKNOWN


@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_no_number_entities_for_non_min_devices(
    hass: HomeAssistant,
    mock_growatt_v1_api,
    mock_config_entry: MockConfigEntry,
    entity_registry: er.EntityRegistry,
) -> None:
    """Test that number entities are not created for non-MIN devices."""
    # Mock a different device type (not MIN) - type 7 is MIN, type 8 is non-MIN
    mock_growatt_v1_api.device_list.return_value = {
        "devices": [
            {
                "device_sn": "TLX123456",
                "type": 8,  # Non-MIN device type (MIN is type 7)
            }
        ]
    }

    # Mock TLX API response to prevent coordinator errors
    mock_growatt_v1_api.tlx_detail.return_value = {"data": {}}

    mock_config_entry.add_to_hass(hass)

    assert await hass.config_entries.async_setup(mock_config_entry.entry_id)
    await hass.async_block_till_done()

    # Should have no number entities for TLX devices
    entity_entries = er.async_entries_for_config_entry(
        entity_registry, mock_config_entry.entry_id
    )
    number_entities = [entry for entry in entity_entries if entry.domain == "number"]
    assert len(number_entities) == 0


@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_no_number_entities_for_classic_api(
    hass: HomeAssistant,
    mock_growatt_classic_api,
    mock_config_entry_classic: MockConfigEntry,
    entity_registry: er.EntityRegistry,
) -> None:
    """Test that number entities are not created for Classic API."""
    # Mock device list to return no devices
    mock_growatt_classic_api.device_list.return_value = []

    mock_config_entry_classic.add_to_hass(hass)

    assert await hass.config_entries.async_setup(mock_config_entry_classic.entry_id)
    await hass.async_block_till_done()

    # Should have no number entities for classic API (no devices)
    entity_entries = er.async_entries_for_config_entry(
        entity_registry, mock_config_entry_classic.entry_id
    )
    number_entities = [entry for entry in entity_entries if entry.domain == "number"]
    assert len(number_entities) == 0


@pytest.mark.usefixtures("entity_registry_enabled_by_default", "init_integration")
async def test_float_to_int_conversion(
    hass: HomeAssistant,
    mock_growatt_v1_api,
) -> None:
    """Test that float values are converted to integers when setting."""
    # Test setting a float value gets converted to int
    await hass.services.async_call(
        NUMBER_DOMAIN,
        SERVICE_SET_VALUE,
        {"entity_id": "number.min123456_battery_charge_power_limit", ATTR_VALUE: 75.7},
        blocking=True,
    )

    # Verify API was called with integer value
    mock_growatt_v1_api.min_write_parameter.assert_called_once_with(
        "MIN123456",
        "charge_power",
        75,  # Should be converted to int
    )


@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_number_coordinator_data_update(
    hass: HomeAssistant,
    mock_growatt_v1_api,
    mock_config_entry: MockConfigEntry,
    freezer: FrozenDateTimeFactory,
) -> None:
    """Test that number state updates when coordinator data changes."""
    # Set up integration
    mock_config_entry.add_to_hass(hass)
    await hass.config_entries.async_setup(mock_config_entry.entry_id)
    await hass.async_block_till_done()

    # Initial state should be 50 (based on mock data)
    state = hass.states.get("number.min123456_battery_charge_power_limit")
    assert state is not None
    assert float(state.state) == 50.0

    # Change mock data and trigger coordinator update
    mock_growatt_v1_api.min_detail.return_value = {
        "deviceSn": "MIN123456",
        "chargePowerCommand": 75,  # Changed value
        "wchargeSOCLowLimit": 10,
        "disChargePowerCommand": 80,
        "wdisChargeSOCLowLimit": 20,
        "onGridDischargeStopSOC": 15,
    }

    # Advance time to trigger coordinator refresh
    freezer.tick(SCAN_INTERVAL)
    async_fire_time_changed(hass)
    await hass.async_block_till_done(wait_background_tasks=True)

    # State should now be 75
    state = hass.states.get("number.min123456_battery_charge_power_limit")
    assert state is not None
    assert float(state.state) == 75.0
