package at.procon.eventhub.dto;

import jakarta.validation.Valid;

/**
 * Source-side driver reference. No internal EventHub driver id is required in
 * incoming acquisition requests; it can be resolved later from sourceEntityId or
 * nation-scoped driver card number.
 *
 * Organisation assignment is intentionally not stored on the event. Driver ↔
 * organisation relation belongs to master data and can be resolved by
 * sourceEntityId/driverCard + occurredAt when needed.
 */
public record DriverRefDto(
        String sourceEntityId,
        @Valid DriverCardRefDto driverCard
) {
    public DriverRefDto {
        sourceEntityId = normalizeNullable(sourceEntityId);
    }

    public boolean hasAnyReference() {
        return (sourceEntityId != null && !sourceEntityId.isBlank())
                || (driverCard != null && driverCard.hasValue());
    }

    public String stableKey() {
        String cardKey = driverCard == null ? "" : driverCard.stableKey();
        return (sourceEntityId == null ? "" : sourceEntityId) + "|" + cardKey;
    }

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