package cz.moneta.test.regression.cards.localtests;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.ufo.banka.pages.cards.credit.OPN008_S001_SignContractPage;
import cz.moneta.test.dsl.ufo.banka.tasks.BankaTasks;
import cz.moneta.test.dsl.util.BankaUtils;
import cz.moneta.test.dsl.util.credentials.Keys;
import cz.moneta.test.dsl.util.data.client.NonClient;
import cz.moneta.test.dsl.util.task.retail.cards.constants.CreditCardSQL;
import cz.moneta.test.harness.annotations.TestCase;
import cz.moneta.test.harness.annotations.TestScenario;
import cz.moneta.test.harness.endpoints.ods.OdsEndpoint;
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.AfterEach;
import org.junit.jupiter.api.BeforeAll;

import static cz.moneta.test.dsl.ufo.banka.pages.cards.credit.OPN008_S001_SignContractPage.APPLICATION_ID_KEY;
import static cz.moneta.test.dsl.ufo.banka.pages.main.HKO101_S001_MainClientPage.CLIENT_ID_KEY;
import static cz.moneta.test.dsl.ufo.banka.pages.main.HKO101_S001_MainClientPage.NEW_ACCOUNT_NUMBER;
import static cz.moneta.test.dsl.ufo.banka.pages.main.PSP004_S001_PrintPage.DOCUMENT_URLS_STORE_KEY;
import static cz.moneta.test.dsl.ufo.banka.pages.search.HLO001_S001_ClientSearchPage.BANKER_NAME;
import static cz.moneta.test.harness.annotations.Environment.PPE;
import static cz.moneta.test.harness.annotations.Environment.TST1;

@TestScenario(name = "Teller must be able to Onboard a card for FOP")
public class OnboardingCardsForFopTest {
    Long accountNumber = 235570315L;
    String companyNumber = "62662333";
    String cardNumber;

    @BeforeAll
    public void setup(Harness harness, @Key(Keys.ufo.banka.secondBanker) Credentials secondBanker) {
        BankaTasks bankaTasks = harness.tasks().ufo().banka();

        harness.withUfo()
                .openLoginPage()
                .then(bankaTasks.login().withCredentialsAndTurnOffSignpad(secondBanker))
                .globalStoreBankerName(harness)
                .then(bankaTasks.search().findByIco(companyNumber))
                .then(bankaTasks.cardsOnboarding().createAccountForFop());

        accountNumber = harness.get(NEW_ACCOUNT_NUMBER);
    }

    @TestCase(name = "Onboarding MBC for existing client", environments = {TST1, PPE})
    public void creditCardForExistingContract(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials,
                                              @Key(Keys.ufo.banka.secondBanker) Credentials secondBankerCredentials) {
        NonClient nonClient = harness.data().newFVNonClient()
                .withIco(companyNumber);

        BankaTasks bankaTasks = harness.tasks().ufo().banka();

        OPN008_S001_SignContractPage firstBanker = harness.withUfo()
                .openLoginPage()
                .then(bankaTasks.login().withCredentialsAndTurnOffSignpad(credentials))
                .then(bankaTasks.search().findByIcoWithAlert(nonClient.ico()))
                .then(bankaTasks.cardsOnboarding().onboardingMBCForFopFirstBanker("10000"))
                .then(bankaTasks.cardsOnboarding().onboardingMBCForFopRequestConfirmation(harness.get(BANKER_NAME)));

        harness.withUfoSecondInstance()
                .openLoginPage()
                .then(bankaTasks.login().withCredentialsSecondBanker(secondBankerCredentials))
                .then(bankaTasks.cardsServicing().secondBankerApplicationSign(harness.get(APPLICATION_ID_KEY), harness.get(CLIENT_ID_KEY)));

        firstBanker
                .then(bankaTasks.cardsOnboarding().onboardingMbcAfterSecondBankerSigned());

        Result<Record> resultList = harness.getEndpoint(OdsEndpoint.class).executeSql(CreditCardSQL.FIND_ONBOARDED_CARDS_BY_RC.replaceAll("ID_NUMBER", companyNumber));
        cardNumber = resultList.getValue(0, 1).toString();
    }

    @AfterEach
    public void tearDown(Harness harness) {
        harness.store("reports.tmfj.execution.comment",
                companyNumber + "\n Číslo karty: " + cardNumber +
                        "\n Url dokumentů: " + BankaUtils.formatDocumentUrls(harness.get(DOCUMENT_URLS_STORE_KEY, true)));
    }
}


