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

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 org.apache.commons.lang3.RandomStringUtils;

import java.util.Random;

import static cz.moneta.test.sanity.transactional.cards.firstvision.MESSAGE.FV;
import static cz.moneta.test.sanity.transactional.cards.firstvision.MESSAGE.SVC_RETURN;
import static cz.moneta.test.sanity.transactional.cards.firstvision.MESSAGE.VMX_MSGIN;
import static cz.moneta.test.sanity.transactional.cards.firstvision.MESSAGE.VMX_MSGOUT;

@TestScenario(name = "creating new card")
public class CardCreation {

    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T1901")
    public void createCard(Harness harness) {
        String dateString = DateUtils.getFormattedTodayDate(DateFormatPatterns.FIRST_VISION_DATE_FORMAT_WITHOUT_SEPARATOR);
        Random random = new Random();
        String user = random.nextInt(9999999) + "";
        String uniqueId = "000861" + random.nextInt(999_999_999);
        String taxId = random.nextInt(999_999_999) + 100_000_000 + "";
        String firstName = RandomStringUtils.randomAlphabetic(5);
        String lastName = "samo" + RandomStringUtils.randomAlphabetic(10);
        //TODO:  response xml has attributes which harness convert to fields with data loss
//        <CARD_DATA SEQ="00005">  -> <CARD_DATA> <SEQ>00005</SEQ>
        harness.withWso2()
                .prepareRequestXml()
                .withPath(FV)
                .withPayloadFromFile("regression/transactional/cards/sme/VMX.CUST.ACCT.CARD.ADD.EMEA.E8VG5.xml")
                .addField(VMX_MSGIN + ".ACCOUNT_DATA_SEGMENT", "USER_APPL_1", user)
                .addField(VMX_MSGIN + ".CUSTOMER_DATA_SEGMENT", "UNIQUE_ID", uniqueId)
                .addField(VMX_MSGIN + ".CUSTOMER_DATA_SEGMENT.M_C_TABLE", "TAX_ID", taxId)
                .addField(VMX_MSGIN + ".CUSTOMER_DATA_SEGMENT.M_C_TABLE", "FIRST_NAME", firstName)
                .addField(VMX_MSGIN + ".CUSTOMER_DATA_SEGMENT.M_C_TABLE", "LAST_NAME", lastName)
                .addField(VMX_MSGIN + ".ACCOUNT_DATA_SEGMENT", "DATE_OPENED", dateString)
                .postExtended()
                .andAssertFieldValue(SVC_RETURN, "P")
                .andAssertFieldValue(VMX_MSGOUT + ".CARD_DATA.CARD_ADD_RESULT", "1");
    }
}
