package cz.moneta.test.regression.risk.applications.retail;

import cz.moneta.test.dataprepare.risk.DefaultLoan;
import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.ufo.banka.pages.main.HKO101_S001_MainClientPage;
import cz.moneta.test.dsl.util.DateUtils;
import cz.moneta.test.dsl.util.code.lists.HCisAllStavZadosti;
import cz.moneta.test.dsl.util.code.lists.HCisDoklady;
import cz.moneta.test.dsl.util.data.client.Address;
import cz.moneta.test.dsl.util.data.client.Employer;
import cz.moneta.test.dsl.util.data.client.IncomeSubtype;
import cz.moneta.test.dsl.util.data.client.NonClient;
import cz.moneta.test.harness.annotations.*;
import cz.moneta.test.harness.support.auth.Credentials;
import cz.moneta.test.harness.support.auth.Key;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Order;

import java.time.ZoneId;
import java.util.Date;
import java.util.List;

import static cz.moneta.test.dsl.ufo.banka.pages.loans.expres.NEP006_S001_ExpressScoringResultPage.APP_KEY_STORE;
import static cz.moneta.test.dsl.ufo.banka.pages.main.HKO101_S001_MainClientPage.*;
import static cz.moneta.test.dsl.util.credentials.Keys.ufo.banka.banker;


@TestScenarioWithOrder(name = "Express Loan Tests")
public class ExpressLoanTests extends DefaultLoan {

    private final Employer EMPLOYER = new Employer("Pianoclassic s.r.o.", "17466865", Address.builder().street("Hvězdova").descriptiveNumber("1716").orientationNumber("2b").city("Praha").zip("14000").country("CZ").build(), "733303555");

    @BeforeEach
    public void initApplicant() {
        applicant.withEmployer(EMPLOYER);
        applicant.withHomeIncome(150000);
        applicant.withOtherLoanPayments(200);
    }

    @Order(1)
    @TestCase(name = "POS Schválení - Expres žádost - Podmíněný souhlas pro částku 560 tis. Kč na 84 měsíců", environments = {Environment.TST1})
    @JiraTestCase(id = "RISK-T2052")
    public void newClientWithExpressLoanApplicationAccept(Harness harness, @Key(banker) Credentials credentials) {
        applicant.withBirthNumber("8905173552");
        applicant.withNetMonthlyIncome(95000);
        applicant.withOtherExpenses(11000);

        openUfoAndPrepareClient(harness, credentials)
                .then(bankaTasks.expresTasks().createNewExpressApplicationWithExisting(applicant, 560000, 84, HCisAllStavZadosti.APPROVED, HCisDoklady.CP));
    }

    @Order(2)
    @TestCase(name = "POS Zamítnutí - Expres žádost - Zamítnutí pro částku 560 tis. Kč na 84 měsíců", environments = {Environment.TST1})
    @JiraTestCase(id = "RISK-T2053")
    public void newClientWithExpressLoanApplicationDeclined(Harness harness, @Key(banker) Credentials credentials) {
        applicant.withBirthNumber("8905173552");
        applicant.withNetMonthlyIncome(95000);
        applicant.withOtherExpenses(94000);

        openUfoAndPrepareClient(harness, credentials)
                .then(bankaTasks.expresTasks().createNewExpressApplicationWithExisting(applicant, 560000, 84, HCisAllStavZadosti.APPLICATION_DENIED, HCisDoklady.CP));
    }

    @Order(3)
    @TestCase(name = "POS Doplň doklad PoP - Expres žádost - Požadavek na doplnění dokladu příjmu", environments = {Environment.TST1})
    @JiraTestCase(id = "RISK-T2054")
    public void newClientWithExpressLoanApplicationIncomeProofRequired(Harness harness, @Key(banker) Credentials credentials) {
        applicant.withBirthNumber("8905173255");
        applicant.withNetMonthlyIncome(140000);
        applicant.withOtherExpenses(12000);
        applicant.withIncomeSubtype(IncomeSubtype.EMPLOYMENT_PER_FIXED);
        applicant.withOccupationTrialPeriod(false);
        applicant.withOccupationEmploymentTillRaw(Date.from(DateUtils.getNextWorkday().plusYears(2).atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()));

        openUfoAndPrepareClient(harness, credentials)
                .then(bankaTasks.expresTasks().createNewExpressApplicationWithExisting(applicant, 560000, 84, HCisAllStavZadosti.DOCUMENTS_REQUIRED, HCisDoklady.CP));
    }

    @Order(4)
    @TestCase(name = "POS Žádost se spolužadatelem - Expres žádost - Podmíněný souhlas při více žadatelích", environments = {Environment.TST1})
    @JiraTestCase(id = "RISK-T2057")
    public void newClientWithExpressLoanApplicationWithCoApplicant(Harness harness, @Key(banker) Credentials credentials) {
        applicant.withBirthNumber("8905173552");
        applicant.withNetMonthlyIncome(95000);
        applicant.withOtherExpenses(12000);
        NonClient coApplicant = harness.data().newNonClient()
                .withBirthNumber("8905172903")
                .withHomeIncome(120000)
                .withOtherLoanPayments(200)
                .withOtherExpenses(300)
                .withHousingExpenses(16000);

        openUfoAndPrepareClient(harness, credentials)
                .then(bankaTasks.expresTasks().createNewExpressApplicationWithCoApplicant(applicant, coApplicant, 560000, 84, HCisAllStavZadosti.APPLICATION_CONDITIONALLY_APROVED));
    }

    @Order(5)
    @TestCase(name = "POS Semiscoring - Scénář bez externích závazků", environments = {Environment.TST1})
    @JiraTestCase(id = "RISK-T2056")
    public void newClientWithSemiscoringNoExternalObligations(Harness harness, @Key(banker) Credentials credentials) {
        applicant.withBirthNumber("8905173552");
        applicant.withNetMonthlyIncome(140000);
        applicant.withOtherExpenses(12000);

        openUfoAndPrepareClient(harness, credentials)
                .then(bankaTasks.newProduct().createUzofka(applicant, LOAN_AMOUNT, TERM));

    }

    @AfterEach
    public void tearDown(Harness harness) {
        harness.store("reports.tmfj.execution.comment",
                "\nCIF klienta: " + harness.get(CLIENT_CIF_STORE_KEY, true) +
                        "\nAPP_KEY žádosti: " + harness.get(APP_KEY_STORE, true) +
                        "\nRC klienta: " + harness.get(BIRTH_NUMBER_STORE_KEY, true) +
                        "\nJméno klienta: " + harness.get(FULL_NAME_KEY, true) +
                        "\nUFO Session ID: " + harness.get("reports.tmfj.execution.commentufosessionid"));
    }

    private HKO101_S001_MainClientPage openUfoAndPrepareClient(Harness harness, Credentials credentials) {
        harness.store(BIRTH_NUMBER_STORE_KEY, applicant.birthNumber());
        HKO101_S001_MainClientPage ufoWindow = bankaTasks.newClient().openUfoAndFindClient(harness, credentials, applicant.birthNumber());
        List<String> applList = harness.tasks().nasDb().getAllPendingApllicationsForClient(applicant.birthNumber());
        return harness.tasks().ufo().banka().expresTasks().cancelAllPendingApplications(applList, ufoWindow);
    }
}