package cz.moneta.test.utils.loans;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.util.credentials.Keys;
import cz.moneta.test.dsl.util.data.client.IdCard;
import cz.moneta.test.dsl.util.data.client.NonClient;
import cz.moneta.test.dsl.util.task.monetaportal.childbuildingsavings.ChildBuildingSavingsUtils;
import cz.moneta.test.dsl.util.task.retail.loans.RetailSQL;
import cz.moneta.test.dsl.util.task.retail.other.DocumentUploaderTasks;
import cz.moneta.test.harness.annotations.TestCase;
import cz.moneta.test.harness.annotations.TestScenario;
import cz.moneta.test.harness.endpoints.nasdb.NasDbEndpoint;
import cz.moneta.test.harness.endpoints.udebs.UdebsEndpoint;
import cz.moneta.test.harness.support.auth.AuthSupport;
import cz.moneta.test.harness.support.auth.Credentials;
import org.jooq.Record;
import org.jooq.Result;
import org.junit.jupiter.api.Assertions;

import java.time.LocalDate;

import static cz.moneta.test.dsl.util.task.retail.loans.constants.BridgeLoanSQL.DU_APPLICANT_DATA_BY_RC_SQL;
import static cz.moneta.test.dsl.util.task.retail.loans.constants.BridgeLoanSQL.DU_CO_APPLICANT_DATA_BY_RC_SQL;
import static cz.moneta.test.dsl.util.task.retail.loans.constants.DefaultLoansConstants.DEFAULT_CLIENT_ADDRESS;
import static cz.moneta.test.dsl.util.task.retail.loans.constants.DefaultLoansConstants.DEFAULT_ID_CARD;

@TestScenario()
public class BridgeLoanUtilTests {

    @TestCase(name = "BL DU Applicant by BirthNumber")
    public void bridgeLoanDU(Harness harness) {
        DocumentUploaderTasks tasks = harness.tasks().retail().documentUploaderTasks();
        NonClient client = harness.data().newNonClient()
                .withResidencyAddress(DEFAULT_CLIENT_ADDRESS)
                .withIdCard(DEFAULT_ID_CARD)
                .withBirthNumber(harness.getSystemOrConfigValue("birthNumber"));

        Result<Record> resultList = harness.getEndpoint(UdebsEndpoint.class)
                .executeSql(DU_APPLICANT_DATA_BY_RC_SQL
                        .replaceAll("_BIRTHNUMBER_", client.birthNumber()));
        Assertions.assertFalse(resultList.isEmpty(), String.format("For BirthNumber %s was not found DU Code in UDEBSE", client.birthNumber()));
        Assertions.assertNotNull(resultList.getValue(0, "CODE"), String.format("For BirthNumber %s was not found DU Code in UDEBSE", client.birthNumber()));
        client.withFirstName(resultList.getValue(0, "FIRSTNAME").toString())
                .withLastName(resultList.getValue(0, "LASTNAME").toString());

        harness.store("CODE", resultList.getValue(0, "CODE").toString());
        tasks.takeIdentityBE(client, "22");
    }

    @TestCase(name = "BL DU Co-Applicant by BirthNumber")
    public void bridgeLoanDUCoApplicant(Harness harness) {
        DocumentUploaderTasks tasks = harness.tasks().retail().documentUploaderTasks();
        NonClient coApplicant = harness.data().newNonClient()
                .withIdCard(new IdCard("209373728", LocalDate.of(2018, 8, 12), LocalDate.of(2028, 8, 12)))
                .withResidencyAddress(DEFAULT_CLIENT_ADDRESS)
                .withBirthNumber(harness.getSystemOrConfigValue("birthNumber"));

        Result<Record> resultList = harness.getEndpoint(UdebsEndpoint.class)
                .executeSql(DU_CO_APPLICANT_DATA_BY_RC_SQL
                        .replaceAll("_BIRTHNUMBER_", coApplicant.birthNumber()));
        Assertions.assertFalse(resultList.isEmpty(), String.format("For BirthNumber %s was not found DU Code in UDEBSE", coApplicant.birthNumber()));
        Assertions.assertNotNull(resultList.getValue(0, "CODE"), String.format("For BirthNumber %s was not found DU Code in UDEBSE", coApplicant.birthNumber()));
        coApplicant.withFirstName(resultList.getValue(0, "FIRSTNAME").toString())
                .withLastName(resultList.getValue(0, "LASTNAME").toString());
        harness.store("CODE", resultList.getValue(0, "CODE").toString());
        tasks.takeIdentityBE(coApplicant, "22");
    }

    @TestCase(name = "Zpracování Manual Approving")
    public void nasStates(Harness harness) {
        String birthNumber = "9008243563";

//        Result<Record> resultList = harness.getEndpoint(NasDbEndpoint.class)
//                .executeSql(String.format(RetailSQL.GET_APP_STATE_CIF_BY_RC, birthNumber));
//        harness.log(RetailUtils.getValueFromResult(resultList, "APPL_STAT_KEY"));
//        = 8 Schválená - MFCH OK

        Result<Record> resultList = harness.getEndpoint(NasDbEndpoint.class)
                .executeSql(String.format(RetailSQL.GET_APP_STATE_NBL_BY_RC, birthNumber));
        Assertions.assertEquals("9", ChildBuildingSavingsUtils.getValueFromResult(resultList, "APPL_STAT_KEY"), "Žádost není v Manuálním schvalování");
        //      9 = Manuální Schvalování

        Credentials bankerCred = AuthSupport.getCredentials(Keys.ufo.banka.banker, harness);
        harness.withUfo().openLoginPage()
                .typeUserNameAndPasswordAndClickLogin(bankerCred.getUsername(), bankerCred.getPassword())
                .onApplicationSelectPage()
                .selectFirstOmIfPresent()
                .selectUfoOperationsAndContinueWithoutOmSelect()
                .clickRiskManualApprovingQueue()
                .selectQueueAndSubmit("MSS")
                .clickEditManualApproving(birthNumber)
                .clickDecide();
    }
}
