package cz.moneta.test.sandbox.connectiontests.jenkinsapi;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.harness.annotations.Environment;
import cz.moneta.test.harness.annotations.TestCase;
import cz.moneta.test.harness.annotations.TestScenario;
import cz.moneta.test.harness.connectors.VaultConnector;
import cz.moneta.test.harness.endpoints.jira.JiraEndpoint;
import cz.moneta.test.harness.exception.HarnessException;
import cz.moneta.test.harness.support.rest.RawRestRequest;
import cz.moneta.test.system.ilodsserver.IlodsServerTest;
import cz.moneta.test.system.openapi.OpenApiTest;
import org.junit.jupiter.api.Assertions;

@TestScenario(name = "Check Global REST API Connections")
public class RestApiConnectionTest {

    @TestCase(name = "Check OpenAPI Connection", environments = {Environment.TST1, Environment.PPE, Environment.FVE})
    public void testOpenApiConnection(Harness harness) {
        new OpenApiTest().callOpenApi(harness);
    }

    @TestCase(name = "Check WSO2 Connection", environments = {Environment.TST1, Environment.PPE, Environment.FVE})
    public void testWSO2Connection(Harness harness) {
        String token = harness.tasks().wso2().authorization().getToken("DigitalchannelsSB_CBL");
        Assertions.assertNotNull(token);
    }

    @TestCase(name = "Check CA API GW Connection", environments = {Environment.TST1, Environment.PPE})
    public void testCaApiGwConnection(Harness harness) {
        String token = harness.tasks().caGw().authorization().getVoicebotToken();
        Assertions.assertNotNull(token);
    }

    @TestCase(name = "Check WireMock Server Connection", environments = {Environment.DEV})
    public void testWireMockServerConnection(Harness harness) {
        harness.withMockServer()
                .prepareRequest()
                .withPath("/__admin/health")
                .get()
                .andAssertFieldValue("status", "healthy");
    }

    @TestCase(name = "Check MVCR Connection")
    public void testMvcrConnection(Harness harness) {
        harness.tasks().mvcr().checkIdCardValidity("123456123");
    }

    @TestCase(name = "Check Elastic Read Connection")
    public void testElasticReadConnection(Harness harness) {
        harness.withElasticApi()
                .prepareReadRequest()
                .withPath("/")
                .withHeader("Authorization", "Basic " + harness.tasks().elasticApiTasks().getAuthorizationToken())
                .get()
                .andAssertStatus(200);
    }

    @TestCase(name = "Check Elastic Write Connection")
    public void testElasticWriteConnection(Harness harness) {
        harness.withElasticApi()
                .prepareWriteRequest()
                .withPath("/")
                .withHeader("Authorization", "Basic " + harness.tasks().elasticApiTasks().getAuthorizationToken())
                .get()
                .andAssertStatus(200);
    }

    @TestCase(name = "Check Vault Connection")
    public void testVaultConnection(Harness harness) {
        new VaultConnector(harness.getConfig("vault.url"), harness.getConfig("vault.username"), harness.getConfig("vault.password"));
    }

    @TestCase(name = "Check Jira API Connection")
    public void testJiraConnection(Harness harness) {
        VaultConnector v = new VaultConnector(harness.getConfig("vault.url"), harness.getConfig("vault.username"), harness.getConfig("vault.password"));
        String token = (String) v.getValue("/kv/autotesty/common/jira", "token").orElseThrow(() -> new HarnessException("Cannot get Jira token from Vault"));

        RawRestRequest.Response comment = RawRestRequest.jsonBuilder(new JiraEndpoint(harness.getConfig("endpoints.jira.core")))
                .withPath("issue/TEA-245")
                .withHeader("Authorization", "Basic " + token)
                .get()
                .andAssertStatus(200);
    }
    @TestCase(name = "Check Jira Zephyr API Connection")
    public void testJiraZephyrConnection(Harness harness) {
        VaultConnector v = new VaultConnector(harness.getConfig("vault.url"), harness.getConfig("vault.username"), harness.getConfig("vault.password"));
        String token = (String) v.getValue("/kv/autotesty/common/jira", "token").orElseThrow(() -> new HarnessException("Cannot get Jira token from Vault"));

        RawRestRequest.Response comment = RawRestRequest.jsonBuilder(new JiraEndpoint(harness.getConfig("reports.tmfj.url")))
                .withPath("testcase/TEA-T10")
                .withHeader("Authorization", "Basic " + token)
                .get()
                .andAssertStatus(200);
    }

    @TestCase(name = "Check ILODS Server Connection")
    public void testIlodsServer(Harness harness) {
        new IlodsServerTest().callTestAction(harness);
    }

    @TestCase(name = "Check Selenium Hub Connection")
    public void testTestSeleniumHubConnection(Harness harness) {
        harness.withTestAutomationPage()
                .openHomePage();
    }

}