package cz.moneta.test.regression.risk;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.ufo.banka.pages.loans.expres.NEP006_S001_ExpressScoringResultPage;
import cz.moneta.test.dsl.ufo.banka.pages.loans.flexikredit.NFL006_S001_FlexikreditScoringResultPage;
import cz.moneta.test.dsl.ufo.banka.tasks.BankaTasks;
import cz.moneta.test.dsl.util.code.lists.HCisAllStavZadosti;
import cz.moneta.test.dsl.util.data.ClientUtils;
import cz.moneta.test.dsl.util.data.client.NonClient;
import cz.moneta.test.dsl.util.task.ufo.operations.OperationsTasks;
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 cz.moneta.test.harness.support.util.Level;
import org.junit.jupiter.api.*;

import java.util.ArrayList;
import java.util.List;

import static cz.moneta.test.dsl.util.credentials.Keys.ufo.banka.banker;

@TestScenario(name = "Loan applications for blacklisted clients")
public class BlacklistedClientsTests {
    private NonClient applicant;
    private BankaTasks bankaTasks;
    private OperationsTasks operationsTasks;
    private final int EXPRESS_TERM = 60;
    private final int DEFAULT_LOAN_AMOUNT = 30000;
    protected List<String> clientBirthNumbers = new ArrayList<>();

    @BeforeAll
    void setup(Harness harness) {
        bankaTasks = harness.tasks().ufo().banka();
        operationsTasks = harness.tasks().ufo().operations();
    }

    @BeforeEach
    void prepareClient(Harness harness) {
        applicant = ClientUtils.getRiskDefaultClient(harness);
    }

    @AfterEach
    void logBirthNumber() {
        clientBirthNumbers.add(applicant.birthNumber());
    }

    @AfterAll
    protected void logBirthNumbers(Harness harness) {
        clientBirthNumbers.forEach(rc -> harness.log(Level.INFO, rc));
    }

    @TestCase(name = "Express loan application for blacklisted client (risk-430)")
    public void express_for_blacklisted_client(Harness harness, @Key(banker) Credentials credentials) {
        int amount = 50000;

        bankaTasks.newClient().openUfoAndCreateClient(harness, credentials, applicant)
                .then(operationsTasks.blacklistWhitelist().addToRcBlacklistFromUfoBankaAndSwitchBackToClient(applicant.birthNumber()))
                .then(bankaTasks.expresTasks().createNewExpressApplication(applicant, amount, EXPRESS_TERM, HCisAllStavZadosti.VERIFICATION));

        Assertions.assertEquals(HCisAllStavZadosti.VERIFICATION.getValue(), harness.get(NEP006_S001_ExpressScoringResultPage.APP_STAT_STORE));
    }

    @TestCase(name = "New Express loan application for blacklisted client with a co-applicant")
    public void express_for_blacklisted_client_with_coApplicant(Harness harness, @Key(banker) Credentials credentials) {
        NonClient coApplicant = ClientUtils.getRiskDefaultClient(harness);
        int amount = 200000;

        bankaTasks.newClient().openUfoAndCreateClient(harness, credentials, coApplicant)
                .clickSearch()
                .then(bankaTasks.newClient().createWithIdCard(applicant, credentials.getUsername()))
                .then(operationsTasks.blacklistWhitelist().addToRcBlacklistFromUfoBankaAndSwitchBackToClient(applicant.birthNumber()))
                .then(bankaTasks.expresTasks().createNewExpressApplicationWithCoApplicant(applicant, coApplicant, amount, EXPRESS_TERM, HCisAllStavZadosti.VERIFICATION));

        Assertions.assertEquals(HCisAllStavZadosti.VERIFICATION.getValue(), harness.get(NEP006_S001_ExpressScoringResultPage.APP_STAT_STORE));
    }

    @TestCase(name = "New FlexiCredit application for blacklisted client")
    public void flexicredit_for_blacklisted_client(Harness harness, @Key(banker) Credentials credentials) {
        bankaTasks.newClient().openUfoAndCreateClient(harness, credentials, applicant)
                .then(operationsTasks.blacklistWhitelist().addToRcBlacklistFromUfoBankaAndSwitchBackToClient(applicant.birthNumber()))
                .then(bankaTasks.flexiCreditTasks().createNewFlexiCreditApplication(applicant, DEFAULT_LOAN_AMOUNT, HCisAllStavZadosti.VERIFICATION));

        Assertions.assertEquals(HCisAllStavZadosti.VERIFICATION.getValue(), harness.get(NFL006_S001_FlexikreditScoringResultPage.APP_STAT_STORE));
    }

    @TestCase(name = "New Credit Card application for blacklisted client")
    public void credit_card_for_blacklisted_client(Harness harness, @Key(banker) Credentials credentials) {
        bankaTasks.newClient().openUfoAndCreateClient(harness, credentials, applicant)
                .then(operationsTasks.blacklistWhitelist().addToRcBlacklistFromUfoBankaAndSwitchBackToClient(applicant.birthNumber()))
                .then(bankaTasks.cardsOnboarding().creditCards().createNewCreditCardApplication(applicant, DEFAULT_LOAN_AMOUNT, HCisAllStavZadosti.VERIFICATION));

        Assertions.assertEquals(HCisAllStavZadosti.VERIFICATION.getValue(), harness.get(NEP006_S001_ExpressScoringResultPage.APP_STAT_STORE));
    }

}
