"""Config flow for Weheat."""

from collections.abc import Mapping
import logging
from typing import Any, override

from weheat.abstractions.user import async_get_user_id_from_token
from weheat.exceptions import ApiException

from homeassistant.config_entries import SOURCE_REAUTH, ConfigFlowResult
from homeassistant.const import CONF_ACCESS_TOKEN, CONF_TOKEN
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.config_entry_oauth2_flow import AbstractOAuth2FlowHandler

from .const import API_URL, DOMAIN, ENTRY_TITLE, OAUTH2_SCOPES


class OAuth2FlowHandler(AbstractOAuth2FlowHandler, domain=DOMAIN):
    """Config flow to handle Weheat OAuth2 authentication."""

    DOMAIN = DOMAIN

    @property
    @override
    def logger(self) -> logging.Logger:
        """Return logger."""
        return logging.getLogger(__name__)

    @property
    @override
    def extra_authorize_data(self) -> dict[str, str]:
        """Extra data that needs to be appended to the authorize url."""
        return {
            "scope": " ".join(OAUTH2_SCOPES),
        }

    @override
    async def async_oauth_create_entry(self, data: dict) -> ConfigFlowResult:
        """Override create entry to find heat pumps."""
        try:
            user_id = await async_get_user_id_from_token(
                API_URL,
                data[CONF_TOKEN][CONF_ACCESS_TOKEN],
                async_get_clientsession(self.hass),
            )
        except ApiException as err:
            self.logger.error("Failed to get user ID from Weheat API: %s", err)
            return self.async_abort(reason="oauth_failed")
        if user_id is None:
            return self.async_abort(reason="oauth_failed")
        await self.async_set_unique_id(user_id)
        if self.source != SOURCE_REAUTH:
            self._abort_if_unique_id_configured()

            return self.async_create_entry(title=ENTRY_TITLE, data=data)

        self._abort_if_unique_id_mismatch(reason="wrong_account")
        return self.async_update_reload_and_abort(
            self._get_reauth_entry(), data_updates=data
        )

    async def async_step_reauth(
        self, entry_data: Mapping[str, Any]
    ) -> ConfigFlowResult:
        """Perform reauth upon an API authentication error."""
        return await self.async_step_reauth_confirm()

    async def async_step_reauth_confirm(
        self, user_input: dict[str, Any] | None = None
    ) -> ConfigFlowResult:
        """Confirm reauth dialog."""
        if user_input is None:
            return self.async_show_form(step_id="reauth_confirm")
        return await self.async_step_user()
