package at.procon.eventhub.yellowfox.service;

import at.procon.eventhub.dto.DriverCardRefDto;
import at.procon.eventhub.dto.DriverRefDto;
import at.procon.eventhub.dto.EventDomain;
import at.procon.eventhub.dto.EventType;
import at.procon.eventhub.dto.VehicleRefDto;
import at.procon.eventhub.dto.VehicleRegistrationRefDto;
import at.procon.eventhub.service.EventDetailsFactory;
import at.procon.eventhub.yellowfox.dto.YellowFoxD8BookingDto;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.time.OffsetDateTime;
import java.util.Map;
import org.junit.jupiter.api.Test;

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

class YellowFoxD8IgnitionTransitionDetectorTest {

    @Test
    void emitsBoundaryTransitionFromPreviousIgnitionFromSourceRow() {
        YellowFoxD8BookingEventMapper mapper = new YellowFoxD8BookingEventMapper(new EventDetailsFactory(new ObjectMapper()));
        YellowFoxD8IgnitionTransitionDetector detector = new YellowFoxD8IgnitionTransitionDetector(mapper);

        var event = detector.newSession(false).detect(booking(1, 0));

        assertThat(event).isNotNull();
        assertThat(event.eventDomain()).isEqualTo(EventDomain.IGNITION);
        assertThat(event.eventType()).isEqualTo(EventType.IGNITION_ON);
    }

    @Test
    void doesNotEmitWhenCurrentIgnitionMatchesPreviousIgnitionFromSourceRow() {
        YellowFoxD8BookingEventMapper mapper = new YellowFoxD8BookingEventMapper(new EventDetailsFactory(new ObjectMapper()));
        YellowFoxD8IgnitionTransitionDetector detector = new YellowFoxD8IgnitionTransitionDetector(mapper);

        var event = detector.newSession(false).detect(booking(1, 1));

        assertThat(event).isNull();
    }

    private YellowFoxD8BookingDto booking(Integer ignition, Integer previousIgnition) {
        return new YellowFoxD8BookingDto(
                "tenant-1",
                "instance-1",
                "setting-1",
                "7",
                "Fleet 7",
                "evt-1",
                "key-1",
                ignition,
                previousIgnition,
                2,
                3,
                OffsetDateTime.parse("2026-05-08T10:15:30+02:00"),
                null,
                new DriverRefDto("1", new DriverCardRefDto(YellowFoxReferenceSemantics.SYNTHETIC_REFERENCE_NATION, "12345678901234")),
                new VehicleRefDto("42", "VIN-42", "42", new VehicleRegistrationRefDto(YellowFoxReferenceSemantics.SYNTHETIC_REFERENCE_NATION, "W-4242")),
                123_000L,
                null,
                null,
                Map.of()
        );
    }
}
