package cz.moneta.test.sanity.transactional.cards.firstvision.pilotfirstvision;

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.sanity.transactional.SmokeData;
import cz.moneta.test.sanity.transactional.cards.firstvision.MESSAGE;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;

import java.time.LocalDate;

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

@TestScenario(name = "Customer transactions/statements")
public class Transactions {
    private String account;
    private String accountStatement;
    private String lastDayOfPreviousMonthWithSeparator;

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

    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T1914")
    public void MFasOutstandingAuthInq(Harness harness) {
        Integer authCount = Integer.valueOf(harness.withWso2()
                .prepareRequest()
                .withPath(FV)
                .withPojoPayload(new MESSAGE("M.FAS.OUTSTANDING.AUTH.INQ", "R8V3A"))
                .addField(VMX_MSGIN, "ACCT", account)
                .addField(VMX_MSGIN, "ORG", "861")
                .postExtended()
                .andAssertFieldValue(SVC_RETURN, "P")
                .extract(VMX_MSGOUT + ".NBR_RETURNED_ITEMS").textValue());
        Assertions.assertTrue(authCount > 0, "No outstanding auth found");
    }

    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T1926")
    public void VmxStmtSummInq(Harness harness) {
        harness.withWso2()
                .prepareRequest()
                .withPath(FV)
                .withPojoPayload(new MESSAGE("VMX.STMT.SUMM.INQ", "E8V5A"))
                .addField(VMX_MSGIN, "ACCOUNT", accountStatement)
                .addField(VMX_MSGIN, "SERVICE_TYPE", "S")
                .addField(VMX_MSGIN, "STATEMENT_DATE", lastDayOfPreviousMonthWithSeparator)
                .postExtended()
                .andAssertFieldValue(SVC_RETURN, "P");
    }

    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T3142")
    public void VmxTransInq(Harness harness) {
        String count = harness.withWso2()
                .prepareRequest()
                .withPath(FV)
                .withPojoPayload(new MESSAGE("VMX.TRANS.INQ", "E8VD"))
                .addField(VMX_MSGIN, "ACCOUNT", account)
                .postExtended()
                .andAssertFieldValue(SVC_RETURN, "P")
                .extract(VMX_MSGOUT + ".NBR_TXN_RECS")
                .asText();
        Assertions.assertTrue(Integer.valueOf(count) > 0, "No transactions on account " + account);
    }

    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T2779")
    public void VmxStatementDateInqEMEA(Harness harness) {
        String count = harness.withWso2()
                .prepareRequest()
                .withPath(FV)
                .withPojoPayload(new MESSAGE("VMX.STATEMENTDATE.INQ.EMEA", "E8V1"))
                .addField(VMX_MSGIN, "ACCOUNT", accountStatement)
                .postExtended()
                .andAssertFieldValue(SVC_RETURN, "P")
                .extract(VMX_MSGOUT + ".SD_NBR_OCCUR")
                .textValue();
        Assertions.assertTrue(Integer.valueOf(count) > 0, "No transactions on account " + accountStatement);
    }

    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T2756")
    public void MCmsStatementInqEMEA(Harness harness) {
        harness.withWso2()
                .prepareRequest()
                .withPath(FV)
                .withPojoPayload(new MESSAGE("M.CMS.STATEMENT.INQ.EMEA", "E8V2"))
                .addField(VMX_MSGIN, "ACCOUNT", accountStatement)
                .addField(VMX_MSGIN, "STMT_DT", lastDayOfPreviousMonthWithSeparator)
                .addField(VMX_MSGIN, "SVC_TYP", "S")
                .postExtended()
                .andAssertFieldValue(SVC_RETURN, "P")
                .andAssertFieldValue(VMX_MSGOUT + ".ACCT_NBR", accountStatement);
    }


}
