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

import com.fasterxml.jackson.annotation.JsonProperty;
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 lombok.Data;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

import static cz.moneta.test.dsl.util.DateFormatPatterns.FIRST_VISION_DATE_FORMAT_WITHOUT_SEPARATOR;
import static cz.moneta.test.sanity.transactional.cards.firstvision.MESSAGE.*;

@Execution(ExecutionMode.SAME_THREAD)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@TestScenario(name = "enroll")
public class Enroll {
    private final String VMX_MSGIN_MODULE3 = "VMX_MSGIN.MODULE003-VMXMSGIN.BLNC_TRNSFR_OFR_DTLS_IN";
    private String account;
    private String offerId;
    private String startDate;
    private String expireDate;
    private String offerStatus;

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

    @Order(1)
    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T3103")
    public void M_OMS_ENROLL_ADD(Harness harness) {
        RawRestRequest.Response response = harness.withWso2()
                .prepareRequest()
                .withPath(FV)
                .withPojoPayload(new MESSAGE("M.OMS.ENROLL.ADD", "R8V1"))
                .addField(VMX_MSGIN, "ACCT_CUST_NBR", account)
                .addField(VMX_MSGIN, "OFFER_ID", offerId)
                .addField(VMX_MSGIN, "OFFER_PRIORITY", "1")
                .addField(VMX_MSGIN, "ENROLL_ACTION", "0")
                .postExtended();
        String svcReturn = response
                .extract(SVC_RETURN).textValue();

        if ("F".equalsIgnoreCase(svcReturn)) {
            response.andAssertFieldValue(VMX_MSGOUT + ".RC_DATA.RETURN_DESC", "OFFER ENROLLMENT ALREADY ON FILE");
        } else {
            response.andAssertFieldValue(SVC_RETURN, "P");
        }
    }

    @Order(2)
    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T3104")
    public void OMS_ENROLL_INQ(Harness harness) {
        RawRestRequest.Response response = harness.withWso2()
                .prepareRequest()
                .withPath(FV)
                .withPojoPayload(new MESSAGE("OMS.ENROLL.INQ", "R8V4"))
                .addField(VMX_MSGIN, "ACCT_NBR", account)
                .addField(VMX_MSGIN, "OFFER_ID", offerId)
                .postExtended()
                .andAssertFieldValue(SVC_RETURN, "P")
                .andAssertFieldValue(VMX_MSGOUT + ".OUTPUT-AREA.OFFER_ID", offerId);

        startDate = response.extract(VMX_MSGOUT + ".OUTPUT-AREA.START_DATE").textValue();
        expireDate = response.extract(VMX_MSGOUT + ".OUTPUT-AREA.EXPIRE_DATE").textValue();
        offerStatus = response.extract(VMX_MSGOUT + ".OUTPUT-AREA.OFFER_STATUS").textValue();

    }

    @Order(3)
    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T3141")
    public void M_OMS_ENROLL_UPD(Harness harness) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(FIRST_VISION_DATE_FORMAT_WITHOUT_SEPARATOR);
        String newExpire = LocalDate.parse(expireDate, formatter).minusDays(1).format(formatter);
        harness.withWso2()
                .prepareRequest()
                .withPath(FV)
                .withPojoPayload(new MESSAGE("M.OMS.ENROLL.UPD", "R8V2"))
                .addField(VMX_MSGIN, "ACCT_NBR", account)
                .addField(VMX_MSGIN, "OFFER_ID", offerId)
                .addField(VMX_MSGIN, "ENROLLMENT_ACTION", "")
                .addField(VMX_MSGIN, "PRIORITY", "1")
                .addField(VMX_MSGIN, "START_DATE", startDate)
                .addField(VMX_MSGIN, "EXPIRE_DATE", newExpire)
                .addField(VMX_MSGIN, "OFFER_STATUS", offerStatus)
                .postExtended()
                .andAssertFieldValue(SVC_RETURN, "P")
                .andAssertFieldValue(VMX_MSGOUT + ".OFFER_ID", offerId);
    }

    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T4622")
    public void M_OMS_OFFERDEF_INQ(Harness harness) {
        harness.withWso2()
                .prepareRequest()
                .withPath(FV)
                .withPojoPayload(new MESSAGE("M.OMS.OFFERDEF.INQ", "R8V3"))
                .addField(VMX_MSGIN, "MODULE003-VMXMSGIN", new MODULE003_VMXMSGIN())
                .addField("VMX_MSGIN.MODULE003-VMXMSGIN", "BLNC_TRNSFR_OFR_DTLS_IN", new BLNC_TRNSFR_OFR_DTLS_IN())
                .addField(VMX_MSGIN_MODULE3, "ORG", "861")
                .addField(VMX_MSGIN_MODULE3, "OFFER_CD", offerId)
                .postExtended()
                .andAssertFieldValue(VMX_MSGOUT + ".SVC_RETURN", "P");
    }

    @Data
    private static class MODULE003_VMXMSGIN {
        @JsonProperty("BLNC_TRNSFR_OFR_DTLS_IN")
        private String module003;
    }

    @Data
    private static class BLNC_TRNSFR_OFR_DTLS_IN {
        @JsonProperty("ORG")
        private String org;
        @JsonProperty("OFFER_CD")
        private String offerCD;
    }
}
