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

import com.fasterxml.jackson.databind.JsonNode;
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.harness.support.rest.RawRestRequest;
import cz.moneta.test.sanity.transactional.cards.firstvision.MESSAGE;
import org.junit.jupiter.api.Assertions;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

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

@TestScenario(name = "Customer update/inquiry")
public class FvHeartBeat {

    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T5072")
    public void VMX_SYSTEM_STATUS_EMEA(Harness harness) {
        // services should be available (status == 0) in working hours [0:00:00 to 21:00:00] of local time
        // services should be unavailable (status <> 0) in after hours (21:00:00 to 0:00:00) of local time
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime startOfAfterHours = LocalDateTime.of(LocalDate.now(), LocalTime.of(21, 0, 0));
        boolean afterHours = now.isAfter(startOfAfterHours);
        RawRestRequest.Response response = harness.withWso2()
                .prepareRequest()
                .withPath(FV)
                .withPojoPayload(new MESSAGE("VMX.SYSTEM.STATUS.EMEA", "E8V1"))
                .addField("VMX_MSGIN", "ORG", "861")
                .postExtended()
                .andAssertFieldValue("VMX_ROOT.VMX_MSGOUT.SVC_RETURN", "P");
        Object activityId = response.getHeader("activityId");
        JsonNode output = response.extract("VMX_ROOT.VMX_MSGOUT.OUTPUT_AREA");
        String cmsStatus = output.path("CMS_PROCESSING_STATUS").textValue();
        String omsStatus = output.path("OMS_PROCESSING_STATUS").textValue();

        if (afterHours) {
            Assertions.assertNotEquals("0", cmsStatus, "CMS is available " + now + " activityId->" + activityId);
            Assertions.assertNotEquals("0", omsStatus, "OMS is available " + now + " activityId->" + activityId);
        } else {
            Assertions.assertEquals("0", cmsStatus, "CMS is unavailable " + now + " activityId->" + activityId);
            Assertions.assertEquals("0", omsStatus, "OMS is unavailable " + now + " activityId->" + activityId);
        }
    }
}
