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

import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

import io.delphiplatform.api.util.CollectionUtils;
import io.delphiplatform.api.util.OffsetBasedPageRequest;
import io.delphiplatform.api.v3.model.ads.AdPerformanceSummaryItem;
import io.delphiplatform.api.v3.model.ads.GroupByAdsPerformance;
import io.delphiplatform.api.v3.rdb.service.ads.report.AdsAgeRangeReportService;
import io.delphiplatform.api.v3.rdb.service.ads.report.AdsCampaignCountryReportService;
import io.delphiplatform.api.v3.rdb.service.ads.report.AdsCampaignPlatformReportService;
import io.delphiplatform.api.v3.rdb.service.ads.report.AdsCampaignReportService;
import io.delphiplatform.api.v3.rdb.service.ads.report.AdsGenderReportService;
import io.delphiplatform.api.v3.rdb.service.ads.report.LinkfireAdsCampaignPlatformReportService;
import io.delphiplatform.api.v3.rdb.service.dto.AdsReportQueryFilterParams;
import lombok.extern.slf4j.Slf4j;

import static io.delphiplatform.api.v3.model.ads.GroupByAdsPerformance.AGE_BAND;
import static io.delphiplatform.api.v3.model.ads.GroupByAdsPerformance.GENDER;
import static io.delphiplatform.api.v3.model.ads.GroupByAdsPerformance.LINK_TYPE;
import static io.delphiplatform.api.v3.model.ads.GroupByAdsPerformance.LINK_URL;
import static io.delphiplatform.api.v3.model.ads.GroupByAdsPerformance.PLATFORM;
import static io.delphiplatform.api.v3.model.ads.GroupByAdsPerformance.REFERRER_DOMAIN;
import static io.delphiplatform.api.v3.model.ads.GroupByAdsPerformance.REFERRER_NAME;
import static io.delphiplatform.api.v3.model.ads.GroupByAdsPerformance.REGION;

@Slf4j
@Service
public class AdsMetricService {

    private final AdsAgeRangeReportService ageRangeReportService;
    private final AdsCampaignCountryReportService campaignCountryReportService;
    private final AdsCampaignPlatformReportService campaignPlatformReportService;
    private final AdsGenderReportService genderReportService;
    private final AdsCampaignReportService campaignReportService;
    private final LinkfireAdsCampaignPlatformReportService linkfireCampaignPlatformReportService;

    protected AdsMetricService(AdsGenderReportService genderReportService,
        AdsAgeRangeReportService ageRangeReportService,
        AdsCampaignCountryReportService campaignCountryReportService,
        AdsCampaignPlatformReportService campaignPlatformReportService,
        AdsCampaignReportService campaignReportService,
        LinkfireAdsCampaignPlatformReportService linkfireCampaignPlatformReportService) {
        this.genderReportService = genderReportService;
        this.ageRangeReportService = ageRangeReportService;
        this.campaignCountryReportService = campaignCountryReportService;
        this.campaignPlatformReportService = campaignPlatformReportService;
        this.campaignReportService = campaignReportService;
        this.linkfireCampaignPlatformReportService = linkfireCampaignPlatformReportService;
    }

    public List<AdPerformanceSummaryItem> getMetrics(
        AdsReportQueryFilterParams filterParams,
        Set<GroupByAdsPerformance> groupBy,
        OffsetBasedPageRequest pageRequest) {

        if (CollectionUtils.contains(groupBy, GENDER)) {
            return genderReportService.getReportMetrics(filterParams, groupBy, pageRequest);
        }
        if (CollectionUtils.contains(groupBy, AGE_BAND)) {
            return ageRangeReportService.getReportMetrics(filterParams, groupBy, pageRequest);
        }
        if (CollectionUtils.contains(groupBy, PLATFORM)) {
            return campaignPlatformReportService
                .getReportMetrics(filterParams, groupBy, pageRequest);
        }
        if (CollectionUtils.containsAny(groupBy,
            List.of(LINK_TYPE, LINK_URL, REFERRER_DOMAIN, REFERRER_NAME))) {
            return linkfireCampaignPlatformReportService
                .getReportMetrics(filterParams, groupBy, pageRequest);
        }
        if (CollectionUtils.contains(groupBy, REGION)
            || CollectionUtils.isNotEmpty(filterParams.getCountryCodes())) {
            return campaignCountryReportService.getReportMetrics(filterParams, groupBy, pageRequest);
        }
        return campaignReportService.getReportMetrics(filterParams, groupBy, pageRequest);
    }
}
