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.AfterAll;
import org.junit.jupiter.api.BeforeAll;

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

@TestScenario(name = "M.CMS.ACCTTRANSFER.ACT.EMEA E8V4")
public class AccountTransfer {
    private String account;

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

    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T1912")
    public void transferAccountAndReverse(Harness harness) {
        RawRestRequest.Response response = prepareRequest(harness, "P")// product transfer
                .postExtended()
                .andAssertStatus(200);
        if (response.extract(SVC_RETURN).textValue().equals("F")) {
            response.andAssertFieldValue(VMX_MSGOUT + ".RC_DATA.RETURN_DESC",
                    "ACCT HAS ALREADY BEEN TRANSFERRED TODAY");
        } else
            response.andAssertFieldValue(SVC_RETURN, "P");
    }

    private RawRestRequest.Post prepareRequest(Harness harness, String function) {
        return harness.withWso2()
                .prepareRequest()
                .withPath(FV)
                .withPojoPayload(new MESSAGE("M.CMS.ACCTTRANSFER.ACT.EMEA", "E8V4"))
                .addField(VMX_MSGIN, "FUNCTION", function)// P/R  forward/revert product transfer
                .addField(VMX_MSGIN, "NEW_ACCT_LOGO", "470")
                .addField(VMX_MSGIN, "CYCLE", "10")  //day in month
                .addField(VMX_MSGIN, "FROM_ACCT_NBR", account);
    }

    @AfterAll
    public void reverseTransfer(Harness harness) {
        prepareRequest(harness, "R").post();
    }
}
