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

import com.fasterxml.jackson.databind.JsonNode;
import cz.moneta.test.dsl.Harness;
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 org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;

import java.util.stream.StreamSupport;

@TestScenario(name = "Card disposition")
public class ListCard {
    private String contract;
    private String creditCard;
    private String debitCard;
    private String rc;
    private String name;
    private String account;
    private String creditCif;
    private String debitCif;
    private SmokeData smokeData;

    @BeforeAll
    public void getInfo(Harness harness) {
        smokeData = SmokeData.getInstance(harness);
        CardData creditData = smokeData.getCreditData();
        contract = creditData.getContract();
        creditCard = creditData.getCard();
        rc = creditData.getRc();
        name = creditData.getName();
        account = creditData.getAccount();
        creditCif = creditData.getCif();
        CardData debitData = smokeData.getDebitData();
        debitCif = debitData.getCif();
        debitCard = debitData.getCard();
    }

    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T1949")
    public void MWFB62GetCardListByCurrentAcc(Harness harness) {
        harness.tasks().wso2().restRequest()
                .prepareEmptyRequestFromUfo(smokeData.getPath("MWFB62"))
                .addField("BU", contract)
                .addField("Organization", "1")
                .postExtended()
                .andAssertStatus(200)
                .andAssertFieldValue("Data[0].CisloPK", creditCard.substring(3));
    }

    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T1951")
    public void MWFB71GetCardListByBirthNum(Harness harness) {
        harness.tasks().wso2().restRequest()
                .prepareEmptyRequestFromUfo(smokeData.getPath("MWFB71"))
                .addField("RodneCislo", rc)
                .postExtended()
                .andAssertStatus(200)
                .andAssertFieldValue("Data[0].CisloKarty", creditCard.substring(3));
    }

    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T1939")
    public void MWF625GetCardListByContractAll(Harness harness) {
        harness.tasks().wso2().restRequest()
                .prepareEmptyRequestFromUfo(smokeData.getPath("MWF625"))
                .addField("CisloSmlouvy", contract)
                .postExtended()
                .andAssertStatus(200)
                .andAssertFieldValue("Data[0].CisloKarty", creditCard.substring(3))
                .andAssertFieldValue("Data[0].JmenoMajitel", name.toUpperCase())
                .andAssertFieldValue("Data[0].CisloSmlouvy", contract)
                .andAssertFieldValue("Data[0].RodneCisloMajitel", rc)
                .andAssertFieldValue("Data[0].Organizace", "1");
    }

    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T2094")
    public void MWFA93GetCardListByCardAccount(Harness harness) {
        harness.tasks().wso2().restRequest()
                .prepareEmptyRequestFromUfo(smokeData.getPath("MWFA93"))
                .addField("CisloKaretnihoUctu", account)
                .addField("DruhKarty", "1")
                .postExtended()
                .andAssertStatus(200)
                .andAssertFieldValue("Data[0].CisloKarty", creditCard.substring(3));
    }

    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T1956")
    public void MWFBR6GetCardListByName(Harness harness) {
        String[] nameInput = name.split(" ");
        String firstName = nameInput[nameInput.length - 2];
        String lastName = nameInput[nameInput.length - 1];
        JsonNode data = harness.tasks().wso2().restRequest()
                .prepareEmptyRequestFromUfo(smokeData.getPath("MWFBR6"))
                .addField("Jmeno", firstName.toUpperCase())
                .addField("Prijmeni", lastName.toUpperCase())
                .addField("DruhKarty", "1")
                .postExtended()
                .andAssertStatus(200)
                .extract("Data");
        boolean found = StreamSupport.stream(data.spliterator(), false)
                .map(i -> i.path("CisloKaretnihoUctu").textValue())
                .anyMatch(a -> account.equals(a));

        Assertions.assertTrue(found, "Account " + account + " not found for client " + name);
    }

    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T1942")
    public void MWF987GetCardListByRelNum(Harness harness) {
        harness.tasks().wso2().restRequest()
                .prepareEmptyRequestFromUfo(smokeData.getPath("MWF987"))
                .addField("ORGANIZATION", "1")
                .addField("RELATIONSHIP_NUMBER", contract)
                .postExtended()
                .andAssertStatus(200)
                .andAssertFieldValue("Data[0].ACCOUNT_NUMBER", account);
    }

    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T1955")
    public void MWFBQ7GetCardListWithAddCards(Harness harness) {
        harness.tasks().wso2().restRequest()
                .prepareEmptyRequestFromUfo(smokeData.getPath("MWFBQ7"))
                .addField("CisloKaretnihoUctu", account)
                .postExtended()
                .andAssertStatus(200)
                .andAssertFieldValue("Data[0].CisloKarty", creditCard.substring(3))
                .andAssertFieldValue("Data[0].CisloSmlouvy", contract);
    }

    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T1957")
    public void MWFD75GetCardListAllCards(Harness harness) {
        harness.tasks().wso2().restRequest()
                .prepareEmptyRequestFromUfo(smokeData.getPath("MWFD75"))
                .addField("RodneCislo", rc)
                .postExtended()
                .andAssertStatus(200)
                .andAssertFieldValue("Data[0].CisloKarty", creditCard);
    }

    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T1978")
    public void MWFKDVGetAddServicesInsCCAcc(Harness harness) {
        harness.tasks().wso2().restRequest()
                .prepareEmptyRequestFromHub(smokeData.getPath("MWFKDV"))
                .addField("ClientCif", creditCif)
                .postExtended()
                .andAssertStatus(200)
                .andAssertFieldValue("InsuranceView[0].AccountNumber", account)
                .andAssertFieldValue("InsuranceView[0].InsuranceContractNumber", contract);
    }

    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T1937")
    public void MWF494GetCardListActiveForADC(Harness harness) {
        harness.tasks().wso2().restRequest()
                .prepareEmptyRequestFromCbl(smokeData.getPath("MWF494"))
                .addField("BirthNumber", rc)
                .postExtended()
                .andAssertStatus(200)
                .andAssertPresent("Data[0].AccountNumber")
                .andAssertFieldValue("Data[0].AccountNumber", account)
                .andAssertFieldValue("Data[0].CardNumber", creditCard);
    }

    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T1977")
    public void MWFK32_GetCardListAllByBirthNum(Harness harness) {
        harness.tasks().wso2().restRequest()
                .prepareEmptyRequestFromUfo(smokeData.getPath("MWFK32"))
                .addField("RodneCislo", rc)
                .postExtended()
                .andAssertStatus(200)
                .andAssertFieldValue("Data[0].CisloKarty", creditCard.substring(3));
    }

    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T2439")
    public void MWFXXX5_GetFirstVisionCardAccount(Harness harness) {
        harness.tasks().wso2().restRequest()
                .prepareEmptyRequestFromUfo(smokeData.getPath("MWFXXX5"))
                .addField("SEARCH_KEY", contract)
                .addField("ORGANIZATION", "1")
                .postExtended()
                .andAssertStatus(200)
                .andAssertFieldValue("ACCT_NBR", account);
    }

    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T2088")
    public void MWF2D0GetDispositionsCardList(Harness harness) {
        harness.tasks().wso2().restRequest()
                .prepareEmptyRequestFromUfo(smokeData.getPath("MWF2D0"))
                .addField("ClientCif", debitCif)
                .postExtended()
                .andAssertStatus(200)
                .andAssertFieldValue("Data[0].CardNumber", debitCard);
    }

    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T2427")
    public void MWFCC2GetCardListContractCards(Harness harness) {
        harness.tasks().wso2().restRequest()
                .prepareEmptyRequestFromUfo(smokeData.getPath("MWFCC2"))
                .addField("CONTRACT_NUMBER", contract)
                .postExtended()
                .andAssertStatus(200)
                .andAssertFieldValue("Data[0].CARD_NUMBER", creditCard);
    }
}
