package at.procon.eventhub.tachographfilesession.service;

import at.procon.eventhub.reference.DriverCardNumberNormalizer;
import org.springframework.stereotype.Component;

@Component
public class DriverKeyFactory {

    public String createDriverKey(String cardNation, String cardNumber) {
        String normalizedNation = normalize(cardNation, "UNKNOWN");
        String normalizedCardNumber = normalize(canonicalCardNumber(cardNumber), "UNKNOWN");
        return normalizedNation + ":" + normalizedCardNumber;
    }

    public String canonicalCardNumber(String cardNumber) {
        return DriverCardNumberNormalizer.canonical(cardNumber);
    }

    public String fullCardNumber(String cardNumber) {
        return DriverCardNumberNormalizer.full(cardNumber);
    }

    public String createSourceDriverId(String driverKey) {
        return "DRV:" + driverKey;
    }

    public String createSourceDriverCardId(String driverKey) {
        return "CARD:" + driverKey;
    }

    private String normalize(String value, String fallback) {
        if (value == null || value.isBlank()) {
            return fallback;
        }
        return value.trim().toUpperCase();
    }
}
