package cz.moneta.test.regression.data.api.kyc;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.cagw.api.v2.external.client.kyc.businesscard.BusinessCardResponse;
import cz.moneta.test.dsl.cagw.api.v4.token.TokenResponse;
import cz.moneta.test.dsl.util.TestUtils;
import cz.moneta.test.harness.annotations.NonConcurrentTestScenario;
import cz.moneta.test.harness.annotations.TestCase;
import cz.moneta.test.harness.annotations.JiraTestCase;
import cz.moneta.test.harness.support.auth.Credentials;
import cz.moneta.test.harness.support.auth.Key;
import org.jooq.Record;
import org.jooq.Result;
import org.junit.jupiter.api.Assertions;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Disabled;

import static cz.moneta.test.harness.annotations.Environment.TST1;

@SuppressWarnings("unchecked")
@NonConcurrentTestScenario(name = "KYC - confirm client identity via CAGW")
public class KycCaGwGetClientInformation {

    private static final String MOBILE_KEY = "12341234";
    private static final String OAUTH_2_CLIENT_ID = "l7xx3580306d692b4bd49a896b806582945e";
    private static final String OAUTH_2_CLIENT_SECRET = "f3f4222f5068421b939cf67d3ab35513";
    private static final String STATE = "S" + new Date().getTime();
    private static final String DATE_FROM = TestUtils.getTodayDate("dd.MM.yyyy");
    private static final String DATE_TO = new SimpleDateFormat("dd.MM.yyyy").format(java.sql.Date
            .valueOf(LocalDate.parse(TestUtils.getTodayDate("yyyy-MM-dd")).plusDays(1)));
    private static final String CLIENT_ID = "3289615";

    @Deprecated
    @Disabled("Obsolete, to be reviewed.")
    @TestCase(name = "Login via KYC, get token, calling api with client client's business card ," +
            " check data in UDEBS  ", environments = {TST1})
    @JiraTestCase(id = "AO-T422")
    public void tc_check_accounts_via_ca_gw_psd2(Harness harness, @Key("api.client") Credentials client) throws IOException {

        harness.withOauth2Web()
                .onOauth2WebTransitionsKyc()
                .openApproveAccessToAccountPageKyc(STATE, OAUTH_2_CLIENT_ID)
                .then(harness.tasks().oauth2().web().loginToApproveAccessPageKyc(client.getUsername(), client.getPassword()))
                .fillSmsKey(MOBILE_KEY)
                .continueButton()
                .getAuthTokenAndStoreItKyc();

        String authToken = harness.withCaGw()
                .api().v4().token()
                .prepareTokenRequest()
                .log("Token is: {}", TokenResponse::getAccess_token)
                .then(harness.tasks().oauth2().token().setRequestValuesV2(OAUTH_2_CLIENT_ID, OAUTH_2_CLIENT_SECRET))
                .send()
                .getAccess_token();

        //KYC - GET /kyc/businessCard
        BusinessCardResponse businesscard = harness.withCaGw()
                .api().v2().external().client().kyc().bussinesCard()
                .prepareBusinessCardRequest()
                .then(harness.tasks().caGw().api().businessCard().setRequestValues(authToken))
                .send();

        Assertions.assertEquals(businesscard.getFirstName(), "Jan");
        Assertions.assertEquals(businesscard.getLastName(), "Polang");
        Assertions.assertEquals(businesscard.getNationality(), "CZ");
        Assertions.assertEquals(businesscard.getGender(), "MALE");
        Assertions.assertEquals(businesscard.getBirthDate(), "1982-02-12");
        Assertions.assertEquals(businesscard.getBirthPlace(), "Praha");
        Assertions.assertEquals(businesscard.getAddress().getBuildingNumber(), "208");
        Assertions.assertEquals(businesscard.getAddress().getStreetNumber(), "8");
        Assertions.assertEquals(businesscard.getAddress().getStreet(), "Nerudova");
        Assertions.assertEquals(businesscard.getAddress().getCity(), "Praha");
        Assertions.assertEquals(businesscard.getAddress().getZip(), "11800");
        Assertions.assertEquals(businesscard.getAddress().getCountry(), "CZ");
        Assertions.assertEquals(businesscard.getBirthNumber(), "8202128627");
        Assertions.assertEquals(businesscard.getPrimaryIdentityPaper().getNumber(), "200115551");
        Assertions.assertEquals(businesscard.getPrimaryIdentityPaper().getExpirationDate(), "2022-01-28");
        Assertions.assertEquals(businesscard.getPrimaryIdentityPaper().getIssuingCountry(), "CZ");
        Assertions.assertEquals(businesscard.getPrimaryIdentityPaper().getType(), "ID_CARD_CR");

        Result<Record> records = harness.withUdebs()
                .transactionForKyc()
                .getTransaction(DATE_FROM, DATE_TO, CLIENT_ID, STATE);

        harness.log(records.formatJSON());

        Assertions.assertEquals(records.get(0).getValue("OPERID").toString(), "914");
        Assertions.assertEquals(records.get(0).getValue("STATUSID").toString(), "60");
        Assertions.assertEquals(records.get(0).getValue("CHANNELID").toString(), "23");

        ObjectMapper mapper = new ObjectMapper();
        JsonNode json = mapper.readValue(records.get(0).getValue("DATA").toString(), JsonNode.class);

        Assertions.assertEquals(json.findValuesAsText("applicationName").get(0), "Testovaci klient");
        Assertions.assertEquals(json.findValuesAsText("applicationApiKey").get(0), OAUTH_2_CLIENT_ID);
        Assertions.assertEquals(json.findValuesAsText("mobileNumber").get(0), "+420777123321");
        Assertions.assertEquals(json.get("scopes").get(0).toString(), "\"KYC\"");
        Assertions.assertNotNull(json.findValuesAsText("state").get(0));
        Assertions.assertEquals(json.findValuesAsText("thirdPartyCode").get(0), "111456121");
        Assertions.assertNotNull(json.findValuesAsText("validFrom").get(0));
        Assertions.assertNotNull(json.findValuesAsText("validTo").get(0));
    }
}