package cz.moneta.test.regression.transactional.payments.cashless.payment_engine.domestic.analytical;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.payment_engine.parts_of_menu.menu_clients.PaymentEngineClientsAuthorizationsForDirectDebits;
import cz.moneta.test.dsl.payment_engine.tasks.PaymentEngineTasks;
import cz.moneta.test.dsl.payment_engine.utils.PaymentEngineUtils;
import cz.moneta.test.dsl.util.credentials.Keys;
import cz.moneta.test.dsl.util.data.payment.PaymentData;
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 cz.moneta.test.harness.support.auth.Key;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Order;

import java.util.concurrent.ThreadLocalRandom;

@TestScenarioWithOrder(name = "Create/edit/delete Authorization For Direct Debit")
public class AuthorizationForDirectDebit {
    private PaymentData data;
    private static final String statusValid = "Platný";
    private static final String statusArchiv = "Archivovaný";

    @BeforeAll
    void setUpTestData(Harness harness) {
        data = PaymentData.builder()
                .debtorAcc(harness.getConfig("payments_cashless.domesticPayment.debtorAcc"))
                .creditorAcc(harness.getConfig("payments_cashless.domesticPayment.creditorAccInh"))
                .amount(String.valueOf(ThreadLocalRandom.current().nextInt(50, 99)))
                .transactionName("Autotest založení inkasa")
                .description("Autotest založení inkasa")
                .branchCode("134")
                .period("Měsíčně")
                .build();
    }

    @Order(1)
    @JiraTestCase(id = "PAY-T2999")
    @TestCase(name = "Create/edit/delete authorization for direct debit", environments = {Environment.TST1, Environment.PPE})
    void createEditDeleteAuthorizationForDirectDebitInhouseAndVerify(Harness harness, @Key(Keys.paymentEngine.firstUser) Credentials user1,
                                                                     @Key(Keys.paymentEngine.secondUser) Credentials user2) {

        PaymentEngineTasks tasks = harness.tasks().paymentEngineTasks();
        tasks.assumptionAuthorizationForDirectDebitExistence(data, user2, user1);

        tasks.loginTasks().logInPae(harness, user1)
                .then(tasks.createAuthorizationForDirectDebit(data));
        harness.withPaymentEngine().closePaymentEngine();

        tasks.loginTasks().logInPae(harness, user2)
                .then(tasks.verifyAuthorizationForDirectDebit(data));
        String rootId1 = harness.get(PaymentEngineClientsAuthorizationsForDirectDebits.AUTH_ROOT_ID_KEY);
        harness.withPaymentEngine().closePaymentEngine();

        tasks.loginTasks().logInPae(harness, user1)
                .then(tasks.checkAuthorizationForDirectDebitStatus(statusValid, rootId1))
                .then(tasks.editAuthorizationForDirectDebit(data));
        String rootId2 = harness.get(PaymentEngineClientsAuthorizationsForDirectDebits.AUTH_ROOT_ID_KEY);
        harness.withPaymentEngine().closePaymentEngine();

        tasks.loginTasks().logInPae(harness, user2)
                .then(tasks.verifyEditingOfAuthorizationsForDirectDebits(rootId2))
                .closeTab()
                .then(tasks.checkAuthorizationForDirectDebitStatus(statusArchiv, rootId1))
                .closeTab()
                .then(tasks.checkAuthorizationForDirectDebitStatus(statusValid, rootId2))
                .then(tasks.deleteAuthorizationForDirectDebitAndStoreRootId(data));
        String rootId3 = harness.get(PaymentEngineClientsAuthorizationsForDirectDebits.AUTH_ROOT_ID_KEY);
        harness.withPaymentEngine().closePaymentEngine();

        tasks.loginTasks().logInPae(harness, user1)
                .then(tasks.verifyEditingOfAuthorizationsForDirectDebits(rootId3))
                .closeTab()
                .then(tasks.checkAuthorizationForDirectDebitStatus(statusArchiv, rootId1))
                .closeTab()
                .then(tasks.checkAuthorizationForDirectDebitStatus(statusArchiv, rootId2))
                .closeTab()
                .then(tasks.checkAuthorizationForDirectDebitStatus(statusArchiv, rootId3));
        harness.withPaymentEngine().closePaymentEngine();

        PaymentEngineUtils.logAuthorizationForDirectDebitStatus(harness, rootId1, rootId2, rootId3, data);
    }
}