"""Test Autoskope config flow."""

from unittest.mock import AsyncMock

from autoskope_client.models import CannotConnect, InvalidAuth
import pytest

from homeassistant.components.autoskope.const import (
    DEFAULT_HOST,
    DOMAIN,
    SECTION_ADDITIONAL_SETTINGS,
)
from homeassistant.config_entries import SOURCE_USER
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType

from tests.common import MockConfigEntry

USER_INPUT = {
    CONF_USERNAME: "test_user",
    CONF_PASSWORD: "test_password",
    SECTION_ADDITIONAL_SETTINGS: {
        CONF_HOST: DEFAULT_HOST,
    },
}


@pytest.mark.usefixtures("mock_setup_entry")
async def test_full_flow(hass: HomeAssistant, mock_autoskope_client: AsyncMock) -> None:
    """Test full user config flow from form to entry creation."""
    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"],
        USER_INPUT,
    )

    assert result["type"] is FlowResultType.CREATE_ENTRY
    assert result["title"] == "Autoskope (test_user)"
    assert result["data"] == {
        CONF_USERNAME: "test_user",
        CONF_PASSWORD: "test_password",
        CONF_HOST: DEFAULT_HOST,
    }
    assert result["result"].unique_id == f"test_user@{DEFAULT_HOST}"


@pytest.mark.parametrize(
    ("exception", "error"),
    [
        (InvalidAuth("Invalid credentials"), "invalid_auth"),
        (CannotConnect("Connection failed"), "cannot_connect"),
    ],
)
@pytest.mark.usefixtures("mock_setup_entry")
async def test_flow_errors(
    hass: HomeAssistant,
    mock_autoskope_client: AsyncMock,
    exception: Exception,
    error: str,
) -> None:
    """Test config flow error handling with recovery."""
    result = await hass.config_entries.flow.async_init(
        DOMAIN, context={"source": SOURCE_USER}
    )

    mock_autoskope_client.__aenter__.side_effect = exception

    result = await hass.config_entries.flow.async_configure(
        result["flow_id"],
        USER_INPUT,
    )
    assert result["type"] is FlowResultType.FORM
    assert result["errors"] == {"base": error}

    # Recovery: clear the error and retry
    mock_autoskope_client.__aenter__.side_effect = None

    result = await hass.config_entries.flow.async_configure(
        result["flow_id"],
        USER_INPUT,
    )
    assert result["type"] is FlowResultType.CREATE_ENTRY


@pytest.mark.usefixtures("mock_setup_entry")
async def test_flow_invalid_url(
    hass: HomeAssistant, mock_autoskope_client: AsyncMock
) -> None:
    """Test config flow rejects invalid URL with recovery."""
    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: "test_user",
            CONF_PASSWORD: "test_password",
            SECTION_ADDITIONAL_SETTINGS: {
                CONF_HOST: "not-a-valid-url",
            },
        },
    )
    assert result["type"] is FlowResultType.FORM
    assert result["errors"] == {"base": "invalid_url"}

    # Recovery: provide a valid URL
    result = await hass.config_entries.flow.async_configure(
        result["flow_id"],
        USER_INPUT,
    )
    assert result["type"] is FlowResultType.CREATE_ENTRY


async def test_already_configured(
    hass: HomeAssistant,
    mock_config_entry: MockConfigEntry,
    mock_autoskope_client: AsyncMock,
) -> None:
    """Test aborting if already configured."""
    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"],
        USER_INPUT,
    )

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


@pytest.mark.usefixtures("mock_setup_entry")
async def test_custom_host(
    hass: HomeAssistant, mock_autoskope_client: AsyncMock
) -> None:
    """Test config flow with a custom white-label host."""
    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: "test_user",
            CONF_PASSWORD: "test_password",
            SECTION_ADDITIONAL_SETTINGS: {
                CONF_HOST: "https://custom.autoskope.server",
            },
        },
    )

    assert result["type"] is FlowResultType.CREATE_ENTRY
    assert result["data"][CONF_HOST] == "https://custom.autoskope.server"
    assert result["result"].unique_id == "test_user@https://custom.autoskope.server"


@pytest.mark.usefixtures("mock_setup_entry")
async def test_reauth_flow(
    hass: HomeAssistant,
    mock_config_entry: MockConfigEntry,
    mock_autoskope_client: AsyncMock,
) -> None:
    """Test reauth flow updates password and reloads entry."""
    mock_config_entry.add_to_hass(hass)

    result = await mock_config_entry.start_reauth_flow(hass)
    assert result["type"] is FlowResultType.FORM
    assert result["step_id"] == "reauth_confirm"

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

    assert result["type"] is FlowResultType.ABORT
    assert result["reason"] == "reauth_successful"
    assert mock_config_entry.data == {
        CONF_USERNAME: "test_user",
        CONF_PASSWORD: "new_password",
        CONF_HOST: DEFAULT_HOST,
    }


@pytest.mark.parametrize(
    ("exception", "error"),
    [
        (InvalidAuth("Invalid credentials"), "invalid_auth"),
        (CannotConnect("Connection failed"), "cannot_connect"),
    ],
)
@pytest.mark.usefixtures("mock_setup_entry")
async def test_reauth_flow_errors(
    hass: HomeAssistant,
    mock_config_entry: MockConfigEntry,
    mock_autoskope_client: AsyncMock,
    exception: Exception,
    error: str,
) -> None:
    """Test reauth flow error handling with recovery."""
    mock_config_entry.add_to_hass(hass)

    result = await mock_config_entry.start_reauth_flow(hass)
    assert result["type"] is FlowResultType.FORM
    assert result["step_id"] == "reauth_confirm"

    mock_autoskope_client.__aenter__.side_effect = exception

    result = await hass.config_entries.flow.async_configure(
        result["flow_id"],
        {CONF_PASSWORD: "wrong_password"},
    )
    assert result["type"] is FlowResultType.FORM
    assert result["errors"] == {"base": error}

    # Recovery: clear the error and retry
    mock_autoskope_client.__aenter__.side_effect = None

    result = await hass.config_entries.flow.async_configure(
        result["flow_id"],
        {CONF_PASSWORD: "new_password"},
    )
    assert result["type"] is FlowResultType.ABORT
    assert result["reason"] == "reauth_successful"
