package de.votian.services.controller;

import de.votian.services.service.LegacyXmlIntegrationService;
import de.votian.services.service.LegacyXmlMetaobjectIntegrationService;
import de.votian.services.service.LegacyXmlOrderDataIntegrationService;
import de.votian.services.service.LegacyXmlOrderRequestIntegrationService;
import de.votian.services.service.LegacyXmlSystemIntegrationService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.http.ResponseEntity;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

class LegacyXmlControllerTest {

    private LegacyXmlIntegrationService legacyXmlIntegrationService;
    private LegacyXmlMetaobjectIntegrationService legacyXmlMetaobjectIntegrationService;
    private LegacyXmlOrderDataIntegrationService legacyXmlOrderDataIntegrationService;
    private LegacyXmlOrderRequestIntegrationService legacyXmlOrderRequestIntegrationService;
    private LegacyXmlSystemIntegrationService legacyXmlSystemIntegrationService;
    private LegacyXmlController controller;

    @BeforeEach
    void setUp() {
        legacyXmlIntegrationService = mock(LegacyXmlIntegrationService.class);
        legacyXmlMetaobjectIntegrationService = mock(LegacyXmlMetaobjectIntegrationService.class);
        legacyXmlOrderDataIntegrationService = mock(LegacyXmlOrderDataIntegrationService.class);
        legacyXmlOrderRequestIntegrationService = mock(LegacyXmlOrderRequestIntegrationService.class);
        legacyXmlSystemIntegrationService = mock(LegacyXmlSystemIntegrationService.class);
        controller = new LegacyXmlController(
                legacyXmlIntegrationService,
                legacyXmlMetaobjectIntegrationService,
                legacyXmlOrderDataIntegrationService,
                legacyXmlOrderRequestIntegrationService,
                legacyXmlSystemIntegrationService
        );
    }

    @Test
    void costcenterUsesNamedRequestParameterWhenPresent() {
        when(legacyXmlIntegrationService.handleCostcenterRequest("<xml/>")).thenReturn("<xml><state>OK</state></xml>");

        ResponseEntity<String> response = controller.costcenter("", "<xml/>");

        assertThat(response.getBody()).isEqualTo("<xml><state>OK</state></xml>");
        verify(legacyXmlIntegrationService).handleCostcenterRequest("<xml/>");
    }

    @Test
    void customerUsesNamedRequestParameterWhenPresent() {
        when(legacyXmlSystemIntegrationService.handleCustomerRequest("<xml/>")).thenReturn("<xml><state>OK</state></xml>");

        ResponseEntity<String> response = controller.customer("", "<xml/>");

        assertThat(response.getBody()).isEqualTo("<xml><state>OK</state></xml>");
        verify(legacyXmlSystemIntegrationService).handleCustomerRequest("<xml/>");
    }

    @Test
    void metaobjectUsesNamedRequestParameterWhenPresent() {
        when(legacyXmlMetaobjectIntegrationService.handleMetaobjectRequest("<xml/>")).thenReturn("<xml><state>OK</state></xml>");

        ResponseEntity<String> response = controller.metaobject("", "<xml/>");

        assertThat(response.getBody()).isEqualTo("<xml><state>OK</state></xml>");
        verify(legacyXmlMetaobjectIntegrationService).handleMetaobjectRequest("<xml/>");
    }

    @Test
    void orderDataUsesNamedRequestParameterWhenPresent() {
        when(legacyXmlOrderDataIntegrationService.handleOrderDataRequest("<xml/>")).thenReturn("<xml><state>OK</state></xml>");

        ResponseEntity<String> response = controller.orderData("", "<xml/>");

        assertThat(response.getBody()).isEqualTo("<xml><state>OK</state></xml>");
        verify(legacyXmlOrderDataIntegrationService).handleOrderDataRequest("<xml/>");
    }

    @Test
    void orderUsesNamedRequestParameterWhenPresent() {
        when(legacyXmlOrderRequestIntegrationService.handleOrderRequest("<xml/>")).thenReturn("<xml><state>OK</state></xml>");

        ResponseEntity<String> response = controller.order("", "<xml/>");

        assertThat(response.getBody()).isEqualTo("<xml><state>OK</state></xml>");
        verify(legacyXmlOrderRequestIntegrationService).handleOrderRequest("<xml/>");
    }

    @Test
    void stationUsesNamedRequestParameterWhenPresent() {
        when(legacyXmlOrderRequestIntegrationService.handleStationRequest("<xml/>")).thenReturn("<xml><state>OK</state></xml>");

        ResponseEntity<String> response = controller.station("", "<xml/>", null);

        assertThat(response.getBody()).isEqualTo("<xml><state>OK</state></xml>");
        verify(legacyXmlOrderRequestIntegrationService).handleStationRequest("<xml/>");
    }

    @Test
    void stationFallsBackToOrderRequestParameterWhenNeeded() {
        when(legacyXmlOrderRequestIntegrationService.handleStationRequest("<xml/>")).thenReturn("<xml><state>OK</state></xml>");

        ResponseEntity<String> response = controller.station("", null, "<xml/>");

        assertThat(response.getBody()).isEqualTo("<xml><state>OK</state></xml>");
        verify(legacyXmlOrderRequestIntegrationService).handleStationRequest("<xml/>");
    }
}
