package at.procon.eventhub;

import at.procon.eventhub.dto.DriverCardRefDto;
import at.procon.eventhub.dto.DriverRefDto;
import at.procon.eventhub.dto.EventDetailsDto;
import at.procon.eventhub.dto.EventDomain;
import at.procon.eventhub.dto.EventHubEventDto;
import at.procon.eventhub.dto.EventHubPackageRequest;
import at.procon.eventhub.dto.EventLifecycle;
import at.procon.eventhub.dto.EventSourceDto;
import at.procon.eventhub.dto.EventType;
import at.procon.eventhub.dto.ImportScopeDto;
import at.procon.eventhub.dto.VehicleRefDto;
import at.procon.eventhub.dto.VehicleRegistrationRefDto;
import at.procon.eventhub.service.EventAcquisitionRecordKeyService;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.time.OffsetDateTime;
import org.junit.jupiter.api.Test;

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

class EventAcquisitionRecordKeyServiceTest {

    private final ObjectMapper objectMapper = new ObjectMapper();
    private final EventAcquisitionRecordKeyService service = new EventAcquisitionRecordKeyService();

    @Test
    void eventSignatureExcludesSourceAndExternalSourceEventId() {
        EventHubEventDto vuEvent = event(
                "TACHOGRAPH:VU:activity:456:start",
                new EventSourceDto("TACHOGRAPH", "VEHICLE_UNIT", "TACHOGRAPH_VU", "tachograph-db", null, null)
        );
        EventHubEventDto cardEvent = event(
                "TACHOGRAPH:CARD:activity:789:start",
                new EventSourceDto("TACHOGRAPH", "DRIVER_CARD", "TACHOGRAPH_DRIVER_CARD", "tachograph-db", null, null)
        );

        assertThat(service.buildSourceRecordKeyHash(vuEvent, 1))
                .isNotEqualTo(service.buildSourceRecordKeyHash(cardEvent, 2));
        assertThat(service.buildEventSignatureHash(vuEvent))
                .isEqualTo(service.buildEventSignatureHash(cardEvent));
    }

    private EventHubEventDto event(String externalSourceEventId, EventSourceDto eventSource) {
        EventHubPackageRequest packageInfo = new EventHubPackageRequest(
                "tenant-1",
                eventSource,
                null,
                ImportScopeDto.tenantAll(null, null),
                "DRIVER_ACTIVITY",
                null,
                eventSource.sourceKey() + ":package"
        );
        return new EventHubEventDto(
                null,
                externalSourceEventId,
                new DriverRefDto("driver-source-100", new DriverCardRefDto("AT", "D123456789")),
                new VehicleRefDto("vehicle-source-200", "WDB9634031L123456", new VehicleRegistrationRefDto("AT", "W-12345")),
                OffsetDateTime.parse("2026-04-28T08:00:00+02:00"),
                null,
                null,
                EventDomain.DRIVER_ACTIVITY,
                EventType.DRIVE,
                EventLifecycle.START,
                null,
                null,
                new EventDetailsDto("DRIVER_ACTIVITY", objectMapper.createObjectNode().put("cardSlot", "DRIVER")),
                null,
                objectMapper.createObjectNode(),
                false,
                packageInfo
        );
    }
}
