package io.delphi.qa.auto.awsmfrw.tests.api.steps.tiktok;

import io.delphi.qa.auto.awsmfrw.common_baby.delphi.aws.EnAwsSecrets;
import io.delphi.qa.auto.awsmfrw.common_baby.delphi.aws.ICanFetchAwsSecret;
import io.delphi.qa.auto.awsmfrw.managers.helpers.db.bigTable.RowKeyTikTokIsrcDateStringBuilder;
import io.delphi.qa.auto.awsmfrw.models.db.bigTable.BigtableTableName;
import io.delphi.qa.auto.awsmfrw.models.db.sql.dsl.DslSql;
import io.delphi.qa.auto.awsmfrw.models.db.sql.dsl.InterfaceDslSqlQuery;
import io.delphi.qa.auto.awsmfrw.models.db.sql.snowflake._delphi_exploration.main.MlSn_FactTiktokTopSounds;
import io.delphi.qa.auto.awsmfrw.tests.api.BsApiTest;
import io.qameta.allure.Step;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;

import java.util.List;
import java.util.Map;

public abstract class TikTokSteps extends BsApiTest {

    @Step("Get data from BigTable db")
    protected List<Map<String, String>> getIsrcBigTable(String startDate) {
        Pair<String, String> rowKey = new ImmutablePair<>(new RowKeyTikTokIsrcDateStringBuilder("isrc_date_week", "0",
                startDate).toString(), "");
        //find ISRC from BigTable
        return delphi.db().bigTable(ICanFetchAwsSecret.pullAwsSecret(EnAwsSecrets.DELPHI_API_BT_PARAMS))
                        .getRowValuesFromBigTable(BigtableTableName.FACT_TIKTOK_TOP_ISRC_SOUNDS_WEEKLY, rowKey, List.of("isrc"));
    }


    @Step("Get data from Snowflake db")
    protected List<Map<String, String>> getTotalCreationsSf(String isrc, String date) {
        InterfaceDslSqlQuery grasIdSonyArtistQuery =
                DslSql.select("w.REPORT_DATE, t.ISRC, REPORT_LICENSOR, sum(CREATIONS) \n")
                        .from("select distinct REPORT_DATE from " + new MlSn_FactTiktokTopSounds().locator() + ") w\n" +
                                "        left join STAGE_DELPHI_EXPLORATION.MAIN.TIKTOK_TRENDS_DAILY t\n" +
                                "        on t.DATE between (w.REPORT_DATE - 6) and w.REPORT_DATE\n" +
                                "        where t.PLATFORM_NAME = 'TikTok'").and("ISRC = '" + isrc + "'")
                        .and("w.REPORT_DATE = '" + date + "'").groupBy("1,2,3").orderByDescNoBraskets("report_date");
        //find ISRC from BigTable
        return delphi.db().sf().sysAdmin().getAdminClient().queryRows(grasIdSonyArtistQuery.queryText());
    }


}
