package io.delphi.qa.auto.awsmfrw.tests.poc;

import io.delphi.qa.auto.awsmfrw.common_baby.IsaCommonMethods;
import io.delphi.qa.auto.awsmfrw.tests.BsTest;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import org.hamcrest.Matchers;
import org.hamcrest.core.AnyOf;
import org.testng.annotations.Test;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class DatadogApiClientTest extends BsTest {

  @Test
  public void testName() {
    RequestSpecification requestSpecification = RestAssured.given();
    String path = "logs-analytics/list-csv";
    String value =
        "{\"list\":{\"columns\":[{\"field\":{\"path\":\"@extra.method\"}},{\"field\":{\"path\":\"@extra.path\"}},{\"field\":{\"path\":\"@extra.params.artist_id\"}},{\"field\":{\"path\":\"@extra.params.isrc\"}},{\"field\":{\"path\":\"@extra.params.campaign_id\"}},{\"field\":{\"path\":\"@extra.params.channel_id\"}},{\"field\":{\"path\":\"@extra.params.chart_group\"}},{\"field\":{\"path\":\"extra.params.chart_id\"}},{\"field\":{\"path\":\"extra.params.company_key\"}},{\"field\":{\"path\":\"extra.params.content_type\"}},{\"field\":{\"path\":\"@extra.params.country_code\"}},{\"field\":{\"path\":\"@extra.params.dsp\"}},{\"field\":{\"path\":\"@extra.params.end_date\"}},{\"field\":{\"path\":\"extra.params.isrc\"}},{\"field\":{\"path\":\"extra.params.label_id\"}},{\"field\":{\"path\":\"extra.params.parent_rep_owner_key\"}},{\"field\":{\"path\":\"@extra.params.phase_id\"}},{\"field\":{\"path\":\"@extra.params.platform\"}},{\"field\":{\"path\":\"@extra.params.playlist_id\"}},{\"field\":{\"path\":\"extra.params.product_family_no\"}},{\"field\":{\"path\":\"extra.params..product_id\"}},{\"field\":{\"path\":\"extra.params.product_version_no\"}},{\"field\":{\"path\":\"extra.params.profit_center_key\"}},{\"field\":{\"path\":\"@extra.params.project_id\"}},{\"field\":{\"path\":\"@extra.params.provider_id\"}},{\"field\":{\"path\":\"extra.params.region\"}},{\"field\":{\"path\":\"extra.params.rep_owner_key\"}},{\"field\":{\"path\":\"@extra.params.start_date\"}},{\"field\":{\"path\":\"@extra.params.sub_category_id\"}},{\"field\":{\"path\":\"extra.params.track_id\"}},{\"field\":{\"path\":\"@extra.params.user_id\"}},{\"field\":{\"path\":\"extra.params.video_id\"}},{\"field\":{\"path\":\"extra.params.limit\"}},{\"field\":{\"path\":\"@extra.params.offset\"}},{\"field\":{\"path\":\"@extra.params.include\"}},{\"field\":{\"path\":\"@extra.params.report_date\"}},{\"field\":{\"path\":\"@extra.params.subset\"}},{\"field\":{\"path\":\"@extra.params.decibel_label_id\"}},{\"field\":{\"path\":\"@extra.params.sort_by\"}},{\"field\":{\"path\":\"@extra.params.report_licensor\"}},{\"field\":{\"path\":\"@extra.params.version\"}},{\"field\":{\"path\":\"@extra.params.group_by\"}},{\"field\":{\"path\":\"@extra.params.report_type\"}},{\"field\":{\"path\":\"@extra.params.agg_by\"}},{\"field\":{\"path\":\"@extra.params.is_assigned\"}},{\"field\":{\"path\":\"@extra.params.limit\"}},{\"field\":{\"path\":\"@extra.params.sort_order\"}}],\"sort\":{\"time\":{\"order\":\"desc\"}},\"limit\":50000,\"time\":{\"offset\":-7200000,\"from\":1618486499893,\"to\":1618500899893},\"search\":{\"query\":\"host:(\\\"/ecs/prod-delphi-api-java\\\"+OR+\\\"/ecs/prod-delphi-api\\\")+status:info+-message:(\\\"GET+/health\\\"+OR+\\\"GET+/v3/health\\\"+OR+\\\"GET+/v2/health\\\")+@extra.path:*\"},\"indexes\":[\"main\"],\"executionInfo\":{}}}&type=logs";
    Map<String, ?> formParams =
        new HashMap<String, String>() {
          {
            put("api_key", "45d82ac8943b27e69201ebf4d007e6d5");
            put("app_key", "2bf09b0741e55c1d43d5f758f4ee13370a324214");
            //            put("x-datadog-trace-id", "290461980819307013");
            //            put("x-datadog-parent-id","8408834102647589822");
            //            put(
            //                "referer",
            //
            // "https://app.datadoghq.com/logs?cols=%40extra.method%2C%40extra.path%2C%40extra.params.artist_id%2C%40extra.params.isrc%2C%40extra.params.campaign_id%2C%40extra.params.channel_id%2C%40extra.params.chart_group%2Cextra.params.chart_id%2Cextra.params.company_key%2Cextra.params.content_type%2C%40extra.params.country_code%2C%40extra.params.dsp%2C%40extra.params.end_date%2Cextra.params.isrc%2Cextra.params.label_id%2Cextra.params.parent_rep_owner_key%2C%40extra.params.phase_id%2C%40extra.params.platform%2C%40extra.params.playlist_id%2Cextra.params.product_family_no%2Cextra.params..product_id%2Cextra.params.product_version_no%2Cextra.params.profit_center_key%2C%40extra.params.project_id%2C%40extra.params.provider_id%2Cextra.params.region%2Cextra.params.rep_owner_key%2C%40extra.params.start_date%2C%40extra.params.sub_category_id%2Cextra.params.track_id%2C%40extra.params.user_id%2Cextra.params.video_id%2Cextra.params.limit%2C%40extra.params.offset%2C%40extra.params.include%2C%40extra.params.report_date%2C%40extra.params.subset%2C%40extra.params.decibel_label_id%2C%40extra.params.sort_by%2C%40extra.params.report_licensor%2C%40extra.params.version%2C%40extra.params.group_by%2C%40extra.params.report_type%2C%40extra.params.agg_by%2C%40extra.params.is_assigned%2C%40extra.params.limit%2C%40extra.params.sort_order&from_ts=1618486499893&index=%2A&live=false&messageDisplay=inline&query=host%3A%28%22%2Fecs%2Fprod-delphi-api-java%22+OR+%22%2Fecs%2Fprod-delphi-api%22%29++status%3A%28info%29++-message%3A%28%22GET+%2Fhealth%22+OR+%22GET+%2Fv3%2Fhealth%22+OR+%22GET+%2Fv2%2Fhealth%22%29++%40extra.path%3A%2A&stream_sort=desc&to_ts=1618500899893");
          }
        };
    Map<String, ?> parametersMap =
        new HashMap<String, String>() {
          {
            put("query", value);
          }
        };
    RequestSpecification specification =
        requestSpecification
            .baseUri("https://app.datadoghq.com")
            //          .port(443)
            .basePath("api/v1")
            .urlEncodingEnabled(true);
    Response response =
        specification
            .log()
            .all(true)
            .formParams(formParams)
            //            .headers(headers)
            .params(parametersMap)
            .get(path);
    byte[] bytes =
        response
            .then()
            .log()
            .all(true)
            .statusCode(AnyOf.anyOf(Matchers.is(200), Matchers.is(210), Matchers.is(400)))
            .extract()
            .asByteArray();
    IsaCommonMethods.writeStreamToFile(
        bytes, new File(""), "datadog-", DT_NOW.getMillis() + ".csv");
  }
}
