package com.vaessl.app.sync;

import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Service;
import com.vaessl.app.exception.WrongServiceTypeException;
import com.vaessl.app.shared.ServiceType;

@Service
public class SyncService {

    private final Map<ServiceType, SyncProvider> providerRegistry;

    public SyncService(List<SyncProvider> providers) {
        Map<ServiceType, SyncProvider> registry = new EnumMap<>(ServiceType.class);
        for (SyncProvider provider : providers) {
            registry.put(provider.getServiceType(), provider);
        }
        this.providerRegistry = registry;
    }

    /**
     * Dispatches the vector-store sync request to the provider registered for
     * {@link SyncRequest#serviceType()}.
     *
     * @param request the sync request
     * @throws WrongServiceTypeException if no provider is registered for the given service type
     */
    public void syncServiceVectorStore(SyncRequest request) {

        SyncProvider provider = providerRegistry.get(request.serviceType());

        if (provider == null) {
            throw new WrongServiceTypeException();
        }

        provider.syncVectorStore(request);
    }
}
