package cz.moneta.test.regression.channels.digital.ib.newib.messagesAndDocuments;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.newib.tasks.NewIbTasks;
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.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Order;

@TestScenarioWithOrder(name = "Messages tests")
public class MessagesTest {
    Credentials clientCredentials;
    String clientId;

    @BeforeAll
    public void getCredentials(Harness harness) {
        String clientBirthNumber = harness.getConfig("nib.rc.messages");

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

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

    @Order(1)
    @TestCase(name = "Messages - received messages", environments = {Environment.TST1, Environment.PPE})
    @JiraTestCase(id = "SB-T9178")
    public void receivedMessagesTest(Harness harness) {
        NewIbTasks newIbTasks = harness.tasks().newIb();
        harness.withNewIb()
                .openLoginPage()
                .then(newIbTasks.loginWithUsernamePasswordAndDefaultMobKey(clientCredentials.getUsername(), clientCredentials.getPassword()))
                .then(newIbTasks.receivedMessagesTest());
    }

    @Order(2)
    @TestCase(name = "Messages - sending a message to the bank", environments = {Environment.TST1, Environment.PPE})
    @JiraTestCase(id = "SB-T9177")
    public void sendingMessageToTheBankTest(Harness harness) {
        String lastMessageSubject = "Last Message Subject";

        NewIbTasks newIbTasks = harness.tasks().newIb();
        harness.withNewIb()
                .openLoginPage()
                .then(newIbTasks.loginWithUsernamePasswordAndDefaultMobKey(clientCredentials.getUsername(), clientCredentials.getPassword()))
                .then(newIbTasks.sendingAmessageToTheBankTest(lastMessageSubject));

        String messageSubject = harness.withUdebs()
                .messageMovements()
                .getLastMessageMovement(10, clientId, lastMessageSubject);

        Assertions.assertEquals(lastMessageSubject, messageSubject);
        harness.log("Generated message is:" + lastMessageSubject + " and message in UDEBS is: " + messageSubject);
    }
}