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

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.util.DateFormatPatterns;
import cz.moneta.test.dsl.util.DateUtils;
import cz.moneta.test.dsl.wso2.data.CardData;
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.*;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;

import java.time.LocalDate;

@Execution(ExecutionMode.SAME_THREAD)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@TestScenario(name = "Block Unblock card")
public class BlockUnblock {
    private String card;
    private String account;
    private String bu;
    private SmokeData smokeData;

    @BeforeAll
    public void getInfo(Harness harness) {
        smokeData = SmokeData.getInstance(harness);
        CardData debitData = smokeData.getDebitData();
        card = debitData.getCard();
        account = debitData.getAccount();
        bu = debitData.getBu();
    }

    @Order(1)
    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T1943")
    public void MWFA63_DO_CARD_SERVICING_BLOCK_CAR(Harness harness) {
        String desc = RandomStringUtils.randomAlphabetic(10);
        harness.tasks().wso2().restRequest()
                .prepareEmptyRequestFromUfo(smokeData.getPath("MWFA63"))
                .addField("CisloKarty", card)
                .addField("DruhBlokace", "25")
                .addField("DuvodAkce", desc)
                .postExtended()
                .andAssertStatus(200)
                .andAssertFieldValue("DuvodAkce", desc);
    }

    @Order(2)
    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T2403")
    public void MWFA86_DO_CARD_SERVICING_UNLOCKING(Harness harness) {
        unblockCard(harness, card)
                .andAssertStatus(200)
                .andAssertFieldValue("VysledekKontrol", "OK");
    }

    @Order(3)
    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T2416")
    public void MWF2E0_ODS_BLOCK_CARD(Harness harness) {
        harness.tasks().wso2().restRequest()
                .prepareEmptyRequestFromUfo(smokeData.getPath("MWF2E0"))
                .addField("CardInstanceNumber", card)
                .addField("CardInstanceBlockCode", "U")
                .postExtended()
                .andAssertStatus(200);
    }

    @Order(4)
    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T2418")
    public void MWF2E2_ODS_UNBLOCK_CARD(Harness harness) {
        harness.tasks().wso2().restRequest()
                .prepareEmptyRequestFromUfo(smokeData.getPath("MWF2E2"))
                .addField("CardInstanceNumber", card)
                .postExtended()
                .andAssertStatus(200);
    }

    @Order(5)
    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T2417")
    public void MWF2E1_ODS_BLOCK_ACCOUNT(Harness harness) {
        String blockCode = "D";
        harness.tasks().wso2().restRequest()
                .prepareEmptyRequestFromUfo(smokeData.getPath("MWF2E1"))
                .addField("CardAccountNumber", account)
                .addField("CardAccountBlockCode1", "")
                .addField("CardAccountBlockCode2", blockCode)
                .postExtended()
                .andAssertStatus(200);
    }

    @Order(6)
    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T2433")
    public void MWFJW3_DO_CARD_SERV_RECALL_CANCELLING(Harness harness) {
        harness.tasks().wso2().restRequest()
                .prepareEmptyRequestFromUfo(smokeData.getPath("MWFJW3"))
                .addField("CisloKaretnihoUctu", account)
                .postExtended()
                .andAssertStatus(200);
    }

    @Order(7)
    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T2419")
    public void MWF2E3_ODS_UNBLOCK_ACCOUNT(Harness harness) {
        harness.tasks().wso2().restRequest()
                .prepareEmptyRequestFromUfo(smokeData.getPath("MWF2E3"))
                .addField("CardAccountNumber", account)
                .postExtended()
                .andAssertStatus(200);
    }

    @Order(8)
    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T2093")
    public void MWFA88_DO_CARD_SERVICING_STOP_RENEWAL(Harness harness) {
        harness.tasks().wso2().restRequest()
                .prepareEmptyRequestFromUfo(smokeData.getPath("MWFA88"))
                .addField("CisloKarty", card)
                .postExtended()
                .andAssertStatus(200);
    }

    @Order(9)
    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T2098")
    public void MWFII9_DO_CARD_SERV_UNBLOCK_WTH_RENEW(Harness harness) {
        harness.tasks().wso2().restRequest()
                .prepareEmptyRequestFromUfo(smokeData.getPath("MWFII9"))
                .addField("CisloKarty", card)
                .postExtended()
                .andAssertStatus(200);
    }

    @Order(10)
    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T2415")
    public void MWF2DV_BATCH_UPDATES_BLOCK_DEBIT_CARD(Harness harness) {
        harness.tasks().wso2().restRequest()
                .prepareEmptyRequestFromIcb(smokeData.getPath("MWF2DV"))
                .addField("CurrentAccountNumber", bu)
                .post()
                .andAssertStatus(200);
    }

    @Order(11)
    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T2782")
    public void MWFXX11_CARD_SERVICING_CARD_BLOCK_UPD(Harness harness) {
        harness.tasks().wso2().restRequest()
                .prepareEmptyRequestFromUfo(smokeData.getPath("MWFXX11"))
                .addField("CisloKarty", card)
                .addField("DuvodZruseni", "7")
                .postExtended()
                .andAssertStatus(200);
    }

    @Order(12)
    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T2091")
    public void MWFA83_CRT_CARD_SERVICING_TERMINATION(Harness harness) {
        harness.tasks().wso2().restRequest()
                .prepareEmptyRequestFromUfo(smokeData.getPath("MWFA83"))
                .addField("CisloKaretnihoUctu", account)
                .addField("DuvodZruseni", "6")
                .postExtended()
                .andAssertStatus(200)
                .andAssertFieldValue("Result", "OK");
        unblockAccount(harness, account);
    }

    @Order(13)
    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T2099")
    public void MWFXXX2_DO_ACCOUNT_ACTION_WRITE_OFF(Harness harness) {
        String cancelDateString = DateUtils.getDateWithFormat(LocalDate.now().plusDays(7),
                DateFormatPatterns.FIRST_VISION_DATE_FORMAT_WITH_SEPARATOR);
        harness.tasks().wso2().restRequest()
                .prepareEmptyRequestFromUfo(smokeData.getPath("MWFXXX2"))
                .addField("ACCT_NBR", account)
                .addField("ORG", "3")
                .addField("TC_EFF_DATE", cancelDateString)
                .postExtended()
                .andAssertStatus(200)
                .andAssertFieldValue("ACCT_NBR", account)
                .andAssertFieldValue("ORG", "3");
        unblockAccount(harness, account);
    }

    private void unblockAccount(Harness harness, String account) {
        harness.tasks().wso2().restRequest()
                .prepareEmptyRequestFromUfo(smokeData.getPath("MWFJW3"))
                .addField("CisloKaretnihoUctu", account)
                .postExtended();
    }

    private RawRestRequest.Response unblockCard(Harness harness, String card) {
        return harness.tasks().wso2().restRequest()
                .prepareEmptyRequestFromUfo(smokeData.getPath("MWFA86"))
                .addField("CisloKarty", card)
                .addField("DuvodOdblokovani", "21")
                .addField("OdblokokaceKaretnihoUctu", "1")
                .addField("VypnoutKontroly", "1")
                .postExtended();
    }

    @AfterAll
    public void unblockAfterTests(Harness harness) {
        unblockCard(harness, card);
    }
}
