package at.procon.eventhub.dto;

import java.time.OffsetDateTime;

/**
 * Reference to the original source package from which a source record was read.
 *
 * For tachograph data this is the original driver-card or vehicle-unit package
 * already imported into the tachograph DB. EventHub data packages are extraction
 * batches and are intentionally not forced to be identical to this source package.
 */
public record SourcePackageRefDto(
        String packageKind,
        String sourcePackageId,
        String sourceEntityId,
        OffsetDateTime packagePeriodFrom,
        OffsetDateTime packagePeriodTo,
        OffsetDateTime importedIntoSourceAt
) {
    public SourcePackageRefDto {
        packageKind = normalize(packageKind);
        sourcePackageId = normalizeNullable(sourcePackageId);
        sourceEntityId = normalizeNullable(sourceEntityId);
    }

    public boolean hasAnyReference() {
        return sourcePackageId != null || sourceEntityId != null || importedIntoSourceAt != null;
    }

    private static String normalize(String value) {
        if (value == null || value.isBlank()) {
            return value;
        }
        return value.trim().toUpperCase().replace('-', '_').replace(' ', '_');
    }

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