package at.procon.eventhub.processing.eventprocessing.module.epl;

import static org.assertj.core.api.Assertions.assertThat;

import at.procon.eventhub.dto.DriverCardRefDto;
import at.procon.eventhub.dto.DriverRefDto;
import at.procon.eventhub.dto.EventDomain;
import at.procon.eventhub.dto.EventHubEventDto;
import at.procon.eventhub.dto.EventLifecycle;
import at.procon.eventhub.dto.EventType;
import at.procon.eventhub.dto.VehicleRefDto;
import at.procon.eventhub.dto.VehicleRegistrationRefDto;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.time.OffsetDateTime;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.junit.jupiter.api.Test;

class DriverWorkingTimeEplEventMapperTest {

    @Test
    void usesCanonicalTachographKeysInsteadOfSourceSpecificRawVehicleIdentifiers() {
        EventHubEventDto activityEvent = event(
                EventDomain.DRIVER_ACTIVITY,
                EventType.DRIVE,
                EventLifecycle.START,
                "ACT-1",
                "2026-05-01T08:00:00Z"
        );
        EventHubEventDto cardEvent = event(
                EventDomain.DRIVER_CARD,
                EventType.CARD_INSERTED,
                EventLifecycle.INSERT,
                "CVU-1",
                "2026-05-01T08:00:00Z"
        );

        Map<String, Object> activityPoint = DriverWorkingTimeEplEventMapper.activityPointEvents(List.of(activityEvent)).getFirst();
        Map<String, Object> vehicleUsagePoint = DriverWorkingTimeEplEventMapper.vehicleUsagePointEvents(List.of(cardEvent)).getFirst();

        assertThat(activityPoint.get("driverKey")).isEqualTo("1:12345678901234");
        assertThat(activityPoint.get("registrationKey")).isEqualTo("1:LL-158TE");
        assertThat(activityPoint.get("vehicleKey")).isEqualTo("VIN123456789");

        assertThat(vehicleUsagePoint.get("driverKey")).isEqualTo("1:12345678901234");
        assertThat(vehicleUsagePoint.get("registrationKey")).isEqualTo("1:LL-158TE");
        assertThat(vehicleUsagePoint.get("vehicleKey")).isEqualTo("VIN123456789");
    }

    private EventHubEventDto event(
            EventDomain domain,
            EventType eventType,
            EventLifecycle lifecycle,
            String intervalId,
            String occurredAt
    ) {
        ObjectNode raw = JsonNodeFactory.instance.objectNode();
        raw.put("intervalId", intervalId);
        raw.put("sourceRowId", intervalId);
        raw.put("driverKey", "1:12345678901234");
        raw.put("vehicleKey", "3560|VIN123456789|9876|1:LL-158TE");
        raw.put("vehicleVin", "VIN123456789");
        raw.put("registrationKey", "1:LL-158TE");
        raw.put("sourceKind", "DRIVER_CARD");
        ObjectNode payload = JsonNodeFactory.instance.objectNode();
        payload.set("raw", raw);
        return new EventHubEventDto(
                UUID.randomUUID(),
                intervalId + ":" + lifecycle.name(),
                new DriverRefDto("3560", new DriverCardRefDto("1", 1, "12345678901234")),
                new VehicleRefDto(
                        "3560",
                        "VIN123456789",
                        "9876",
                        new VehicleRegistrationRefDto("1", 1, "LL-158TE")
                ),
                OffsetDateTime.parse(occurredAt),
                null,
                OffsetDateTime.parse(occurredAt),
                domain,
                eventType,
                lifecycle,
                null,
                null,
                null,
                null,
                payload,
                false,
                null
        );
    }
}
