"""Define tests for the israel rail config flow."""

from unittest.mock import AsyncMock

import pytest

from homeassistant.components.israel_rail import CONF_DESTINATION, CONF_START, DOMAIN
from homeassistant.config_entries import SOURCE_USER
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType

from .conftest import VALID_CONFIG

from tests.common import MockConfigEntry


@pytest.mark.usefixtures("mock_setup_entry")
async def test_create_entry(hass: HomeAssistant, mock_israelrail: AsyncMock) -> None:
    """Test that the user step works."""
    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"],
        VALID_CONFIG,
    )
    assert result["type"] is FlowResultType.CREATE_ENTRY
    assert result["title"] == "באר יעקב אשקלון"
    assert result["data"] == {
        CONF_START: "באר יעקב",
        CONF_DESTINATION: "אשקלון",
    }


@pytest.mark.usefixtures("mock_setup_entry")
async def test_flow_fails(hass: HomeAssistant, mock_israelrail: AsyncMock) -> None:
    """Test that the user step fails."""
    mock_israelrail.query.side_effect = Exception("error")
    failed_result = await hass.config_entries.flow.async_init(
        DOMAIN,
        context={"source": SOURCE_USER},
        data=VALID_CONFIG,
    )

    assert failed_result["errors"] == {"base": "unknown"}
    assert failed_result["type"] is FlowResultType.FORM

    mock_israelrail.query.side_effect = None

    result = await hass.config_entries.flow.async_configure(
        failed_result["flow_id"],
        VALID_CONFIG,
    )

    assert result["type"] is FlowResultType.CREATE_ENTRY
    assert result["title"] == "באר יעקב אשקלון"
    assert result["data"] == {
        CONF_START: "באר יעקב",
        CONF_DESTINATION: "אשקלון",
    }


@pytest.mark.usefixtures("mock_setup_entry")
async def test_flow_already_configured(
    hass: HomeAssistant, mock_israelrail: AsyncMock, mock_config_entry: MockConfigEntry
) -> None:
    """Test that the user step fails when the entry is already configured."""
    mock_config_entry.add_to_hass(hass)

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

    result_aborted = await hass.config_entries.flow.async_configure(
        result["flow_id"],
        VALID_CONFIG,
    )

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