package com.vaessl.app.sync;

import java.util.ArrayList;
import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Component;
import com.vaessl.app.homebox.HomeboxItemClient;
import com.vaessl.app.homebox.HomeboxItemClient.HomeboxItemPage;
import com.vaessl.app.shared.ServiceItem;
import com.vaessl.app.shared.ServiceType;
import com.vaessl.app.vector.EmbeddingService;
import lombok.RequiredArgsConstructor;

/**
 * Pages through the full Homebox catalog and re-indexes it into the vector store.
 * Item IDs are collected across all pages before {@link EmbeddingService#deleteStaleVectorEntries}
 * is called once at the end, rather than after each page: deleting per page would treat every
 * item outside the current page as stale, wiping out entries from pages already synced.
 */
@Component
@RequiredArgsConstructor
public class HomeboxSyncProvider implements SyncProvider {

    private final EmbeddingService embeddingService;

    private final HomeboxItemClient client;

    @Override
    public ServiceType getServiceType() {
        return ServiceType.HOMEBOX;
    }

    @Override
    public void syncVectorStore(SyncRequest request) {

        int batchSize = 100;
        int page = 0;
        Page<ServiceItem> current;
        Long connectionId = null;
        List<String> currentItemIds = new ArrayList<>();
        do {
            Pageable pageable = PageRequest.of(page, batchSize);
            HomeboxItemPage result = client.hbResponse(request, null, pageable);
            current = result.page();
            if (connectionId == null) {
                connectionId = result.connectionId();
            }
            embeddingService.vectorizeData(current.getContent(), request.serviceType(),
                    result.connectionId(), currentItemIds);
            page++;
        } while (page < current.getTotalPages());

        embeddingService.deleteStaleVectorEntries(connectionId, currentItemIds);
    }
}
