package io.delphi.qa.auto.awsmfrw.tests.api.swagger;

import io.delphi.qa.auto.awsmfrw.common_baby.delphi.aws.ICanFetchAwsSecret;
import io.delphi.qa.auto.awsmfrw.tests.BsTestCase;
import io.qameta.allure.Issue;
import net.snowflake.client.jdbc.internal.joda.time.DateTime;
import org.assertj.core.api.Assertions;
import org.testng.annotations.Test;

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

public class GrasDataQualityTests extends BsTestCase implements ICanFetchAwsSecret {
    static {
        System.setProperty("delphi.slz.db.pg.isAutomation", "false");
    }

    @Issue("DAE-6766")
    @Test(groups = {"GRAS Data Quality"})
    public void isrcIsMainProduct() {
        String query = "select isrc, count(*)\n" +
                "from public.track\n" +
                "where is_main_product = true\n" +
                "group by isrc\n" +
                "having count(*) > 1;";
        List<Map<String, String>> result = delphi.db().postgres(delphiApiDbSecret).awsmQueryNew(query);
        Assertions.assertThat(result).withFailMessage(result.size() + " non-unique elements found: " + result).isEmpty();
    }

//    @Issue("DAE-6813")
//    @Test(groups = {"GRAS Data Quality"})
//    public void isrcWithoutMainProduct() {
//        String query = "select count(*) \n" +
//                "from \n" +
//                "(\n" +
//                "select  isrc , count (track_id) as n, \n" +
//                "sum(case when is_main_product = true then 1 else 0 end ) as main_product \n" +
//                "from track \n" +
//                "group by isrc\n" +
//                "having sum(case when is_main_product = true then 1 else 0 end )=0) as sub;";
//        List<Map<String, String>> result = delphi.db().postgres(delphiApiDbSecret).awsmQueryNew(query);
//        Long count = Long.valueOf(result.get(0).get("count"));
//        Assertions.assertThat(count).withFailMessage(count + " isrcs without main product found").isZero();
//    }

    @Test(groups = {"GRAS Data Quality"})
    public void isrcWithMainProductMore1() {
        String query = "select count(*) \n" +
                "from \n" +
                "(\n" +
                "select  isrc , count (track_id) as n, \n" +
                "sum(case when is_main_product = true then 1 else 0 end ) as main_product \n" +
                "from track \n" +
                "group by isrc\n" +
                "having sum(case when is_main_product = true then 1 else 0 end )>1) as sub;";
        List<Map<String, String>> result = delphi.db().postgres(delphiApiDbSecret).awsmQueryNew(query);
        Long count = Long.valueOf(result.get(0).get("count"));
        Assertions.assertThat(count).withFailMessage(count + " isrcs with more than one main product found").isZero();
    }

    @Override
    public String genTestRunId() {
        return String.valueOf(DateTime.now().getMillisOfDay());
    }
}
