package cz.moneta.test.regression.cvm.api.cvm;

import com.fasterxml.jackson.databind.JsonNode;
import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.util.DateUtils;
import cz.moneta.test.dsl.util.task.cvm.onlinedb.OnlineDbGetDataTasks;
import cz.moneta.test.dsl.util.task.cvm.onlinedb.OnlineDbVerificationTasks;
import cz.moneta.test.harness.annotations.TestCase;
import cz.moneta.test.harness.annotations.TestScenario;
import cz.moneta.test.harness.support.rest.RawRestRequest;
import org.jooq.Record;
import org.jooq.Result;
import org.junit.jupiter.api.Assertions;

import java.util.List;

import static cz.moneta.test.harness.annotations.Environment.PPE;
import static cz.moneta.test.harness.annotations.Environment.TST1;

@TestScenario(name = "Call API RTRD_DIGITALCHANNELS_LOAN_CALCULATOR_CALCULATION to WSO2 and assert response parameters")
public class ApiRtrdDigitalchannelsLoanCalculatorCalculation {

    @TestCase(name = "Call API RTRD_DIGITALCHANNELS_LOAN_CALCULATOR_CALCULATION to WSO2", environments = {TST1, PPE})
    public void postRtrdDigitalchannelsLoanCalculatorCalculation(Harness harness) throws InterruptedException {
        RawRestRequest.Response response = harness.tasks().wso2().restRequest().prepareRequestFromCvm(
                        "cvm.cluster/RTRD_DIGITALCHANNELS_LOAN_CALCULATOR_CALCULATION/001", false,
                        "{\n" +
                                "  \"calculationInstalment\": 5000,\n" +
                                "  \"calculationInstalmentLength\": 24,\n" +
                                "  \"calculationLoanAmount\": 100000,\n" +
                                "  \"calculationRpsn\": 4,\n" +
                                "  \"channelId\": \"IB\",\n" +
                                "  \"clientCif\": \"" + harness.getConfig("cvm.client.cif") + "\",\n" +
                                "  \"eventTimeStamp\": \"" + DateUtils.getFormattedTodayDateTimeForWso2() + "\",\n" +
                                "  \"userId\": " + harness.getConfig("cvm.client.userid") + "\n" +
                                "}")
                .postExtended()
                .andAssertStatus(200);

        JsonNode body = response.extract("");
        List<Object> activityIds = response.getHeader("activityId");

        Assertions.assertNotNull(body, "Not found output in response");
        Assertions.assertEquals(1, activityIds.size(), "API should return activityId header");
        final String activityId = activityIds.get(0).toString();

        Thread.sleep(2000); // Delay to finish async code

        OnlineDbGetDataTasks onlineDbGetDataTasks = harness.tasks().cvm().onlineDbGetData();
        Result<Record> onlineDbRecords = onlineDbGetDataTasks.getPlannedTasks(
                OnlineDbGetDataTasks.PAYLOAD_ATTRIBUTE_NAME_ACTIVITY_ID, activityId);
        Assertions.assertEquals(1, onlineDbRecords.size(), "Should plan one event");
        final Record onlineDbRecord = onlineDbRecords.get(0);

        OnlineDbVerificationTasks onlineDbVerificationTasks = harness.tasks().cvm().onlineDbVerification();
        onlineDbVerificationTasks.assertOnlineDbRecordIsValid(onlineDbRecord, "digitalChannelsCalculatorCalculationTask",
                "cvm.cluster/IB_EXP_CALC/1.0.0", 40 * 60,
                data -> Assertions.assertEquals(100000, data.get("calculationLoanAmount").getAsInt(), "Event data should contain correct data"));

        Result<Record> triggerLogRecords = onlineDbGetDataTasks.getTriggerLogs(
                "RTRD_DIGITALCHANNELS_LOAN_CALCULATOR_CALCULATION", activityId);
        Assertions.assertEquals(1, triggerLogRecords.size(), "Should log one record");

        onlineDbVerificationTasks.assertTriggerLogRecordIsPlanned(triggerLogRecords.get(0), onlineDbRecord,
                "cvm.cluster/IB_EXP_CALC/1.0.0",
                logData -> Assertions.assertEquals("{\"CIF\":\"" + harness.getConfig("cvm.client.cif") + "\"," +
                                "\"USER_ID\":\"" + harness.getConfig("cvm.client.userid") + "\"}", logData,
                        "Planned call with correct additional data should be logged"));

    }

}