package com.lingniu.ingest.identity;

import com.lingniu.ingest.api.ProtocolId;

import java.util.Map;

public record VehicleIdentityBinding(
        ProtocolId protocol,
        String vin,
        String phone,
        String deviceId,
        String plate,
        Map<String, String> metadata
) {
    public VehicleIdentityBinding(ProtocolId protocol, String vin, String phone, String deviceId, String plate) {
        this(protocol, vin, phone, deviceId, plate, Map.of());
    }

    public VehicleIdentityBinding {
        vin = normalize(vin);
        phone = normalize(phone);
        deviceId = normalize(deviceId);
        plate = normalize(plate);
        metadata = metadata == null ? Map.of() : Map.copyOf(metadata);
        if (vin.isBlank()) {
            throw new IllegalArgumentException("vin must not be blank");
        }
    }

    public boolean hasResolvedVin() {
        return !"unknown".equalsIgnoreCase(vin);
    }

    private static String normalize(String value) {
        return value == null ? "" : value.trim();
    }
}
