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.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 java.util.UUID;

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

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

    @TestCase(name = "Call API RTRD_WEB_TRIGGER_Expres to WSO2", environments = {TST1, PPE})
    public void postRtrdWebTriggerExpres(Harness harness) throws InterruptedException {
        UUID processCacheCode = UUID.randomUUID();
        RawRestRequest.Response response = harness.tasks().wso2().restRequest().prepareRequestFromCvm(
                "cvm.cluster/RTRD_WEB_TRIGGER_Expres/001", false,
                "{\n" +
                        "    \"PROCESS_CACHECODE\": \"" + processCacheCode + "\",\n" +
                        "    \"PROCESS_LASTTRACKER\": \"process.tracker.expressAppCreated\",\n" +
                        "    \"CLIENT_HASHEDCIF\": \"3214521\",\n" +
                        "    \"PRODUCT_EXPRES_LOANAMOUNT\": 50000,\n" +
                        "    \"PRODUCT_EXPRES_TERM\": 24,\n" +
                        "    \"PRODUCT_EXPRES_RPSN\": 8,\n" +
                        "    \"CLIENT_FIRSTNAME\": \"Aleš\",\n" +
                        "    \"CLIENT_LASTNAME\": \"Slunce\",\n" +
                        "    \"CLIENT_MOBILEPHONENUMBER\": \"420725810842\",\n" +
                        "    \"CLIENT_EMAIL\": \"ales.zlamal@neexistujicidomana.com\",\n" +
                        "    \"CLIENT_CONSENTTOOFFERMARKETING\": true,\n" +
                        "    \"CLIENT_PERMANENTADDRESS_STREET\": \"Novotného\",\n" +
                        "    \"CLIENT_PERMANENTADDRESS_LANDREGISTRYNUMBER\": 22,\n" +
                        "    \"CLIENT_PERMANENTADDRESS_HOUSENUMBER\": \"3a\",\n" +
                        "    \"CLIENT_PERMANENTADDRESS_CITY\": \"Praha\",\n" +
                        "    \"CLIENT_PERMANENTADDRESS_ZIP\": 12000,\n" +
                        "    \"CLIENT_PERMANENTADDRESS_STAYONADDRESSSINCE\": \"2017-04-10 14:16:14\",\n" +
                        "    \"CLIENT_CONTACTADDRESS_STREET\": \"Novotného\",\n" +
                        "    \"CLIENT_CONTACTADDRESS_LANDREGISTRYNUMBER\": 2,\n" +
                        "    \"CLIENT_CONTACTADDRESS_HOUSENUMBER\": \"3a\",\n" +
                        "    \"CLIENT_CONTACTADDRESS_CITY\": \"Praha\",\n" +
                        "    \"CLIENT_CONTACTADDRESS_ZIP\": 12000,\n" +
                        "    \"CLIENT_IDENTITYCARD_NUMBER\": 6207066536,\n" +
                        "    \"CLIENT_IDENTITYCARD_EXPIRATION\": \"2020-06-18 14:16:14\",\n" +
                        "    \"CLIENT_BIRTHNUMBER\": \"5487456987\",\n" +
                        "    \"CLIENT_MARITALSTATUS\": \"2\",\n" +
                        "    \"CLIENT_NATIONALITY\": \"52\",\n" +
                        "    \"CLIENT_COUNTRYOFBIRTH\": \"52\",\n" +
                        "    \"CLIENT_EMPLOYMENT_EMPLOYMENTTYPECODE\": \"2\",\n" +
                        "    \"CLIENT_EMPLOYMENT_EMPLOYERNAME\": \"Moneta a.s.\",\n" +
                        "    \"CLIENT_EMPLOYMENT_EMPLOYERID\": \"25672720\",\n" +
                        "    \"CLIENT_EMPLOYMENT_EMPLOYEDFROM\": \"2016-06-18 14:16:14\",\n" +
                        "    \"CLIENT_EMPLOYMENT_EMPLOYEDTO\": \"\",\n" +
                        "    \"CLIENT_EMPLOYMENT_TRIALPERIOD\": false,\n" +
                        "    \"CLIENT_EMPLOYMENT_NOTICEPERIOD\": false,\n" +
                        "    \"CLIENT_NET3MONTHINCOME\": 149820,\n" +
                        "    \"CLIENT_NET3MONTHHOUSEHOLDINCOME\": 281452,\n" +
                        "    \"CLIENT_INSTALLMENTSOUTSIDEMONETA\": 5000,\n" +
                        "    \"CLIENT_OTHERCOSTOFLIVING\": 25000,\n" +
                        "    \"CLIENT_NUMBEROFDEPENDENTS\": 2,\n" +
                        "    \"CLIENT_EDUCATION\": \"4\",\n" +
                        "    \"CLIENT_LIVINGTYPE\": \"3\",\n" +
                        "    \"CLIENT_CIF\": \"362173978\",\n" +
                        "    \"CLIENT_MARKETINGCLIENTTYPE\": \"3\",\n" +
                        "    \"CLIENT_EMPLOYMENT_EMPLOYEEFLAG\": true,\n" +
                        "    \"PRODUCT_EXISTINGINTERNETBANK\": true,\n" +
                        "    \"PRODUCT_PRODPACKAGEKEY\": 12345698,\n" +
                        "    \"PRODUCT_APPKEYEXPRES\": 67950039,\n" +
                        "    \"PRODUCT_APPKEYCA\": 3216547,\n" +
                        "    \"PRODUCT_APPKEYDC\": 3214521,\n" +
                        "    \"PRODUCT_APPKEYIB\": 12345657,\n" +
                        "    \"PRODUCT_INSTPTKEYEXPRES\": 12358965,\n" +
                        "    \"CLIENT_UNIKEY\": 148881,\n" +
                        "    \"PRODUCT_COMPLETABLEIB\": true,\n" +
                        "    \"PRODUCT_INSURANCEREQUESTED\": true,\n" +
                        "    \"CLIENT_REALNET3MONTHINCOME\": 148881,\n" +
                        "    \"PRODUCT_APPKEYCIF\": 12365455,\n" +
                        "    \"PROCESS_FRAUDCHECK_PRIORITY\": \"MANUAL_FRAUD_CHECK\"," +
                        "    \"PROCESS_CLIENTCHECKWFID\": \"CLCH_0000000123\"\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, "digitalChannelsLoanWebExpressTask",
                "cvm.cluster/SAS_RTDM_Web_Exp_Approved_CC/1.0.0", 3 * 24 * 60 * 60,
                data -> Assertions.assertEquals(12365455, data.get("productAppKeyCif").getAsInt(), "Event data should contain correct data"));
        onlineDbVerificationTasks.assertOnlineDbRecordIsValid(onlineDbRecord, "digitalChannelsLoanWebExpressTask",
                "cvm.cluster/SAS_RTDM_Web_Exp_Approved_CC/1.0.0", 3 * 24 * 60 * 60,
                data -> Assertions.assertEquals("CLCH_0000000123", data.get("processClientCheckWfId").getAsString(), "Event data should contain correct data"));

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

        onlineDbVerificationTasks.assertTriggerLogRecordIsPlanned(triggerLogRecords.get(0), onlineDbRecord,
                "cvm.cluster/SAS_RTDM_Web_Exp_Approved_CC/1.0.0",
                logData -> Assertions.assertEquals("{\"BIRTH_NUMBER\":\"5487456987\",\"CACHE_CODE\":\"" + processCacheCode + "\",\"CIF\":\"362173978\"," +
                        "\"PHONE_NUMBER\":\"420725810842\",\"PROCESS_TRACKER\":\"process.tracker.expressAppCreated\"," +
                        "\"UNI_PT_KEY\":\"148881\"}", logData, "Planned call with correct additional data should be logged"));

    }

}