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

import com.fasterxml.jackson.databind.JsonNode;
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.*;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;

import java.util.stream.StreamSupport;

@Execution(ExecutionMode.SAME_THREAD)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@TestScenario(name = "Bundle")
public class CardBundles {
    private String idBundle;
    private String account;
    private SmokeData smokeData;

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

    @Order(1)
    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T2730")
    public void MWFJZ3_VLS_SET_BUNDLE(Harness harness) {
        RawRestRequest.Response response = harness.tasks().wso2().restRequest()
                .prepareEmptyRequestFromUfo(smokeData.getPath("MWFJZ3"))
                .addField("ACCOUNT", account)
                .addField("CHANNEL", "POS")
                .addField("ID_BUNDLE", idBundle)
                .addField("SSO", "543830300")
                .postExtended();
        if (response.getStatus() == 500) {
            response.andAssertFieldValue("tifResponseStatus.errorDesc", "VPLBSAE20S: OFFER ENROLLMENT ALREADY ON FILE");
        } else
            response.andAssertStatus(200);
    }

    @Order(2)
    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T2731")
    public void MWFJZ2_VLS_GET_BUNDLE_HISTORY(Harness harness) {
        JsonNode data = harness.tasks().wso2().restRequest()
                .prepareEmptyRequestFromUfo(smokeData.getPath("MWFJZ2"))
                .addField("ACCOUNT", account)
                .postExtended()
                .andAssertStatus(200)
                .andAssertPresent("Data")
                .andAssertPresent("NBR_OF_REC")
                .extract("Data");
        boolean bundleFound = StreamSupport.stream(data.spliterator(), false)
                .map(i -> i.path("RS_ID_BUNDLE").textValue())
                .anyMatch(idBundle::equals);
        Assertions.assertTrue(bundleFound, "Bundle " + idBundle + " not found on card");
    }

    @Order(3)
    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T2732")
    public void MWFJZ5_VLS_GET_ACCOUNT(Harness harness) {
        harness.tasks().wso2().restRequest()
                .prepareEmptyRequestFromUfo(smokeData.getPath("MWFJZ5"))
                .addField("ACCOUNT", account)
                .postExtended()
                .andAssertStatus(200)
                .andAssertFieldValue("ID_BUNDLE_ACT", idBundle)
                .andAssertPresent("ACT_BUNDLE_ACTIVE_FROM");
    }

    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T3149")
    public void MWFC14_VLS_GET_CARD_DETAIL(Harness harness) {
        harness.tasks().wso2().restRequest()
                .prepareEmptyRequestFromUfo(smokeData.getPath("MWFC14"))
                .addField("CisloKaretnihoUctu", account)
                .post()
                .andAssertStatus(200)
                .andAssertPresent("BodyDoKonceMesice")
                .andAssertPresent("CelkovyAktualniPocetOdmen")
                .andAssertPresent("OdmenyOdZacatkuRoku")
                .andAssertPresent("OdmenyOdVydaniKarty")
                .andAssertPresent("Pocet")
                .andAssertPresent("Data")
                .andAssertPresent("DatumDalsihoVypisu")
                .andAssertPresent("CelkovyAktualniPocetBodu");
    }
}
