"""Step definitions for db_url_sanitisation.feature.

Tests the ``_sanitise_db_url`` helper and its use in ``build_info_data``.
"""

from __future__ import annotations

from pathlib import Path
from tempfile import mkdtemp
from unittest.mock import MagicMock, patch

from behave import given, then, when
from behave.runner import Context


# ---------------------------------------------------------------------------
# Given
# ---------------------------------------------------------------------------


@given('the database url is "{url}"')
def step_db_url_given(context: Context, url: str) -> None:
    """Store the raw database URL for processing."""
    context.raw_db_url = url


@given('a mock settings object with database url "{db_url}"')
def step_mock_settings_with_db_url(context: Context, db_url: str) -> None:
    """Build a full mock Settings that ``build_info_data`` expects."""
    tmpdir = mkdtemp()
    mock_settings = MagicMock()
    mock_settings.database_url = db_url
    mock_settings.data_dir = Path(tmpdir)
    mock_settings.storage_path = Path(tmpdir)
    mock_settings.config_path = Path(tmpdir) / "config.toml"
    mock_settings.log_dir = Path(tmpdir) / "logs"
    mock_settings.default_automation_profile = "auto"
    mock_settings.has_provider_configured = MagicMock(return_value=False)
    mock_settings.configured_provider_names = MagicMock(return_value=[])
    mock_settings.debug_enabled = False
    context.mock_settings = mock_settings


# ---------------------------------------------------------------------------
# When
# ---------------------------------------------------------------------------


@when("I run sanitise_db_url")
def step_run_sanitise(context: Context) -> None:
    """Call _sanitise_db_url with the stored raw URL."""
    from cleveragents.cli.commands.system import _sanitise_db_url

    context.sanitised_url = _sanitise_db_url(context.raw_db_url)


@when("I call build_info_data")
def step_call_build_info(context: Context) -> None:
    """Call ``build_info_data`` with the mock settings."""
    from cleveragents.cli.commands.system import build_info_data

    with patch(
        "cleveragents.config.settings.get_settings",
        return_value=context.mock_settings,
    ):
        context.info_data = build_info_data()


# ---------------------------------------------------------------------------
# Then
# ---------------------------------------------------------------------------


@then('the sanitised database url should be "{expected}"')
def step_assert_sanitised_url(context: Context, expected: str) -> None:
    """Verify the sanitised URL matches expectation."""
    actual = context.sanitised_url
    assert actual == expected, f"Expected '{expected}', got '{actual}'"


@then('the database field in info data should be "{expected}"')
def step_assert_info_db_field(context: Context, expected: str) -> None:
    """Verify the sanitised URL appears correctly in build_info_data output."""
    actual = context.info_data["database"]
    assert actual == expected, f"Expected database field '{expected}', got '{actual}'"
