package at.procon.eventhub.service;

import at.procon.eventhub.dto.EventHubEventDto;
import at.procon.eventhub.dto.EventHubPackageRequest;
import org.springframework.stereotype.Component;

@Component
public class EventHubPackageKeyBuilder {

    public String build(EventHubEventDto event) {
        EventHubPackageRequest packageInfo = event.packageInfo();
        if (packageInfo != null) {
            return packageInfo.tenantKey()
                    + ":" + packageInfo.eventSource().stableKey()
                    + ":" + (packageInfo.sourceGroup() == null ? "NO_GROUP" : packageInfo.sourceGroup().stableKey())
                    + ":" + (packageInfo.importScope() == null ? "NO_SCOPE" : packageInfo.importScope().stableKey())
                    + ":" + packageInfo.eventFamily()
                    + ":" + (packageInfo.businessDate() == null ? "NO_DATE" : packageInfo.businessDate())
                    + ":" + packageInfo.externalPackageId();
        }

        String day = event.occurredAt().toLocalDate().toString();
        return "UNSPECIFIED_TENANT:UNSPECIFIED_SOURCE:" + event.eventDomain() + ":" + day;
    }
}
