package cz.moneta.test.regression.channels.digital.ib.newib.savings.fop;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.newib.tasks.NewIbTasks;
import cz.moneta.test.dsl.newib.util.SavingsFormData;
import cz.moneta.test.harness.annotations.*;
import cz.moneta.test.harness.support.auth.Credentials;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Order;

@TestScenarioWithOrder(name = "Create Savings FOP test")
public class SavingsFopTest {
    Credentials clientCredentials;
    String clientId;

    @BeforeEach
    public void setup(Harness harness) {
        String clientBirthNumber = harness.getConfig("nib.rc.termDepositFop");

        clientCredentials = harness.tasks()
                .udebs()
                .findCredentialsByRC(clientBirthNumber);

        clientId = harness.withUdebs()
                .usersTable()
                .getClientId(clientBirthNumber);
    }

    private SavingsFormData createFormData(String currency, String amount, String type, String period, String limitAmountMin, String limitAmountMax, String taxResidence1, String taxResidence2) {
        return SavingsFormData.builder()
                .currency(currency)
                .amount(amount)
                .limitAmountMin(limitAmountMin)
                .limitAmountMax(limitAmountMax)
                .termDepositType(type)
                .termDepositPeriod(period)
                .taxResidence1(taxResidence1)
                .taxResidence2(taxResidence2)
                .build();
    }

    @Order(1)
    @TestCase(name = "Create FOP JTV + RTV", environments = {Environment.TST1, Environment.PPE})
    @JiraTestCase(id = "SB-T9352")
    public void termDepositCreateFop(Harness harness) {
        String accountNumberCzk = harness.getConfig("nib.currentaccount.termDepositCzkFop");
        String accountNumberEur = harness.getConfig("nib.currentaccount.termDepositEurFop");
        double depositAmountCzk = 50000;
        harness.tasks().wso2().client().subsidyCzkAccount(accountNumberCzk, depositAmountCzk);
        double depositAmountEur = 500;
        harness.tasks().wso2().client().subsidyEurAccount(accountNumberEur, depositAmountEur);
        String operIdJtv = "411";
        String operIdRtv = "412";
        String channelId = "15";
        String expectedStatusSavingAccount = "60";

        SavingsFormData formDataJtv = createFormData("CZK", "50000", "JTV", "3 měsíce", "14999", "999999999999", "Slovensko", "Česká republika");
        SavingsFormData formDataRtv = createFormData("EUR", "500", "RTV", "6 měsíců", "499", "999999999999", "Americká Samoa", "Slovensko");

        NewIbTasks newIbTasks = harness.tasks().newIb();
        harness.withNewIb()
                .openLoginPage()
                .then(newIbTasks.loginWithUsernamePasswordAndDefaultMobKey(clientCredentials.getUsername(), clientCredentials.getPassword()))
                .then(newIbTasks.createTermDeposit(formDataJtv))
                .then(newIbTasks.createTermDeposit(formDataRtv));

        String statusCreatedJtv = harness.withUdebs()
                .transactionsClient()
                .getLastTransactionStatus(clientId, operIdJtv, channelId, 60, expectedStatusSavingAccount);

        Assertions.assertEquals(expectedStatusSavingAccount, statusCreatedJtv, "Status of created JTV  is not equals expected status");
        harness.log("Expected status is: " + expectedStatusSavingAccount + " and status in UDEBS is: " + statusCreatedJtv);

        String statusCreatedRtv = harness.withUdebs()
                .transactionsClient()
                .getLastTransactionStatus(clientId, operIdRtv, channelId, 60, expectedStatusSavingAccount);

        Assertions.assertEquals(expectedStatusSavingAccount, statusCreatedRtv, "Status of created RTV is not equals expected status");
        harness.log("Expected status is: " + expectedStatusSavingAccount + " and status in UDEBS is: " + statusCreatedRtv);
    }
}