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

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JsonNode;
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.harness.support.util.Level;
import cz.moneta.test.sanity.transactional.SmokeData;
import lombok.Builder;
import lombok.Data;
import org.junit.jupiter.api.BeforeAll;

import java.util.stream.StreamSupport;

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

@TestScenario(name = "VMX.LOSTSTOLEN.UPD.E8V3")
public class LostStolenCard {
    private String cardToLoose;
    private SmokeData smokeData;

    @BeforeAll
    public void getInfo(Harness harness) {
        smokeData = SmokeData.getInstance(harness);
        String lostAccount = smokeData.getLostAccount();
        cardToLoose = getActiveCard(harness, lostAccount);
    }

    private String getActiveCard(Harness harness, String account) {
        JsonNode data = harness.tasks().wso2().restRequest() //fixme find another API  to get active card
                .prepareEmptyRequestFromUfo(smokeData.getPath("MWFBQ7"))
                .addField("CisloKaretnihoUctu", account)
                .postExtended()
                .extract("Data");
        harness.log(Level.INFO, "Total cards on account:" + data.size());
        return StreamSupport.stream(data.spliterator(), false)
                .filter(i -> "".equalsIgnoreCase(i.path("DuvodBlokace").textValue()))
                .map(i -> i.path("CisloKarty").textValue())
                .findFirst()
                .orElse("");
    }

    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T1922")
    public void looseActiveCard(Harness harness) {
        String today = DateUtils.getFormattedTodayDate(DateFormatPatterns.FIRST_VISION_DATE_FORMAT_WITHOUT_SEPARATOR);
        harness.withWso2()
                .prepareRequest()
                .withPath(FV)
                .withPojoPayload(new MESSAGE("VMX.LOSTSTOLEN.UPD", "E8V3"))
                .addField(VMX_MSGIN, "MODULE005-VMXMSGIN", new MessageIn())
                .addField(VMX_MSGIN + ".MODULE005-VMXMSGIN.RPT_LST_STOLEN_CRD_IN", "CARD_NBR", cardToLoose)
                .addField(VMX_MSGIN + ".MODULE005-VMXMSGIN.RPT_LST_STOLEN_CRD_IN", "LOSS_DATE", today)
                .postExtended()
                .andAssertFieldValue(SVC_RETURN, "P");
    }

    @Data
    private static class MessageIn {
        @JsonProperty("RPT_LST_STOLEN_CRD_IN")
        private LostStolenCardIn module;

        private MessageIn() {
            this.module = LostStolenCardIn.builder()
                    .cardBlock("L")
                    .lossType("1")
                    .countryOfLoss("CZE")
                    .lossLocation("5")
                    .build();
        }
    }

    @Data
    @Builder
    private static class LostStolenCardIn {
        @JsonProperty("CARD_BLK")
        private String cardBlock;
        @JsonProperty("LOSS_TYPE")
        private String lossType;
        @JsonProperty("COUNTRY_OF_LOSS")
        private String countryOfLoss;
        @JsonProperty("LOSS_LOCATION")
        private String lossLocation;
    }
}
