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.apache.commons.lang3.RandomStringUtils;
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 static cz.moneta.test.sanity.transactional.cards.firstvision.MESSAGE.*;

@Execution(ExecutionMode.SAME_THREAD)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@TestScenario(name = "Embosser update")
public class Embosser {
    private String account;
    private String card;
    private String newEmbosser;
    private String embosser;

    @BeforeAll
    public void getInfo(Harness harness) {
        SmokeData smokeData = SmokeData.getInstance(harness);
        account = smokeData.getCreditData().getAccount();
        card = smokeData.getCreditData().getCard();
        newEmbosser = RandomStringUtils.randomAlphabetic(13);
        embosser = smokeData.getCreditData().getName();
    }

    @Order(1)
    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T3144")
    public void VMX_EMBOSSER_UPD_EMEA_E8VBA(Harness harness) {
        harness.withWso2()
                .prepareRequest()
                .withPath(FV)
                .withPojoPayload(new MESSAGE("VMX.EMBOSSER.UPD.EMEA", "E8VBA"))
                .addField(VMX_MSGIN, "CARD_NBR", card)
                .addField(VMX_MSGIN, "ORG", "861")
                .addField(VMX_MSGIN, "CARD_SEQ", "1")
                .addField(VMX_MSGIN, "FOREIGN_USE", "0")
                .addField(VMX_MSGIN, "EMBR_NAME_1", newEmbosser)
                .postExtended()
                .andAssertFieldValue(SVC_RETURN, "P")
                .andAssertFieldValue(VMX_MSGOUT + ".OUTPUT_DATA.CARD_NBR", card)
                .andAssertFieldValue(VMX_MSGOUT + ".OUTPUT_DATA.EMBR_NAME_1", newEmbosser);
    }

    @Order(2)
    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T1902")
    public void VMX_EMBOSSER_INQ_E8V7B(Harness harness) {
        getEmbosser(harness, "E8V7B", card)
                .andAssertFieldValue(VMX_MSGOUT + ".CARD_NBR", card)
                .andAssertFieldValue(VMX_MSGOUT + ".POST_NBR", account)
                .andAssertFieldValue(VMX_MSGOUT + ".EMB_NAME_1", embosser.toUpperCase())
                .extract(VMX_MSGOUT + ".USER_3").textValue();
    }

    public static RawRestRequest.Response getEmbosser(Harness harness, String version, String card) {
        return harness.withWso2()
                .prepareRequest()
                .withPath(FV)
                .withPojoPayload(new MESSAGE("VMX.EMBOSSER.INQ", version))
                .addField(VMX_MSGIN, "ACCOUNT", card)
                .postExtended()
                .andAssertFieldValue(SVC_RETURN, "P");
    }
}
