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

import java.time.LocalDate;
import java.util.Random;

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

@TestScenario(name = "VMX.ACCT.DIRECTDB.UPD E8V5")
public class DirectDebit {
    private String inputDate;
    private String outputDate;
    private String account;

    @BeforeAll
    public void getInfo(Harness harness) {
        account = SmokeData.getInstance(harness).getCreditData().getAccount();
        LocalDate expireDate = LocalDate.now().plusMonths(1).plusDays(new Random().nextInt(30));
        inputDate = DateUtils.getDateWithFormat(expireDate, DateFormatPatterns.FIRST_VISION_DATE_FORMAT_WITHOUT_SEPARATOR);
        outputDate = DateUtils.getDateWithFormat(expireDate, DateFormatPatterns.FIRST_VISION_DATE_MODERN_JULIAN_FORMAT);
    }

    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T4630")
    public void VMX_ACCT_DIRECTDB_UPD(Harness harness) {
        harness.withWso2()
                .prepareRequest()
                .withPath(FV)
                .withPojoPayload(new MESSAGE("VMX.ACCT.DIRECTDB.UPD", "E8V5"))
                .addField(VMX_MSGIN, "ACCT_NBR", account)
                .addField(VMX_MSGIN, "DD_DATA", new DD_DATA())
                .addField(VMX_MSGIN + ".DD_DATA", "ACH_PMT_EXPR_DATE", inputDate)
                .postExtended()
                .andAssertFieldValue(SVC_RETURN, "P")
                .andAssertFieldValue(VMX_MSGOUT + ".DATE_ACH_PMT_EXPR", outputDate);
    }

    @Data
    private static class DD_DATA {
        @JsonProperty("ACH_PMT_EXPR_DATE")
        private String date;
    }
}
