package cz.moneta.test.sanity.transactional.cards.firstvision;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.harness.annotations.Environment;
import cz.moneta.test.harness.annotations.JiraTestCase;
import cz.moneta.test.harness.annotations.TestCase;
import cz.moneta.test.harness.annotations.TestScenario;
import cz.moneta.test.harness.support.rest.RawRestRequest;
import cz.moneta.test.sanity.transactional.SmokeData;
import org.junit.jupiter.api.BeforeAll;

import static cz.moneta.test.sanity.transactional.cards.firstvision.MESSAGE.*;

@TestScenario(name = "R6 VMX.INSURANCE.STATUS.UPD.E8V2")
public class Insurance {
    private String account;

    @BeforeAll
    public void getInfo(Harness harness) {
        account = SmokeData.getInstance(harness).getCreditData().getAccount();
    }

    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T1928")
    public void VMX_INSURANCE_STATUS_UPD(Harness harness) {
        harness.withWso2()
                .prepareRequest()
                .withPath(FV)
                .withPojoPayload(new MESSAGE("VMX.INSURANCE.STATUS.UPD", "E8V3"))
                .addField(VMX_MSGIN, "ACCOUNT", account)
                .addField(VMX_MSGIN, "INS_PROD_CODE", "DD")
                .addField(VMX_MSGIN, "INS_STATUS_CODE", "F")
                .addField(VMX_MSGIN, "INS_EFF_DATE", "20190520")
                .postExtended()
                .andAssertFieldValue(SVC_RETURN, "P")
                .andAssertFieldValue(VMX_MSGOUT + ".OUTPUT_AREA.ACCOUNT", account)
                .andAssertFieldValue(VMX_MSGOUT + ".OUTPUT_AREA.INS_PROD_CODE", "DD")
                .andAssertFieldValue(VMX_MSGOUT + ".OUTPUT_AREA.INS_STATUS_CODE", "F");
    }

    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T3138")
    public void VMX_ACCT_INSURANCE_ADD(Harness harness) {
        RawRestRequest.Response response = harness.withWso2()
                .prepareRequest()
                .withPath(FV)
                .withPojoPayload(new MESSAGE("VMX.ACCT.INSURANCE.ADD", "E8V2"))
                .addField(VMX_MSGIN, "ACCOUNT", account)
                .addField(VMX_MSGIN, "INS_PROD_CD", "DD")
                .addField(VMX_MSGIN, "INS_STAT_CD", "F")
                .postExtended();
        String status = response.extract(SVC_RETURN).textValue();

        if ("P".equalsIgnoreCase(status)) {
            response.andAssertFieldValue(SVC_RETURN, "P")
                    .andAssertFieldValue(VMX_MSGOUT + ".OUTPUT_AREA.ACCOUNT", account)
                    .andAssertFieldValue(VMX_MSGOUT + ".OUTPUT_AREA.INS_PROD_CODE", "DD")
                    .andAssertFieldValue(VMX_MSGOUT + ".OUTPUT_AREA.INS_STATUS_CODE", "F");
        } else {
            response.andAssertFieldValue(VMX_MSGOUT + ".RETURN_CODE_DATA.RC_DATA.RETURN_DESC",
                    "INSURANCE PRODUCT ALREADY ADDED FOR THIS ACCOUNT");
        }
    }
}
