"""Test the Chess.com config flow."""

from unittest.mock import AsyncMock

from chess_com_api import NotFoundError
import pytest

from homeassistant.components.chess_com.const import DOMAIN
from homeassistant.config_entries import SOURCE_USER
from homeassistant.const import CONF_USERNAME
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType

from tests.common import MockConfigEntry


@pytest.mark.usefixtures("mock_chess_client")
async def test_full_flow(hass: HomeAssistant, mock_setup_entry: AsyncMock) -> None:
    """Test the full flow."""
    result = await hass.config_entries.flow.async_init(
        DOMAIN, context={"source": SOURCE_USER}
    )
    assert result["type"] is FlowResultType.FORM
    assert result["errors"] == {}

    result = await hass.config_entries.flow.async_configure(
        result["flow_id"], {CONF_USERNAME: "joostlek"}
    )

    assert result["type"] is FlowResultType.CREATE_ENTRY
    assert result["title"] == "Joost"
    assert result["data"] == {CONF_USERNAME: "joostlek"}
    assert result["result"].unique_id == "532748851"
    assert len(mock_setup_entry.mock_calls) == 1


@pytest.mark.usefixtures("mock_setup_entry")
async def test_flow_no_name(hass: HomeAssistant, mock_chess_client: AsyncMock) -> None:
    """Test the flow with no name."""
    result = await hass.config_entries.flow.async_init(
        DOMAIN, context={"source": SOURCE_USER}
    )
    assert result["type"] is FlowResultType.FORM
    assert result["errors"] == {}

    mock_chess_client.get_player.return_value.name = None

    result = await hass.config_entries.flow.async_configure(
        result["flow_id"], {CONF_USERNAME: "joostlek"}
    )

    assert result["type"] is FlowResultType.CREATE_ENTRY
    assert result["title"] == "joostlek"
    assert result["data"] == {CONF_USERNAME: "joostlek"}
    assert result["result"].unique_id == "532748851"


@pytest.mark.parametrize(
    ("exception", "error"),
    [
        (NotFoundError, "player_not_found"),
        (Exception, "unknown"),
    ],
)
async def test_form_errors(
    hass: HomeAssistant,
    mock_chess_client: AsyncMock,
    mock_setup_entry: AsyncMock,
    exception: Exception,
    error: str,
) -> None:
    """Test we handle form errors."""
    result = await hass.config_entries.flow.async_init(
        DOMAIN, context={"source": SOURCE_USER}
    )

    mock_chess_client.get_player.side_effect = exception

    result = await hass.config_entries.flow.async_configure(
        result["flow_id"], {CONF_USERNAME: "joostlek"}
    )

    assert result["type"] is FlowResultType.FORM
    assert result["errors"] == {"base": error}

    mock_chess_client.get_player.side_effect = None

    result = await hass.config_entries.flow.async_configure(
        result["flow_id"], {CONF_USERNAME: "joostlek"}
    )

    assert result["type"] is FlowResultType.CREATE_ENTRY
    assert len(mock_setup_entry.mock_calls) == 1


@pytest.mark.usefixtures("mock_chess_client")
async def test_duplicate_entry(
    hass: HomeAssistant, mock_config_entry: MockConfigEntry
) -> None:
    """Test we handle duplicate entries."""
    mock_config_entry.add_to_hass(hass)

    result = await hass.config_entries.flow.async_init(
        DOMAIN, context={"source": SOURCE_USER}
    )

    result = await hass.config_entries.flow.async_configure(
        result["flow_id"], {CONF_USERNAME: "joostlek"}
    )

    assert result["type"] is FlowResultType.ABORT
    assert result["reason"] == "already_configured"
