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

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.harness.support.rest.RawRestRequest;
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;

import java.util.Random;

import static cz.moneta.test.sanity.transactional.SmokeData.getInstance;

@Execution(ExecutionMode.SAME_THREAD)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@TestScenario(name = "CC application")
public class ChangeCardBasedOnRequest {
    private int request;
    private String contract;
    private String bu;
    private String account;
    private SmokeData smokeData;
    private String applToCancel;

    @BeforeAll
    public void getInfo(Harness harness) {
        smokeData = getInstance(harness);
        CardData creditData = smokeData.getCreditData();
        contract = creditData.getContract();
        account = creditData.getAccount();
        bu = creditData.getBu();
    }

    private int getRequestKey(Harness harness) {
        return Double.valueOf(harness.tasks().wso2().restRequest()
                .prepareEmptyRequestFromUfo(smokeData.getPath("MWFJR2"))
                .addField("CisloSmlouvy", contract)
                .postExtended()
                .andAssertStatus(200)
                .andAssertPresent("Data[0].APP_KEY")
                .extract("Data[0].APP_KEY")
                .asText())
                .intValue();
    }

    private RawRestRequest.Response getApplicationDetail(Harness harness) {
        RawRestRequest.Response response = harness.tasks().wso2().restRequest()
                .prepareEmptyRequestFromUfo(smokeData.getPath("MWFAV5"))
                .addField("Zadost_KEY", request)
                .addField("Channel_ID", "IBS")
                .postExtended();
        applToCancel = response.extract("ZadostAP").textValue();
        return response;
    }

    @Order(1)
    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T2511")
    public void MWFJR2_CLIP_PROCESS_GET_APPLICATION(Harness harness) {
        request = getRequestKey(harness);
    }

    @Order(2)
    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T1947")
    public void MWFAV5_GET_APPLICATION_DETAIL(Harness harness) {
        request = getRequestKey(harness);
        getApplicationDetail(harness)
                .andAssertStatus(200)
                .andAssertFieldValue("BUCisloUctu", bu)
                .andAssertFieldValue("CisloSmlouvy", contract);
    }

    @Order(3)
    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T2735")
    public void MWF7D3_CARD_CHANGE_CREDIT_LIMIT(Harness harness) {
        int newLimit = new Random().nextInt(40_000);
        request = getRequestKey(harness);
        harness.tasks().wso2().restRequest()
                .prepareEmptyRequestFromWps(smokeData.getPath("MWF7D3"))
                .addField("CisloKaretnihoUctu", account)
                .addField("IDZadosti", request)
                .addField("UverovyRamecNovy", newLimit)
                .postExtended()
                .andAssertStatus(200);
    }

    @Order(4)
    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T2785")
    public void MWFBX3_clip_process_cancel_application(Harness harness) {
        harness.tasks().wso2().restRequest()
                .prepareEmptyRequestFromNas(smokeData.getPath("MWFBX3"))
                .addField("APPL_ID", applToCancel)
                .postExtended()
                .andAssertStatus(200);
    }
}
