"""Config flow for Evil Genius Labs integration."""

from __future__ import annotations

import asyncio
import logging
from typing import Any

import aiohttp
import pyevilgenius
import voluptuous as vol

from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import aiohttp_client

from .const import DOMAIN

_LOGGER = logging.getLogger(__name__)


async def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> dict[str, Any]:
    """Validate the user input allows us to connect.

    Data has the keys from STEP_USER_DATA_SCHEMA with values provided by the user.
    """
    hub = pyevilgenius.EvilGeniusDevice(
        data["host"], aiohttp_client.async_get_clientsession(hass)
    )

    try:
        async with asyncio.timeout(10):
            data = await hub.get_all()
            info = await hub.get_info()
    except aiohttp.ClientError as err:
        _LOGGER.debug("Unable to connect: %s", err)
        raise CannotConnect from err

    return {"title": data["name"]["value"], "unique_id": info["wiFiChipId"]}


class EvilGeniusLabsConfigFlow(ConfigFlow, domain=DOMAIN):
    """Handle a config flow for Evil Genius Labs."""

    VERSION = 1

    async def async_step_user(
        self, user_input: dict[str, Any] | None = None
    ) -> ConfigFlowResult:
        """Handle the initial step."""
        if user_input is None:
            return self.async_show_form(
                step_id="user",
                data_schema=vol.Schema(
                    {
                        vol.Required("host"): str,
                    }
                ),
            )

        errors = {}

        try:
            info = await validate_input(self.hass, user_input)
        except TimeoutError:
            errors["base"] = "timeout"
        except CannotConnect:
            errors["base"] = "cannot_connect"
        except Exception:
            _LOGGER.exception("Unexpected exception")
            errors["base"] = "unknown"
        else:
            await self.async_set_unique_id(info["unique_id"])
            return self.async_create_entry(title=info["title"], data=user_input)

        return self.async_show_form(
            step_id="user",
            data_schema=vol.Schema(
                {
                    vol.Required("host", default=user_input["host"]): str,
                }
            ),
            errors=errors,
        )


class CannotConnect(HomeAssistantError):
    """Error to indicate we cannot connect."""
