package at.procon.eventhub.esperpoc.service;

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

import at.procon.eventhub.esperpoc.dto.RawActivityEventDto;
import java.time.OffsetDateTime;
import java.util.List;
import java.util.UUID;
import org.junit.jupiter.api.Test;

class EsperDriverActivityEngineTest {

    private final EsperDriverActivityEngine engine = new EsperDriverActivityEngine();

    @Test
    void buildsActivityIntervalsFromStartAndEndEvents() {
        UUID driverId = UUID.randomUUID();
        UUID vehicleId = UUID.randomUUID();
        List<RawActivityEventDto> raw = List.of(
                raw("100", "START", "DRIVE", "2026-04-30T23:55:00Z", driverId, vehicleId),
                raw("100", "END", "DRIVE", "2026-05-01T00:10:00Z", driverId, vehicleId),
                raw("101", "START", "WORK", "2026-05-01T00:10:00Z", driverId, vehicleId),
                raw("101", "END", "WORK", "2026-05-01T00:40:00Z", driverId, vehicleId)
        );

        var intervals = engine.buildIntervals(raw);

        assertThat(intervals).hasSize(2);
        assertThat(intervals.get(0).activityType()).isEqualTo("DRIVE");
        assertThat(intervals.get(0).durationSeconds()).isEqualTo(15 * 60L);
        assertThat(intervals.get(1).activityType()).isEqualTo("WORK");
        assertThat(intervals.get(1).durationSeconds()).isEqualTo(30 * 60L);
    }

    private RawActivityEventDto raw(
            String rowId,
            String lifecycle,
            String activity,
            String occurredAt,
            UUID driverId,
            UUID vehicleId
    ) {
        return new RawActivityEventDto(
                UUID.randomUUID(),
                OffsetDateTime.parse(occurredAt),
                rowId,
                "TACHOGRAPH:CARD_ACTIVITY:" + rowId + ":" + lifecycle,
                "DRIVER_CARD",
                "CARD_ACTIVITY",
                driverId,
                vehicleId,
                null,
                activity,
                lifecycle,
                "DRIVER",
                "INSERTED",
                "SINGLE",
                "package-1"
        );
    }
}
