package at.procon.eventhub.dto;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import java.time.LocalDate;

/**
 * Acquisition package context. One package represents one coherent import unit,
 * for example tenant + TACHOGRAPH + VEHICLE_UNIT + DRIVER_ACTIVITY + import scope.
 */
public record EventHubPackageRequest(
        /** Tenant/client/account owning the acquired data. */
        @NotBlank String tenantKey,
        @Valid @NotNull EventSourceDto eventSource,

        /** Optional source-side group: tachograph organisation, YellowFox fleet, etc. */
        @Valid SourceGroupRefDto sourceGroup,

        /** Organisation/time selection used to acquire the package. */
        @Valid @NotNull ImportScopeDto importScope,

        @NotBlank String eventFamily,
        LocalDate businessDate,
        @NotBlank String externalPackageId
) {
    public EventHubPackageRequest {
        tenantKey = normalizeTenant(tenantKey);
        if (eventFamily != null) {
            eventFamily = eventFamily.trim().toUpperCase().replace('-', '_').replace(' ', '_');
        }
        if (importScope == null) {
            importScope = ImportScopeDto.tenantAll(null, null);
        }
    }

    private static String normalizeTenant(String value) {
        return value == null || value.isBlank() ? value : value.trim();
    }
}
