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

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.wso2.Wso2Utils;
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 cz.moneta.test.sanity.transactional.cards.firstvision.MESSAGE;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;

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

@Execution(ExecutionMode.SAME_THREAD)
@TestScenario(name = "renewal of card/accounts")
public class RenewalProcess {
    private String cardRenew;
    private String debitCard;
    private SmokeData smokeData;

    @BeforeAll
    public void getInfo(Harness harness) {
        smokeData = SmokeData.getInstance(harness);
        cardRenew = smokeData.getCardForRenew();
        debitCard = smokeData.getDebitData().getCard();
    }

    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T2784")
    public void MWF208_add_services_card_renewal(Harness harness) {
        RawRestRequest.Response response = harness.tasks().wso2().restRequest()
                .prepareEmptyRequestFromUfo(smokeData.getPath("MWF208"))
                .addField("CisloKarty", debitCard)
                .addField("KodDS", "PP")
                .postExtended();
        //fixme card renewal is possible only once per batch period, else got 500. Is it by design ?
        Wso2Utils.validateResponse(response,
                500,
                smokeData.isGw(),
                "tifResponseStatus.errorDesc",
                "VPL5SPR29S: CARD ACTION CHANGE NOT ALLOWED DUE TO PENDING CARD ACTION",
                "errorMessage",
                "CARD ACTION CHANGE NOT ALLOWED DUE TO PENDING CARD ACTION");
    }

    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T2089")
    public void MWF2E5_DO_ONBOARD_RENEW_ACC_BY_CARD(Harness harness) {
        harness.tasks().wso2().restRequest()
                .prepareEmptyRequestFromHub(smokeData.getPath("MWF2E5"))
                .addField("CardInstanceNumber", debitCard)
                .addField("CardTargetInstanceNumber", debitCard)
                .postExtended()
                .andAssertStatus(200);
    }

    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T1932")
    public void MWF209_DO_CARD_SERVICING_REPLICA(Harness harness) {
        RawRestRequest.Response response = harness.tasks().wso2().restRequest()
                .prepareEmptyRequestFromUfo(smokeData.getPath("MWF209"))
                .addField("CisloPK", debitCard)
                .addField("DuvodObnovy", "22") //obnova karty
                .postExtended();
        //fixme cardReplica only once per batch repiod
        Wso2Utils.validateResponse(response, 500, smokeData.isGw(),
                "tifResponseStatus.errorDesc",
                "VPL5SPR29S: CARD ACTION CHANGE NOT ALLOWED DUE TO PENDING CARD ACTION",
                "errorMessage",
                "CARD ACTION CHANGE NOT ALLOWED DUE TO PENDING CARD ACTION");
    }

    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T2425")
    public void MWFA87_CARD_SERVICING_DO_RENEWAL(Harness harness) {
        blockCard(harness, cardRenew);
        RawRestRequest.Response response = harness.tasks().wso2().restRequest()
                .prepareEmptyRequestFromUfo(smokeData.getPath("MWFA87"))
                .addField("CisloKarty", cardRenew)
                .addField("DuvodObnovy", "22")// Obnova PK
                .postExtended();
        //fixme  one time action during batch period
        Wso2Utils.validateResponse(response, 500, smokeData.isGw(),
                "tifResponseStatus.errorDesc",
                "VPL5SLS28E: CARD ALREADY TRANSFERRED CANNOT TRANSFER AGAIN",
                "errorMessage",
                "CARD ALREADY TRANSFERRED CANNOT TRANSFER AGAIN");
    }

    private void blockCard(Harness harness, String card) {
        harness.withWso2()
                .prepareRequest()
                .withPath(FV)
                .withPojoPayload(new MESSAGE("M.CMS.BLOCKCODE.UPD.EMEA", "E8V1"))
                .addField("VMX_MSGIN", "CARD_NBR", card)
                .addField("VMX_MSGIN", "SVC_FUNC_CODE", "U")
                .addField("VMX_MSGIN", "BLOCK_CODE", "L") // lost card
                .addField("VMX_MSGIN", "RSN_BLOCK_CODE_1", "01")
                .postExtended();
    }
}
