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

import io.delphi.qa.auto.awsmfrw.common_baby.EnDelphiFsResourceList;
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.models.ICanFetch;
import io.delphi.qa.auto.awsmfrw.tests.BsTestCase;
import io.qameta.allure.Issue;
import net.snowflake.client.jdbc.internal.joda.time.DateTime;
import io.delphi.qa.auto.awsmfrw.common_baby.IsaCommonMethods;
import org.assertj.core.api.Assertions;
import org.testng.annotations.Test;

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

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

    @Issue("DAE-7591")
    @Test(groups = {"SLZ Data Quality"})
    public void isrcIsMainProduct() {
        String queryGetContentStatusId = "select content_status_id\n" +
                "from content_failure_log cfl \n" +
                "where content_status_id in \n" +
                "(select  content_status_id \n" +
                "from content_status cs \n" +
                "where unit_of_work_id in\n" +
                "(select unit_of_work_id from unit_of_work uow  \n" +
                "where unit_of_work_code like '%" + ICanFetch.fetchItem(EnDelphiFsResourceList.D2_POSTGRESQL_PATTERN) +"%') \n" +
                "and content_status <> 'COMPLETE')\n" +
                "order by created_at desc;";
        List<Map<String, String>> contentStatusId= delphi.db().postgres(fetchAwsSecret(EnAwsSecrets.DELPHI_SLZ_STORAGE_PG_PROXY_USER)).awsmQueryNew(queryGetContentStatusId);
        List<String> contentStatusIds = IsaCommonMethods.collectValuesInListOfMapsByKey(contentStatusId,"content_status_id");
        for(String id: contentStatusIds) {
            String queryGetFailureCount = String.format("select failure_count from content_status cs where content_status_id=%s;", id);
            List<Map<String, String>> failureCount= delphi.db().postgres(fetchAwsSecret(EnAwsSecrets.DELPHI_SLZ_STORAGE_PG_PROXY_USER)).awsmQueryNew(queryGetFailureCount);
            Assertions.assertThat(failureCount.get(0).get("failure_count")).withFailMessage( "failure_count is not equal to content_status_id : " + failureCount.get(0).get("failure_count"))
                    .isEqualTo(String.valueOf(contentStatusIds.stream().filter(s -> s.equals(id)).count()));
        }
    }

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