from unittest.mock import MagicMock, patch

from utils.sharepoint.domna_sharepoint_client import DomnaSharepointClient


def test_handler_constructs_ecmk_service_and_calls_run() -> None:
    mock_service = MagicMock()
    mock_service_cls = MagicMock(return_value=mock_service)

    with (
        patch(
            "backend.ecmk_fetcher.handler.handler.EcmkService",
            mock_service_cls,
        ),
        patch(
            "backend.ecmk_fetcher.handler.handler.DomnaSharepointClient",
            return_value=MagicMock(spec=DomnaSharepointClient),
        ),
    ):
        from backend.ecmk_fetcher.handler.handler import handler

        handler({}, None)

    mock_service_cls.assert_called_once()
    mock_service.run.assert_called_once()


def test_handler_passes_correct_config_to_ecmk_service() -> None:
    mock_service = MagicMock()
    mock_service_cls = MagicMock(return_value=mock_service)

    with (
        patch(
            "backend.ecmk_fetcher.handler.handler.EcmkService",
            mock_service_cls,
        ),
        patch(
            "backend.ecmk_fetcher.handler.handler.DomnaSharepointClient",
            return_value=MagicMock(spec=DomnaSharepointClient),
        ),
    ):
        from backend.ecmk_fetcher.handler.handler import handler

        handler({}, None)

    _, kwargs = mock_service_cls.call_args
    assert kwargs["s3_bucket"] == "retrofit-energy-assessments-dev"
    assert (
        kwargs["sharepoint_base_path"]
        == "/Projects/Southern Housing/SH-SURV-26-001/Assessments"
    )
    assert (
        kwargs["sharepoint_excel_path"]
        == "/Projects/Southern Housing/SH-SURV-26-001/Modelling"
    )
    assert kwargs["property_list_filepath"].endswith(
        "hubspot-crm-exports-southern-ra-lite-programme-3103-2026-03-31-2.xlsx"
    )
    assert kwargs["local_dimensions_path"].endswith("Dimensions.xlsx")
