package io.delphiplatform.api.v3.bigtable.config;

import java.util.List;

import io.delphiplatform.api.util.CollectionUtils;

import static io.delphiplatform.api.v3.bigtable.config.RowKeySegment.ARTIST_ID;
import static io.delphiplatform.api.v3.bigtable.config.RowKeySegment.BRAND;
import static io.delphiplatform.api.v3.bigtable.config.RowKeySegment.COUNTRY_CODE;
import static io.delphiplatform.api.v3.bigtable.config.RowKeySegment.DATE;
import static io.delphiplatform.api.v3.bigtable.config.RowKeySegment.DSP;
import static io.delphiplatform.api.v3.bigtable.config.RowKeySegment.ISRC;
import static io.delphiplatform.api.v3.bigtable.config.RowKeySegment.PLAYLIST_ID;
import static io.delphiplatform.api.v3.bigtable.config.RowKeySegment.PRODUCT_FAMILY_TYPE;
import static io.delphiplatform.api.v3.bigtable.config.RowKeySegment.PRODUCT_ID;
import static io.delphiplatform.api.v3.bigtable.config.RowKeySegment.PROJECT_NUMBER;
import static io.delphiplatform.api.v3.bigtable.config.RowKeySegment.TIKTOK_CONTENT_TYPE;
import static io.delphiplatform.api.v3.bigtable.config.RowKeySegment.TRACK_ID;
import static io.delphiplatform.api.v3.bigtable.config.RowKeySegment.UPC;
import static io.delphiplatform.api.v3.bigtable.config.RowKeySegment.VIDEO_CONTENT_TYPE;
import static io.delphiplatform.api.v3.bigtable.config.RowKeySegment.VIDEO_ID;

public enum RowKeyPrefix {
    ARTIST("artist", List.of(ARTIST_ID, DATE, DSP)),
    ARTIST_DATE("artist_date", List.of(ARTIST_ID, DATE)),
    ARTIST_PLAYLIST_DATE("artist_playlist_date", List.of(ARTIST_ID, PLAYLIST_ID, DATE)),
    DSP_ISRC_DATE_PLAYLIST("dsp_isrc_date_playlist", List.of(DSP, ISRC, DATE, PLAYLIST_ID)),
    ISRC_CONTENT_DATE("isrc_content_date", List.of(ISRC, VIDEO_CONTENT_TYPE, DATE)),
    ISRC_DATE("isrc_date", List.of(ISRC, DATE)),
    ISRC_DATE_PLAYLIST("isrc_date_playlist", List.of(ISRC, DATE, PLAYLIST_ID)),
    PLAYLIST_DATE("playlist_date", List.of(PLAYLIST_ID, DATE)),
    PLAYLIST_DATE_ISRC("playlist_date_isrc", List.of(PLAYLIST_ID, DATE, ISRC)),
    PLAYLIST_ISRC_DATE("playlist_isrc_date", List.of(PLAYLIST_ID, ISRC, DATE)),
    PLAYLIST_COUNTRY_CODE_DATE("playlist_country_code_date", List.of(PLAYLIST_ID, COUNTRY_CODE, DATE)),
    PRODUCT_DATE("product_date", List.of(PRODUCT_ID, DATE)),
    TRACK("track", List.of(TRACK_ID)),
    ISRC_TIKTOK_CONTENT_TYPE_DATE("isrc_content_type_date", List.of(ISRC, TIKTOK_CONTENT_TYPE, DATE)),
    VIDEO_DATE("video_date", List.of(VIDEO_ID, DATE)),
    PRODUCT_FAMILY("productfamily_date", List.of(PRODUCT_FAMILY_TYPE, DATE)),
    PROJECT("project_date", List.of(PROJECT_NUMBER, DATE)),
    PRODUCT_FAMILY_CONTENT_TYPE_DATE("productfamily_content_date", List.of(PRODUCT_FAMILY_TYPE, VIDEO_CONTENT_TYPE, DATE)),
    PROJECT_CONTENT_DATE("project_content_date", List.of(PROJECT_NUMBER, VIDEO_CONTENT_TYPE, DATE)),
    UPC_BRAND("upc_brand", List.of(UPC, BRAND));

    private final String prefix;
    private final List<String> segments;

    RowKeyPrefix(String prefix, List<String> segments) {
        this.prefix = prefix;
        this.segments = segments;
    }

    @Override
    public String toString() {
        return String.valueOf(prefix);
    }

    public String getPrefix() {
        return prefix;
    }

    public String getFullPrefix(RowKeyAggPeriod period) {
        if (period == null || CollectionUtils.isEmpty(period.getName())) {
            return prefix;
        }
        return prefix + "_" + period.getName();
    }

    public List<String> getSegments() {
        return segments;
    }
}
