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

import com.fasterxml.jackson.annotation.JsonProperty;
import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.util.DateFormatPatterns;
import cz.moneta.test.dsl.util.DateUtils;
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 lombok.AllArgsConstructor;
import lombok.Data;
import org.junit.jupiter.api.BeforeAll;

import java.time.LocalDate;

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

@TestScenario(name = "Statements for last month")
public class Statements {
    private String account;
    private String card;
    private String lastDayOfPreviousMonthWithoutSeparator;

    @BeforeAll
    public void setUp(Harness harness) {
        account = SmokeData.getInstance(harness).getAccountWithStatements();
        card = getCard(harness, account).extract(VMX_MSGOUT + ".OUTPUT_DATA.CARD_TABLE.CARD_NBRS_SEQ.CARD_NBR").textValue();
        LocalDate lastDayOfPreviousMonth = LocalDate.now().withDayOfMonth(1).minusDays(1);
        lastDayOfPreviousMonthWithoutSeparator = DateUtils.getDateWithFormat(lastDayOfPreviousMonth,
                DateFormatPatterns.FIRST_VISION_DATE_FORMAT_WITHOUT_SEPARATOR);
    }

    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T1925")
    public void VMX_STMT_REPRINT_UPD(Harness harness) {
        StatementInfo statementInfo = new StatementInfo(lastDayOfPreviousMonthWithoutSeparator, "C");
        harness.withWso2()
                .prepareRequest()
                .withPath(FV)
                .withPojoPayload(new MESSAGE("VMX.STMT.REPRINT.UPD", "R8V1"))
                .addField(VMX_MSGIN, "CARD_NBR", card)
                .addField(VMX_MSGIN, "STMT_NBR_OCCUR", "01")
                .addField(VMX_MSGIN, "STATEMENT_INFO", statementInfo)
                .postExtended()
                .andAssertFieldValue(SVC_RETURN, "P")
                .andAssertFieldValue(VMX_MSGOUT + ".OUTPUT_AREA.INPUT_ACCT", card)
                .andAssertPresent(VMX_MSGOUT + ".OUTPUT_AREA.STATEMENT_INFO");
    }

    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T3140")
    public void M_CMS_STMTOPTIONS_UPD(Harness harness) {
        harness.withWso2()
                .prepareRequest()
                .withPath(FV)
                .withPojoPayload(new MESSAGE("M.CMS.STMTOPTIONS.UPD", "R8V1"))
                .addField(VMX_MSGIN, "ACCOUNT", account)
                .addField(VMX_MSGIN, "STMT_NOTIFY_IND", "1")
                .addField(VMX_MSGIN, "STMT_NOTIFY_MOD", "1")
                .addField(VMX_MSGIN, "EMAIL_ADDR", "dummy.64@xxx.xx")
                .addField(VMX_MSGIN, "EMAIL_IND", "1")
                .postExtended()
                .andAssertFieldValue(SVC_RETURN, "P");
    }

    @Data
    @AllArgsConstructor
    private static class StatementInfo {
        @JsonProperty("STATEMENT_DATE")
        private String statementDate;
        @JsonProperty("REPRINT_FLAG")
        private String reprintFlag;
    }
}
