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

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

import java.time.LocalDate;

@TestScenario(name = "CARD TRANSACTIONS")
public class CardTransactions {
    private String account;
    private String accountStatements;
    private String card;
    private SmokeData smokeData;
    private String lastDayOfPreviousMonth;

    @BeforeAll
    public void getInfo(Harness harness) {
        smokeData = SmokeData.getInstance(harness);
        CardData creditData = smokeData.getCreditData();
        account = creditData.getAccount();
        accountStatements = smokeData.getAccountWithStatements();
        card = creditData.getCard();
        lastDayOfPreviousMonth = DateUtils.getDateWithFormat(LocalDate.now().withDayOfMonth(1).minusDays(1),
                DateFormatPatterns.FIRST_VISION_DATE_FORMAT_WITH_SEPARATOR);
    }

    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T1936")
    public void MWF485GetTransactionAuthorizList(Harness harness) {
        int count = harness.tasks().wso2().restRequest()
                .prepareEmptyRequestFromCbl(smokeData.getPath("MWF485"))
                .addField("CardAccountNumber", account)
                .addField("Org", "500")
                .postExtended()
                .andAssertStatus(200)
                .extract("Data")
                .size();
        Assertions.assertTrue(count > 0, "No authorized transactions on CC");
    }

    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T1933")
    public void MWF235GetTransactBlockListCRDNM(Harness harness) {
        int count = harness.tasks().wso2().restRequest()
                .prepareEmptyRequestFromUfo(smokeData.getPath("MWF235"))
                .addField("CisloKarty", card)
                .postExtended()
                .andAssertStatus(200)
                .andAssertPresent("Pocet")
                .andAssertPresent("Data")
                .extract("Data")
                .size();
        Assertions.assertTrue(count > 0, "No blocked transactions on CC");
    }

    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T1958")
    public void MWFDB1GetTransactionMiniStatement(Harness harness) {
        int count = harness.tasks().wso2().restRequest()
                .prepareEmptyRequestFromAtm(smokeData.getPath("MWFDB1"))
                .addField("CisloKarty", card)
                .addField("DelkaPopisuTransakce", 10)
                .addField("PocetTransakci", 3)
                .postExtended()
                .andAssertStatus(200)
                .andAssertPresent("Data[0].Castka")
                .extract("Data")
                .size();
        Assertions.assertTrue(count > 0, "No transactions on card");
    }

    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T2426")
    public void MWFA90TransactionGetList(Harness harness) {
        harness.tasks().wso2().restRequest()
                .prepareEmptyRequestFromUfo(smokeData.getPath("MWFA90"))
                .addField("CisloKaretnihoUctu", account)
                .postExtended()
                .andAssertStatus(200)
                .andAssertPresent("UcetZpusobUhrady")
                .andAssertPresent("UcetKodBanky")
                .andAssertPresent("UcetCisloBU")
                .andAssertPresent("UcetSpecifickySymbol")
                .andAssertPresent("UcetZpusubUhradyJednoduchy")
                .andAssertPresent("Data");
    }

    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T2702")
    public void MWFA89TransactionGetListOnStatement(Harness harness) {
        RawRestRequest.Response response = harness.tasks().wso2().restRequest()
                .prepareEmptyRequestFromUfo(smokeData.getPath("MWFA89"))
                .addField("CisloKaretnihoUctu", accountStatements)
                .addField("DruhKarty", "1")
                .addField("DatumVypisu", lastDayOfPreviousMonth)
                .postExtended();
        int count = response.andAssertStatus(200)
                .andAssertPresent("Pocet")
                .extract("Data").size();
        Assertions.assertTrue(count > 0, "No transaction on CC");
    }

    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T2090")
    public void MWFA80GetStatementsList(Harness harness) {
        RawRestRequest.Response response = harness.tasks().wso2().restRequest()
                .prepareEmptyRequestFromUfo(smokeData.getPath("MWFA80"))
                .addField("CisloKaretnihoUctu", accountStatements)
                .addField("DruhKarty", "1")
                .postExtended()
                .andAssertStatus(200)
                .andAssertPresent("Pocet")
                .andAssertPresent("Data[0].DatumVypisu")
                .andAssertPresent("Data[0].DisponibilniZustatek")
                .andAssertPresent("Data[0].DatumGenerovaniVypisu")
                .andAssertPresent("Data[0].CelkovaSplatnaSplatka")
                .andAssertPresent("Data[0].VyseVycerpanehoRamceKonec")
                .andAssertPresent("Data[0].VyseVycerpanehoRamceZacatek")
                .andAssertPresent("Data[0].SplatkaBezneObdobi");
        String pocet = response.extract("Pocet").asText();
        Assertions.assertTrue(Double.valueOf(pocet) > 0, "No statements on account " + account);
    }

    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T2701")
    public void MWFXXX3TransactionsGetTransactions(Harness harness) {
        String recCount = harness.tasks().wso2().restRequest()
                .prepareEmptyRequestFromUfo(smokeData.getPath("MWFXXX3"))
                .addField("ACCOUNT", account)
                .postExtended()
                .andAssertStatus(200)
                .andAssertPresent("NBR_TXN_RECS")
                .extract("NBR_TXN_RECS").asText();
        Assertions.assertTrue(Integer.valueOf(recCount) > 0, "no transaction on account " + account);
    }
}