package cz.moneta.test.regression.transactional.cards.retail.creditcard.origination;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.ib.tasks.cards.application.IbCardsApplicationTasks;
import cz.moneta.test.dsl.ufo.banka.tasks.BankaTasks;
import cz.moneta.test.dsl.ufo.banka.tasks.accountenums.CurrentAccountsEnum;
import cz.moneta.test.dsl.ufo.operations.search.SearchBy;
import cz.moneta.test.dsl.util.DateUtils;
import cz.moneta.test.dsl.util.TestUtils;
import cz.moneta.test.dsl.util.credentials.Keys;
import cz.moneta.test.dsl.util.data.client.NonClient;
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.auth.Credentials;
import cz.moneta.test.harness.support.auth.Key;
import org.junit.jupiter.api.BeforeEach;

import static cz.moneta.test.dsl.ufo.banka.pages.main.HKO101_S001_MainClientPage.CLIENT_CIF_STORE_KEY;
import static cz.moneta.test.harness.annotations.Environment.TST3;

@TestScenario(name = "ONB.RET.CC.14 - Retail CC - Application Origination")
public class Employee_fromIB_toIB {

    NonClient nonClient;

    @BeforeEach
    void setUp(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        nonClient = harness.data().newFVNonClientEmployee()
                .withNetMonthlyIncome(100000)
                .withHomeIncome(100000)
                .withOtherExpenses(5000)
                .withOtherLoanPayments(0);

        BankaTasks bankaTasks = harness.tasks().ufo().banka();
        harness.withUfo()
                .openLoginPage()
                .then(bankaTasks.login().withCredentialsAndTurnOffSignpad(credentials))
                .then(bankaTasks.newClient().create(nonClient, credentials.getUsername()))
                .then(bankaTasks.newPackagedProductTasks().ibWithMobileKeyAndCurrentAccount(nonClient, CurrentAccountsEnum.GENIUS_FREE_FLEXI));

        harness.tasks().nasDb().insertUser(nonClient);
    }

    @JiraTestCase(id = "FVP-T70")
    @TestCase(name = "Employee wants to get his Credit Card on Moneta INTERNET BANKING and he will complete origination process on Internet Banking", environments = {TST3})
    public void fromIBtoIB(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        IbCardsApplicationTasks cardTasks = harness.tasks().ib().cardsApplication();
        Credentials udebsCredentials = harness.tasks().wso2().idm().getIbCredentialsByRC(nonClient.birthNumber());

        int limit = 100000;

        harness.withIB()
                .openLoginPage()
                .then(harness.tasks().ib().loginWithUsernamePasswordAndMobileKey(
                        udebsCredentials.getUsername(),
                        udebsCredentials.getPassword(),
                        "12341234"))
                .onWelcomePage()
                .consentToCookies()
                .checkCreditCardOfferPresent("Kreditní karta Smart")
                .clickIamInterested()
                .then(cardTasks.fillCardDetails(
                        TestUtils.toString(limit),
                        "50000",
                        "50000",
                        "Elektronicky",
                        "111111",
                        "29.-28.",
                        "Bankovním příkazem"
                ))
                    .selectSecondIdType("Řidičský průkaz")
                    .setSecondIdNumber("EK 608907")
                    .setSecondIdValidTo("31.8.2026")
                .continueToEmploymentDetails()
                    .setApproximateMonthlyIncomeAverage(nonClient.netMonthlyIncome())
                    .setHouseholdMonthlyIncome(nonClient.homeIncome())
                    .setOtherNonMMBMonthlyPayments(nonClient.otherLoanPayments())
                    .setOtherMonthlyExpenses(nonClient.otherExpenses())
                    .setSalaryDiscounts(nonClient.wageDeductions())
                    .setEmployment(nonClient.occupationAdditionalInformation())
                    .setEmploymentStartDate(DateUtils.getDateWithFormat(nonClient.occupationEmploymentFromRaw(), "dd.MM.yyyy"))
                    .setCounty("Česká republika")
                .continueApplicationConfirmation()
                    .typeSmsCode("12341234")
                    .validateMobileKey()
                .onApplicationFinishingPage()
                    .continueToApplicationOvereview()
                    .waitForApproval()
                    .signApplicationAndContinueToPrepareContractPage()
                .clickConfirm()
                    .clickAgreeAndContinueToReview()
                .clickConfirm()
                    .typeSmsCode("12341234")
                    .signContractAndContinueToConfirmationPage()
                .continueToApplicationOverviewPage()
                    .checkFirstApplicationStatus("Schválená")
                    .checkFirstApplicationLimit(TestUtils.toStringWithTwoDecimals(limit))
                .clickLogOff();

//        // Step 24 - UFO Ops check
        harness.withUfo()
                .openLoginPage()
                .then(harness.tasks().ufo().operations().login().withCredentialsExpectingError(credentials))
                .dismissErrorPage()
                .fillSearchInput(harness.get(CLIENT_CIF_STORE_KEY))
                .searchBy(SearchBy.CIF)
                .onEmployeeDetailsConfirmationPage()
                .clickYes()
                .openApplicationList()
                    .assertFirstApplicationProductType("Credit card")
                    .assertFirstApplicationRequestedAmount("100000")
                    .assertFirstApplicationApprovedAmount("100000")
                    .assertFirstApplicationState("SOUHLAS")
                    .assertFirstApplicationChannel("IBS");
    }
}
