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.harness.annotations.TestCase;
import cz.moneta.test.harness.annotations.TestScenario;

import java.util.UUID;

import static cz.moneta.test.harness.annotations.Environment.PPE;
import static cz.moneta.test.harness.annotations.Environment.TST1;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

@TestScenario(name = "Call API digi-investment-trigger to WSO2 and assert response parameters")
public class ApiDigiInvestmentTrigger {

    public static final String DIGI_INVESTMENT_TRIGGER_PATH = "cvm.cluster/digi-investment-trigger/1.0.0";

    @TestCase(name = "Call API digi-investment-trigger to WSO2", environments = {TST1, PPE})
    public void postDigiInvestmentTrigger(Harness harness) {
        JsonNode outputs = harness.tasks().wso2().restRequest().prepareRequestFromCvm(DIGI_INVESTMENT_TRIGGER_PATH, false,
                        "{\n" +
                                "    \"clientTimeZone\": \"Europe/Prague\",\n" +
                                "    \"correlationId\": \"" + UUID.randomUUID() + "\",\n" +
                                "    \"version\": 1,\n" +
                                "    \"inputs\": {\n" +
                                "        \"channelId\": \"17\",\n" +
                                "        \"clientCif\": \"" + harness.getConfig("cvm.client.investment.cif") + "\",\n" +
                                "        \"clientInvestmentDeals\": 73327,\n" +
                                "        \"configurationEventName\": \"PUSH\",\n" +
                                "        \"eventTimeStamp\": \"" + DateUtils.getFormattedTodayDateTimeForWso2() + "\",\n" +
                                "        \"investmentFrameworkAgreement\": true,\n" +
                                "        \"questionaireClientProfile\": \"4\",\n" +
                                "        \"screenId\": \"INVESTMENT_LIST\",\n" +
                                "        \"userId\": " + harness.getConfig("cvm.client.userid") + "\n" +
                                "    }\n" +
                                "}")
                .post()
                .andAssertStatus(201)
                .extract("outputs");

        harness.tasks().cvm().cvmWso2().assertResponseOutputs(outputs);
    }

    @TestCase(name = "Call API digi-investment-trigger to WSO2 with bad request", environments = {TST1, PPE})
    public void postDigiInvestmentTriggerBadRequest(Harness harness) {
        JsonNode message = harness.tasks().wso2().restRequest().prepareRequestFromCvm(DIGI_INVESTMENT_TRIGGER_PATH, false,
                        "{\n" +
                                "}")
                .post()
                .andAssertStatus(400)
                .extract("message");

        assertNotNull(message);
        assertEquals("Unable to parse decision request for decisionId SBA_InvestmentTrigger.", message.asText());
    }
}

