package cz.moneta.test.regression.backoffice.exevido.api;

import com.fasterxml.jackson.databind.node.ObjectNode;
import cz.moneta.test.dsl.Harness;
import cz.moneta.test.harness.annotations.EnableIfSet;
import cz.moneta.test.harness.annotations.JiraTestCase;
import cz.moneta.test.harness.annotations.TestCase;
import cz.moneta.test.harness.annotations.TestScenarioWithOrder;
import cz.moneta.test.harness.support.rest.RawRestRequest;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Order;

import java.util.random.RandomGenerator;

import static cz.moneta.test.dsl.util.task.ufo.cb.CommercialTasks.JIRA_COMMENT_STORE_KEY;
import static cz.moneta.test.harness.annotations.Environment.TST1;

@TestScenarioWithOrder(name = "Api Exevido Service")
public class ApiExevidoService {

    ObjectNode objectNode;
    String inputData;

    @Order(1)
    @TestCase(name = "create incoming message", environments = {TST1})
    @JiraTestCase(id = "BOC-T8655")
    public void createMessageIncoming(Harness harness) {
        RawRestRequest.Response response = harness.tasks().wso2().restRequest().prepareRequestFromUfo("/t/backoffice.cluster/exe-message-incoming/v1")
                .withPayloadFromFile("regression/exevido/api/incomeMessage.json")
                .addField("messageId", String.valueOf(RandomGenerator.getDefault().nextInt(100000000, 999999999)))
                .post();
        inputData = response.getPayload();
        objectNode = response
                .andAssertStatus(200)
                .mapResponseIntoObject(ObjectNode.class);
        harness.storeGlobal("MESSAGE_ID", objectNode.findValue("id").asText());
    }

    @EnableIfSet(globalKey = "MESSAGE_ID")
    @Order(2)
    @TestCase(name = "get message by id")
    @JiraTestCase(id = "BOC-T8659")
    public void getMessage(Harness harness) {
        inputData = "id: " + harness.get("MESSAGE_ID").toString();
        String path = String.format("/t/backoffice.cluster/exevido-service/v1/message/%s/v1", harness.get("MESSAGE_ID").toString());
        objectNode = harness.tasks().wso2().restRequest().prepareGetRequestFromExevido(path)
                .get()
                .andAssertStatus(200)
                .andAssertFieldValue("messageType", "INCOMING")
                .mapResponseIntoObject(ObjectNode.class);
    }

    @EnableIfSet(globalKey = "MESSAGE_ID")
    @Order(3)
    @TestCase(name = "incoming message mark as finished", environments = {TST1})
    @JiraTestCase(id = "BOC-T8662")
    public void incomingMessageMarkAsFinished(Harness harness) {
        inputData = "id: " + harness.get("MESSAGE_ID").toString();
        String path = String.format("/t/backoffice.cluster/exevido-service/v1/incoming-message/%s/mark-as-finished/v1", harness.get("MESSAGE_ID").toString());
        harness.tasks()
                .wso2()
                .restRequest()
                .prepareRequestFromExevido(path)
                .withPayload("{}")
                .post()
                .andAssertStatus(200);
    }

    @AfterEach
    protected void teardown(Harness harness) {
        harness.store(JIRA_COMMENT_STORE_KEY,
                "\nVstup: \n" + inputData + "\nVýstup: \n " + objectNode.toPrettyString());

    }
}