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

import cz.moneta.test.dsl.Harness;
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.BeforeAll;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;

@Execution(ExecutionMode.SAME_THREAD)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@TestScenario(name = "get and use UniPtKey")
public class UniPtKey {
    private String rc;
    private String uniPtKey;
    private String cardType;
    private String firstName;
    private String lastName;
    private String card;
    private String account;
    private String contract;

    @BeforeAll
    public void getInfo(Harness harness) {
        SmokeData smokeData = SmokeData.getInstance(harness);
        cardType = "C";  //credit card
        rc = smokeData.getCreditData().getRc();
        card = smokeData.getCreditData().getCard();
        account = smokeData.getCreditData().getAccount();
        contract = smokeData.getCreditData().getContract();
        String name = smokeData.getCreditData().getName();
        String[] split = name.split(" ");
        lastName = split[split.length - 1];
        firstName = split[split.length - 2];
    }

    @Order(1)
    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T3655")
    public void GET_UNI_PT_KEY(Harness harness) {
        uniPtKey = harness.withWso2()
                .prepareRequestXml()
                .withPath("/t/cards.cluster/test-ods-get-uni-pt-key")
                .withPayload("<GET_UNI_PT_KEY_REQ>" +
                        "<RC_NUM_IN>" + rc + "</RC_NUM_IN>" +
                        "</GET_UNI_PT_KEY_REQ>")
                .post()
                .andAssertStatus(200)
                .andAssertPresent("RS_1")
                .andAssertPresent("RS_1.rows")
                .andAssertPresent("RS_1.rows.row")
                .andAssertPresent("RS_1.rows.row.UNI_PT_KEY_OUT")
                .andAssertFieldValue("RS_1.rows.row.FAMILY_NAME_OUT", lastName)
                .andAssertFieldValue("RS_1.rows.row.FIRST_NAME_OUT", firstName)
                .extract("RS_1.rows.row.UNI_PT_KEY_OUT")
                .textValue();
    }

    @Order(2)
    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T3656")
    public void ODS_GET_CARD_DATA(Harness harness) {
        harness.withWso2()
                .prepareRequestXml()
                .withPath("/t/cards.cluster/test-ods-get-card-data")
                .withPayload("<GET_CARD_DATA_REQ>" +
                        "<CARD_DC_FLAG_IN>" + cardType + "</CARD_DC_FLAG_IN>" +
                        "<ACC_LIST_FLAG_IN>1</ACC_LIST_FLAG_IN>" +
                        "<RS_2><row>" +
                        "<UNI_PT_KEY_IN>" + uniPtKey + "</UNI_PT_KEY_IN>" +
                        "</row></RS_2>" +
                        "</GET_CARD_DATA_REQ>")
                .post()
                .andAssertStatus(200)
                .andAssertFieldValue("RS_3.rows.row.RC_NUM_HOLDER", rc)
                .andAssertFieldValue("RS_3.rows.row.FIRST_NAME_OWNR", firstName)
                .andAssertFieldValue("RS_3.rows.row.FAMILY_NAME_OWNR", lastName)
                .andAssertFieldValue("RS_3.rows.row.CONTR_NUM", contract)
                .andAssertFieldValue("RS_3.rows.row.ACC_NUM", account)
                .andAssertFieldValue("RS_3.rows.row.CARD_NUM", card);
    }
}
