package cz.moneta.test.utils.loans;

import cz.moneta.test.dataprepare.retail.loans.BeforeEachExpresLoanTest;
import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.ufo.banka.pages.loans.expres.HousingLoanData;
import cz.moneta.test.dsl.ufo.banka.pages.loans.expres.NEP006_S001_ExpressScoringResultPage;
import cz.moneta.test.dsl.ufo.banka.pages.main.codelists.HCisAllLoanObject;
import cz.moneta.test.dsl.ufo.banka.pages.main.codelists.HCisBLRateFixation;
import cz.moneta.test.dsl.ufo.banka.pages.main.codelists.HCisPropertyOwnershipType;
import cz.moneta.test.dsl.ufo.banka.pages.main.codelists.HCisUcelUveruExpres;
import cz.moneta.test.dsl.ufo.banka.tasks.expres.ExpresLoanDataPreparationTasks;
import cz.moneta.test.dsl.util.data.client.MaritalStatus;
import cz.moneta.test.dsl.util.data.client.NonClient;
import cz.moneta.test.harness.annotations.ParameterizedTestCase;
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.Assertions;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.List;
import java.util.stream.Stream;

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

@TestScenario(name = "Housing Loan Data Preparation and Purpose Validation")
public class HousingLoanUtilTests extends BeforeEachExpresLoanTest {

    private final Harness harness;

    public HousingLoanUtilTests(Harness harness) {
        this.harness = harness;
    }

    @TestCase(name = "Data preparation for Expres with married couple")
    public void prepareDataWithMarriedClients(Harness harness) {
        data.setClient(data.getClient().withMaritalStatus(MaritalStatus.MARRIED));

        NonClient wife = bankaTasks.expresTasks().expressLoanDataPreparationTasks().createWifeData()
                .withResidencyAddress(data.getClient().residencyAddress());

        //data.getHousingLoanData().isRealityCustomAddress(true);
        data.setCoApplicants(List.of(wife));
        bankaTasks.expresTasks().expressLoanDataPreparationTasks().prepareCoApplicants(data.getCoApplicants());
        harness.log("\n" +
                "Husband: " + data.getClient().fullName() + ", " + data.getClient().birthNumber() + "\n" +
                "Wife: " + wife.fullName() + ", " + wife.birthNumber() + ", ID number: " + wife.idCard().getNumber());
    }

    @ParameterizedTestCase(name = "Housing loans with each purpose")
    @MethodSource("getValuesForTest")
    public void housingLoanEachPurpose(int amount, int term, List<NonClient> coApplicants, HCisAllLoanObject realityType, HCisPropertyOwnershipType ownershipType, HCisUcelUveruExpres loanPurpose, Harness harness, @Key(banker) Credentials credentials) {
        data.setCoApplicants(coApplicants);
        HousingLoanData hlData = data.getHousingLoanData();
        hlData.setRealityCustomAddress(false);
        hlData.setRealityType(realityType);
        hlData.setOwnershipType(ownershipType);
        hlData.setPurposeType(loanPurpose);
        hlData.setFixation(HCisBLRateFixation._5Y);
        data.setAmount(amount);
        data.setTerm(term);

        ExpresLoanDataPreparationTasks preparationTasks = bankaTasks.expresTasks().expressLoanDataPreparationTasks();

        if (data.getCoApplicants() != null && !data.getCoApplicants().isEmpty()) {
            preparationTasks.prepareCoApplicants(data.getCoApplicants());
        }

        NEP006_S001_ExpressScoringResultPage page = harness.withUfo().openLoginPage()
                .then(bankaTasks.login().withCredentialsAndTurnOffSignpad(credentials))
                .then(bankaTasks.expresTasks().housingLoanTasks().housingLoanE2ETask(data));
        Assertions.assertEquals("Úvěr načerpán", page.getAppStat());
    }


    private Stream<Arguments> getValuesForTest() {
        ExpresLoanDataPreparationTasks tasks = harness.tasks().ufo().banka().expresTasks().expressLoanDataPreparationTasks();
        return Stream.of(
                Arguments.of(50000, 60, null, HCisAllLoanObject.FAMILY_HOUSE, HCisPropertyOwnershipType.PERSONAL, HCisUcelUveruExpres.RECONSTRUCTION),
                Arguments.of(55000, 61, null, HCisAllLoanObject.FAMILY_HOUSE, HCisPropertyOwnershipType.PERSONAL, HCisUcelUveruExpres.RECONSTRUCTION_WITH_ENERGY_SAVINGS),
                Arguments.of(200000, 75, null, HCisAllLoanObject.FAMILY_HOUSE, HCisPropertyOwnershipType.PERSONAL, HCisUcelUveruExpres.CONSTRUCTION),
                Arguments.of(75000, 13, null, HCisAllLoanObject.FAMILY_HOUSE, HCisPropertyOwnershipType.PERSONAL, HCisUcelUveruExpres.CONSTRUCTION_WITH_ENERGY_SAVINGS),
                Arguments.of(799000, 300, null, HCisAllLoanObject.APARTMENT, HCisPropertyOwnershipType.PERSONAL, HCisUcelUveruExpres.RECONSTRUCTION),
                Arguments.of(200000, 108, List.of(tasks.createCoApplicantData(), tasks.createSecondCoApplicantData()), HCisAllLoanObject.APARTMENT, HCisPropertyOwnershipType.COOPERATIVE, HCisUcelUveruExpres.TRANSFER_OF_COOPERATIVE_SHARE)
        );
    }
}
