package at.procon.eventhub.tachograph.service;

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.service.EventDetailsFactory;
import at.procon.eventhub.tachograph.dto.source.TachographActivityDto;
import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.util.UUID;
import org.springframework.stereotype.Component;

@Component
public class TachographActivityEventMapper {

    private final EventDetailsFactory detailsFactory;

    public TachographActivityEventMapper(EventDetailsFactory detailsFactory) {
        this.detailsFactory = detailsFactory;
    }

    public EventHubEventDto map(TachographActivityDto source) {
        EventType activityType = source.activityType() == null ? EventType.UNKNOWN_ACTIVITY : source.activityType();
        EventLifecycle lifecycle = source.lifecycle() == null ? EventLifecycle.SNAPSHOT : source.lifecycle();
        String sourceKind = source.sourceKind() == null || source.sourceKind().isBlank()
                ? "VEHICLE_UNIT"
                : source.sourceKind();

        EventSourceDto eventSource = new EventSourceDto(
                "TACHOGRAPH",
                sourceKind,
                "TACHOGRAPH_" + sourceKind,
                source.sourceInstanceKey(),
                source.tenantProviderSettingKey(),
                null
        );
        LocalDate businessDate = source.occurredAt().toLocalDate();
        var occurredFrom = businessDate.atStartOfDay(source.occurredAt().getOffset()).toOffsetDateTime();
        var occurredTo = businessDate.plusDays(1).atStartOfDay(source.occurredAt().getOffset()).toOffsetDateTime();
        EventHubPackageRequest packageInfo = new EventHubPackageRequest(
                tenantOrDefault(source.tenantKey()),
                eventSource,
                null,
                ImportScopeDto.tenantAll(occurredFrom, occurredTo),
                EventDomain.DRIVER_ACTIVITY.name(),
                businessDate,
                eventSource.stableKey() + ":DRIVER_ACTIVITY:" + businessDate
        );

        return new EventHubEventDto(
                UUID.randomUUID(),
                source.externalSourceEventId(),
                source.driverRef(),
                source.vehicleRef(),
                source.occurredAt(),
                source.receivedPartnerAt(),
                OffsetDateTime.now(),
                EventDomain.DRIVER_ACTIVITY,
                activityType,
                lifecycle,
                null,
                null,
                detailsFactory.driverActivity(source.cardSlot(), source.cardStatus(), source.drivingStatus()),
                null,
                detailsFactory.payloadFromMap(source.payload()),
                false,
                packageInfo
        );
    }

    private String tenantOrDefault(String value) {
        return value == null || value.isBlank() ? "default" : value.trim();
    }
}
