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

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.Environment;
import cz.moneta.test.harness.annotations.JiraTestCase;
import cz.moneta.test.harness.annotations.TestCase;
import cz.moneta.test.harness.annotations.TestScenarioWithOrder;
import cz.moneta.test.harness.support.auth.Credentials;
import org.junit.jupiter.api.*;

@TestScenarioWithOrder(name = "Building savings tests")
public class BuildingSavingsTest {

    private static final String OPER_ID_BS = "955";
    private static final String CHANNEL_ID = "15";
    private static final String EXPECTED_STATUS_SAVING_ACCOUNT = "40";

    private Credentials clientCredentials;
    private String clientId;
    private String userId;
    private SavingsFormData formData;

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

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

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

        userId = harness.withUdebs()
                .usersTable()
                .getUserId(clientBirthNumber);

        formData = SavingsFormData.builder()
                .minMonthlyPayment("500")
                .maxMonthlyPayment("50000")
                .defaultMonthlyPayment("1700")
                .build();
    }

    //run only in PPE, because of starbuild environment limitations
    @Order(1)
    @TestCase(name = "Establishment building savings - happy path", environments = {Environment.PPE})
    @JiraTestCase(id = "SB-T9291")
    public void establishmentBuildingSavings(Harness harness) {
        NewIbTasks newIbTasks = harness.tasks().newIb();
        harness.withNewIb()
                .openLoginPage()
                .then(newIbTasks.loginWithUsernamePasswordAndDefaultMobKey(clientCredentials.getUsername(), clientCredentials.getPassword()))
                .then(newIbTasks.establishmentBuildingSavings(formData));

        String statusCreatedBs = harness.withUdebs()
                .transactionsClient()
                .getLastTransactionStatus(clientId, OPER_ID_BS, CHANNEL_ID, 60, EXPECTED_STATUS_SAVING_ACCOUNT);

        Assertions.assertEquals(EXPECTED_STATUS_SAVING_ACCOUNT, statusCreatedBs,
                "Status of created building savings is not equal to expected status");

        harness.log("Expected status is: " + EXPECTED_STATUS_SAVING_ACCOUNT + " and status in UDEBS is: " + statusCreatedBs + " for clientId=" + clientId);
    }

    @AfterEach
    protected void resetTransactionStatusTo51(Harness harness) {
        String packageId = harness.withUdebs()
                .transactionsClient()
                .getLastTransactionPackageId(clientId, OPER_ID_BS, CHANNEL_ID, 60);

        harness.withUdebs()
                .setTransactionStatus()
                .setStatusTo51(packageId, userId);

        harness.log("Transaction with packageId=" + packageId + " reset to status 51 for userId=" + userId);
    }
}