package io.delphiplatform.api.v3.rdb.service;

import com.google.common.collect.ImmutableMap;

import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

import java.util.Set;

import io.delphiplatform.api.util.CollectionUtils;
import io.delphiplatform.api.v3.model.video.IncludeVideoChart;
import io.delphiplatform.api.v3.rdb.service.dto.JpaPropertyPath;
import io.delphiplatform.api.v3.view.util.Params;

public abstract class AbstractVideoPositionService<T> extends EntityService<T> {

    private static final Set<JpaPropertyPath> FETCHES = Set.of(JpaPropertyPath.ofJoin("chart"));
    private static final Set<JpaPropertyPath> FETCHES_WITH_VIDEO = Set.of(JpaPropertyPath.ofJoin("chart"),
        JpaPropertyPath.ofJoins("video", "sonyVideo"));

    protected AbstractVideoPositionService(JpaSpecificationExecutor specificationExecutor,
        ImmutableMap sortByToEntityFieldMap) {
        super(specificationExecutor, sortByToEntityFieldMap);
    }

    public Set<JpaPropertyPath> getFetches(Params params) {
        if (CollectionUtils.isNotEmpty(params.getSortBy()) && params.getSortBy().contains("video")
            || CollectionUtils.contains(params.getIncludeVideChart(), IncludeVideoChart.VIDEO)) {
            return FETCHES_WITH_VIDEO;
        }
        return FETCHES;
    }

}
