package at.procon.eventhub.esperpoc.api;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import at.procon.eventhub.esperpoc.dto.EsperDtiEnrichmentResultDto;
import at.procon.eventhub.esperpoc.service.EsperDtiEnrichmentService;
import at.procon.eventhub.esperpoc.service.EsperOperatingPeriodEvaluationService;
import at.procon.eventhub.esperpoc.service.EsperPocDriverCardActivityService;
import java.time.OffsetDateTime;
import java.util.List;
import java.util.UUID;
import org.junit.jupiter.api.Test;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

class EsperPocControllerTest {

    @Test
    void exposesDtiEnrichmentEndpoint() throws Exception {
        EsperPocDriverCardActivityService activityService = org.mockito.Mockito.mock(EsperPocDriverCardActivityService.class);
        EsperOperatingPeriodEvaluationService operatingService = org.mockito.Mockito.mock(EsperOperatingPeriodEvaluationService.class);
        EsperDtiEnrichmentService enrichmentService = org.mockito.Mockito.mock(EsperDtiEnrichmentService.class);
        EsperPocController controller = new EsperPocController(activityService, operatingService, enrichmentService);
        MockMvc mockMvc = MockMvcBuilders.standaloneSetup(controller).build();

        when(enrichmentService.evaluate(any())).thenReturn(new EsperDtiEnrichmentResultDto(
                "default",
                UUID.fromString("00000000-0000-0000-0000-000000000123"),
                OffsetDateTime.parse("2026-04-01T00:00:00Z"),
                OffsetDateTime.parse("2026-04-02T00:00:00Z"),
                OffsetDateTime.parse("2026-03-31T00:00:00Z"),
                OffsetDateTime.parse("2026-04-03T00:00:00Z"),
                OffsetDateTime.parse("2026-03-01T00:00:00Z"),
                OffsetDateTime.parse("2026-04-03T00:00:00Z"),
                1,
                2,
                1,
                180,
                180,
                720,
                List.of(),
                List.of("note")
        ));

        mockMvc.perform(get("/api/eventhub/esper-poc/tachograph/dti-enrichment")
                        .param("tenantKey", "default")
                        .param("driverId", "00000000-0000-0000-0000-000000000123")
                        .param("occurredFrom", "2026-04-01T00:00:00Z")
                        .param("occurredTo", "2026-04-02T00:00:00Z"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.tenantKey").value("default"))
                .andExpect(jsonPath("$.pureDtiCount").value(1))
                .andExpect(jsonPath("$.vehicleUsageIntervalCount").value(1));
    }
}
