"""Test forecast solar energy platform."""

from datetime import UTC, datetime
from unittest.mock import MagicMock

from homeassistant.components.forecast_solar import energy
from homeassistant.config_entries import ConfigEntryState
from homeassistant.core import HomeAssistant

from tests.common import MockConfigEntry


async def test_energy_solar_forecast(
    hass: HomeAssistant,
    mock_config_entry: MockConfigEntry,
    mock_forecast_solar: MagicMock,
) -> None:
    """Test the Forecast.Solar energy platform solar forecast."""
    mock_forecast_solar.estimate.return_value.wh_period = {
        datetime(2021, 6, 27, 13, 0, tzinfo=UTC): 12,
        datetime(2021, 6, 27, 14, 0, tzinfo=UTC): 8,
    }

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

    assert mock_config_entry.state is ConfigEntryState.LOADED

    assert await energy.async_get_solar_forecast(hass, mock_config_entry.entry_id) == {
        "wh_hours": {
            "2021-06-27T13:00:00+00:00": 12,
            "2021-06-27T14:00:00+00:00": 8,
        }
    }


async def test_energy_solar_forecast_filters_midnight_utc_zeros(
    hass: HomeAssistant,
    mock_config_entry: MockConfigEntry,
    mock_forecast_solar: MagicMock,
) -> None:
    """Test that artificial zero values at UTC midnight boundaries are filtered out."""
    mock_forecast_solar.estimate.return_value.wh_period = {
        datetime(2021, 6, 27, 0, 0, tzinfo=UTC): 0,
        datetime(2021, 6, 27, 1, 0, tzinfo=UTC): 1388,
        datetime(2021, 6, 27, 2, 0, tzinfo=UTC): 830,
        datetime(2021, 6, 27, 14, 0, tzinfo=UTC): 0,
        datetime(2021, 6, 27, 15, 0, tzinfo=UTC): 292,
        datetime(2021, 6, 28, 0, 0, tzinfo=UTC): 0,
    }

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

    assert mock_config_entry.state is ConfigEntryState.LOADED

    result = await energy.async_get_solar_forecast(hass, mock_config_entry.entry_id)
    assert result == {
        "wh_hours": {
            "2021-06-27T01:00:00+00:00": 1388,
            "2021-06-27T02:00:00+00:00": 830,
            "2021-06-27T14:00:00+00:00": 0,
            "2021-06-27T15:00:00+00:00": 292,
        }
    }


async def test_energy_solar_forecast_invalid_id(hass: HomeAssistant) -> None:
    """Test the Forecast.Solar energy platform with invalid config entry ID."""
    assert await energy.async_get_solar_forecast(hass, "invalid_id") is None
