package cz.moneta.test.regression.commercial.smallbusiness.digital.jenkinstests.newtobank;

import com.fasterxml.jackson.databind.JsonNode;
import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.openapi.smeonboarding.SmallBusinessOnboardingEndpointsConstants;
import cz.moneta.test.dsl.openapi.smeonboarding.direct_channels.*;
import cz.moneta.test.dsl.openapi.smeonboarding.identify.CompanyIdentificationForm;
import cz.moneta.test.dsl.openapi.smeonboarding.identify.CompanyIdentificationNumber;
import cz.moneta.test.dsl.openapi.smeonboarding.identify.IdentifyRequestBody;
import cz.moneta.test.dsl.openapi.smeonboarding.identify.ProductInstance;
import cz.moneta.test.dsl.openapi.smeonboarding.process.SmallBusinessOnboardingProcessUtils;
import cz.moneta.test.harness.annotations.JiraTestCase;
import cz.moneta.test.harness.annotations.NonConcurrentTestScenario;
import cz.moneta.test.harness.annotations.TestCase;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.TestMethodOrder;

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@NonConcurrentTestScenario(name = "Call onboarding identify api tests")
public class OnboardingIdentifyTests {

    private String token;
    private static final String COUNTRY = "\"52\"";
    private static final String NAME = "\"Michael Čihák\"";

    @TestCase(name = "NTB test 03 - /smeonboarding/application-open")
    @JiraTestCase(id = "SBD-T78")
    @Order(1)
    public void testOpen(Harness harness) {
        harness.withOpenApi()
                .prepareUnsecuredRequest()
                .withPath(SmallBusinessOnboardingEndpointsConstants.APPLICATION_OPEN_ENDPOINT)
                .withHeader("Content-Type", "application/json")
                .get()
                .andAssertStatus(200);
    }

    @TestCase(name = "NTB test 02 - /smeonboarding/identify")
    @JiraTestCase(id = "SBD-T77")
    @Order(2)
    public void testIdentify(Harness harness) {
        token = SmallBusinessOnboardingProcessUtils.startProcessAndGetToken(harness);

        IdentifyRequestBody identifyRequestBody = IdentifyRequestBody.builder()
                .productInstance(ProductInstance.builder()
                        .amount(350000)
                        .installment(4900)
                        .installmentCount(24)
                        .interestRate(6)
                        .productType("SME_EXPRESS")
                        .build())
                .companyIdentificationForm(CompanyIdentificationForm.builder()
                        .companyIdentificationNumber(CompanyIdentificationNumber.builder()
                                .value("04418093")
                                .name("companyIdentificationNumber")
                                .build())
                        .build())
                .build();

        harness.withOpenApi()
                .prepareSecuredRequest()
                .withPath(SmallBusinessOnboardingEndpointsConstants.IDENTIFY_ENDPOINT)
                .withHeader("Content-Type", "application/json")
                .withHeader("Authorization", "Bearer " + token)
                .withPojoPayload(identifyRequestBody)
                .post()
                .andAssertStatus(200);
    }

    @TestCase(name = "NTB test 04 - /smeonboarding/business/detail")
    @JiraTestCase(id = "SBD-T79")
    @Order(3)
    public void testBusinessDetail(Harness harness) {

        JsonNode responseObject = harness.withOpenApi()
                .prepareSecuredRequest()
                .withPath(SmallBusinessOnboardingEndpointsConstants.BUSINESS_DETAIL)
                .withHeader("Content-Type", "application/json")
                .withHeader("Authorization", "Bearer " + token)
                .get()
                .andAssertStatus(200)
                .extract("responseObject");
        String countryResponse = responseObject.findValue("country").toString();
        String nameResponse = responseObject.findValue("name").toString();

        if ((countryResponse.equals(COUNTRY) && (nameResponse.equals(NAME)))) {
            return;
        }
        throw new AssertionError("Invalid body");
    }

    @TestCase(name = "NTB test 05 - /smeonboarding/business/detail/commit")
    @JiraTestCase(id = "SBD-T168")
    @Order(4)
    public void testCommit(Harness harness) {
        harness.withOpenApi()
                .prepareSecuredRequest()
                .withPath(SmallBusinessOnboardingEndpointsConstants.DETAIL_COMMIT)
                .withHeader("Content-Type", "application/json")
                .withHeader("Authorization", "Bearer " + token)
                .withPayload("{}")
                .post()
                .andAssertStatus(200);
    }

    @TestCase(name = "NTB test 06 - /smeonboarding/process/utm")
    @JiraTestCase(id = "SBD-T171")
    @Order(5)
    public void testUtm(Harness harness) {
        harness.withOpenApi()
                .prepareSecuredRequest()
                .withPath(SmallBusinessOnboardingEndpointsConstants.PROCESS_UTM)
                .withHeader("Content-Type", "application/json")
                .withHeader("Authorization", "Bearer " + token)
                .withPayload("{}")
                .post()
                .andAssertStatus(200);
    }

    @TestCase(name = "NTB test 07 - /smeonboarding/person/contact/direct-channels")
    @JiraTestCase(id = "SBD-T172")
    @Order(6)
    public void testDirectChannel(Harness harness) {
        DirectChannelsRequestBody directChannelsRequestBody = DirectChannelsRequestBody.builder()
                .contactDirectChannelsForm(ContactDirectChannelsForm.builder()
                        .email(Email.builder()
                                .name("email")
                                .value("auto@test.cz")
                                .build())
                        .phoneNumber(PhoneNumber.builder()
                                .name("phoneNumber")
                                .value("776132886")
                                .build())
                        .phonePrefix(PhonePrefix.builder()
                                .name("phonePrefix")
                                .value("+420")
                                .build())
                        .unsecuredCommunicationConsent(UnsecuredCommunicationConsent.builder()
                                .name("unsecuredCommunicationConsent")
                                .value("true")
                                .build())
                        .build())
                .build();

        harness.withOpenApi()
                .prepareSecuredRequest()
                .withPath(SmallBusinessOnboardingEndpointsConstants.DIRECT_CHANNELS)
                .withHeader("Content-Type", "application/json")
                .withHeader("Authorization", "Bearer " + token)
                .withPojoPayload(directChannelsRequestBody)
                .post()
                .andAssertStatus(200);
    }
}