"""Real-power logic for the config flow."""

from typing import TYPE_CHECKING, Any

from homeassistant.components.sensor import SensorDeviceClass
from homeassistant.config_entries import ConfigFlowResult
from homeassistant.const import CONF_DEVICE, CONF_ENTITY_ID, CONF_NAME
from homeassistant.helpers import selector
import voluptuous as vol

from custom_components.powercalc.const import SensorType
from custom_components.powercalc.flow_helper.common import PowercalcFormStep, Step
from custom_components.powercalc.flow_helper.schema import SCHEMA_UTILITY_METER_TOGGLE

if TYPE_CHECKING:
    from custom_components.powercalc.config_flow import PowercalcConfigFlow, PowercalcOptionsFlow

SCHEMA_REAL_POWER_OPTIONS = vol.Schema(
    {
        vol.Required(CONF_ENTITY_ID): selector.EntitySelector(
            selector.EntitySelectorConfig(device_class=SensorDeviceClass.POWER),
        ),
        vol.Optional(CONF_DEVICE): selector.DeviceSelector(),
    },
)

SCHEMA_REAL_POWER = vol.Schema(
    {
        vol.Required(CONF_NAME): selector.TextSelector(),
        **SCHEMA_REAL_POWER_OPTIONS.schema,
        **SCHEMA_UTILITY_METER_TOGGLE.schema,
    },
).extend(SCHEMA_REAL_POWER_OPTIONS.schema)


class RealPowerConfigFlow:
    def __init__(self, flow: PowercalcConfigFlow) -> None:
        self.flow: PowercalcConfigFlow = flow

    async def async_step_real_power(self, user_input: dict[str, Any] | None = None) -> ConfigFlowResult:
        """Handle the flow for real power sensor"""

        self.flow.selected_sensor_type = SensorType.REAL_POWER
        return await self.flow.handle_form_step(
            PowercalcFormStep(
                step=Step.REAL_POWER,
                schema=SCHEMA_REAL_POWER,
                next_step=Step.ENERGY_OPTIONS,
            ),
            user_input,
        )


class RealPowerOptionsFlow:
    def __init__(self, flow: PowercalcOptionsFlow) -> None:
        self.flow: PowercalcOptionsFlow = flow

    async def async_step_real_power(self, user_input: dict[str, Any] | None = None) -> ConfigFlowResult:
        """Handle the real power options flow."""
        return await self.flow.async_handle_options_step(user_input, SCHEMA_REAL_POWER_OPTIONS, Step.REAL_POWER)
