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

import com.fasterxml.jackson.databind.JsonNode;
import cz.moneta.test.dsl.Harness;
import cz.moneta.test.harness.annotations.*;
import cz.moneta.test.harness.support.rest.RawRestRequest;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
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 = "Exevido Api Message Id Attachments")
public class ApiMessageIdAttachments {

    JsonNode jsonNode;
    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();
        jsonNode = response
                .andAssertStatus(200)
                .mapResponseIntoObject(JsonNode.class);
        harness.storeGlobal("MESSAGE_ID", jsonNode.findValue("id").asText());
    }

    @EnableIfSet(globalKey = "MESSAGE_ID")
    @Order(2)
    @TestCase(name = "Exevido Api Message Id Attachments", environments = {TST1})
    @JiraTestCase(id = "BOC-T8660")
    public void messageIdAttachments(Harness harness) {
        inputData = "{}" + "\nid: " + harness.get("MESSAGE_ID").toString();
        String path = String.format("/t/backoffice.cluster/exevido-service/v1/message/%s/attachments/v1", harness.get("MESSAGE_ID").toString());

        jsonNode = harness.tasks()
                .wso2()
                .restRequest()
                .prepareRequestFromExevido(path)
                .withPayload("{}")
                .post()
                .andAssertStatus(200)
                .extract("");
        Assertions.assertEquals("OBRATPORUSSP07470975_11092025.pdf", jsonNode.get(0).path("name").toString().replace("\"", ""));
        harness.storeGlobal("ATTACHMENT_ID", jsonNode.get(0).path("id").toString());
    }

    @EnableIfSet(globalKey = "ATTACHMENT_ID")
    @Order(3)
    @TestCase(name = "Exevido Api Attachment Url", environments = {TST1})
    @JiraTestCase(id = "BOC-T8661")
    public void messageAttachmentUrl(Harness harness) {
        inputData = "{}" + "\nid: " + harness.get("MESSAGE_ID").toString();
        String path = String.format("/t/backoffice.cluster/exevido-service/v1/attachment/%s/file-url/v1", harness.get("ATTACHMENT_ID").toString());
        jsonNode = harness.tasks()
                .wso2()
                .restRequest()
                .prepareRequestFromExevido(path)
                .withPayload("{}")
                .post()
                .andAssertStatus(200)
                .mapResponseIntoObject(JsonNode.class);

        Assertions.assertEquals(String.format("https://exevido.tst.moneta-containers.net/api/attachment/v1/%s/file", harness.get("ATTACHMENT_ID").toString()), jsonNode.path("url").toString().replace("\"", ""));

    }

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