package io.delphiplatform.api.v3.bigtable;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Stream;

import javax.transaction.NotSupportedException;

import io.delphiplatform.api.v3.bigtable.config.RequestRowKeyHandle;
import io.delphiplatform.api.v3.bigtable.config.RowKeyAggPeriod;
import io.delphiplatform.api.v3.bigtable.config.RowKeyGroup;
import io.delphiplatform.api.v3.bigtable.config.RowKeyRequestType;
import io.delphiplatform.api.v3.bigtable.entity.PlaylistPosition;
import io.delphiplatform.api.v3.constant.BigtableTableName;
import io.delphiplatform.api.v3.view.util.Params;

@Deprecated
@Service
public class BigtableTrackPositionQueryExecutor {

    private final BigtableQueryExecutor queryExecutor;
    private final BigtableRowReader rowReader;

    @Autowired
    public BigtableTrackPositionQueryExecutor(BigtableQueryExecutor queryExecutor, BigtableRowReader rowReader) {
        this.queryExecutor = queryExecutor;
        this.rowReader = rowReader;
    }

    public CompletableFuture<Stream<PlaylistPosition>> getAllPlaylistPositions(Params params)
        throws NotSupportedException {
        RowKeyGenerator rowKeyGenerator = new RowKeyGenerator(params, RowKeyGroup.PLAYLIST, RowKeyAggPeriod.DAY, RowKeyRequestType.RANGE);
        List<RequestRowKeyHandle> rowKeys = rowKeyGenerator.getRequestRowKeyHandles();

        return queryExecutor.executePartitioned(BigtableTableName.PLAYLISTS, rowKeys)
            .thenApply(rowStream -> rowReader.readRows(PlaylistPosition.class, rowStream));
    }
}
