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.PaymentEngineClientsListOfStandingOrders;
import cz.moneta.test.dsl.payment_engine.tasks.PaymentEngineTasks;
import cz.moneta.test.dsl.payment_engine.utils.PaymentEngineUtils;
import cz.moneta.test.dsl.util.DateUtils;
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.BeforeEach;
import org.junit.jupiter.api.Order;

import java.util.concurrent.ThreadLocalRandom;

import static cz.moneta.test.dsl.payment_engine.parts_of_menu.menu_clients.PaymentEngineClientsListOfStandingOrdersTransactions.TRANSACTION_BY_DATE_ID_KEY;

@TestScenarioWithOrder(name = "Create/edit/delete domestic standing order")
public class StandingOrder {
    private static final String statusValid = "Platný";
    private static final String statusArchiv = "Archivovaný";
    private PaymentData data;
    private PaymentEngineTasks tasks;

    @BeforeAll
    void setUpTestData(Harness harness) {
        data = PaymentData.builder()
                .transactionType("HD:11 Úhrada klientská")
                .period("Měsíčně")
                .transactionName("Harness Autotest TPP")
                .description("Harness Autotest TPP")
                .branchCode("134")
                .dayMonth(String.valueOf(ThreadLocalRandom.current().nextInt(1, 25)))
                .amount(String.valueOf(ThreadLocalRandom.current().nextInt(50, 99)))
                .debtorAcc(harness.getConfig("payments_cashless.domesticPayment.debtorAcc"))
                .creditorAcc(harness.getConfig("payments_cashless.domesticPayment.creditorAccInh"))
                .build();
    }

    @BeforeEach
    void setUpUniqueTestData(Harness harness) {
        tasks = harness.tasks().paymentEngineTasks();
    }

    @Order(1)
    @JiraTestCase(id = "PAY-T2998")
    @TestCase(name = "Create/edit/delete standing order CZ", environments = {Environment.TST1, Environment.PPE})
    void createEditDeleteStandingOrder(Harness harness, @Key(Keys.paymentEngine.firstUser) Credentials user1,
                                       @Key(Keys.paymentEngine.secondUser) Credentials user2) {

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

        tasks.loginTasks().logInPae(harness, user2)
                .then(tasks.verifyStandingOrder(data));
        String rootId1 = harness.get(PaymentEngineClientsListOfStandingOrders.STAND_ORDER_ROOT_ID_KEY);
        harness.withPaymentEngine().closePaymentEngine();

        tasks.loginTasks().logInPae(harness, user1)
                .then(tasks.checkStandingOrderStatus(statusValid, rootId1))
                .then(tasks.editStandingOrder(data));
        String rootId2 = harness.get(PaymentEngineClientsListOfStandingOrders.STAND_ORDER_ROOT_ID_KEY);
        harness.withPaymentEngine().closePaymentEngine();

        tasks.loginTasks().logInPae(harness, user2)
                .then(tasks.verifyEditingOfStandingOrder(rootId2))
                .then(tasks.checkStandingOrderStatus(statusArchiv, rootId1))
                .closeTab()
                .then(tasks.checkStandingOrderStatus(statusValid, rootId2))
                .then(tasks.deleteStandingOrder(data));
        String rootId3 = harness.get(PaymentEngineClientsListOfStandingOrders.STAND_ORDER_ROOT_ID_KEY);
        harness.withPaymentEngine().closePaymentEngine();

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

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

    @Order(2)
    @JiraTestCase(id = "PAY-T4048")
    @TestCase(name = "Check Transaction From Standing Order CZ Created", environments = {Environment.TST1, Environment.PPE})
    void checkTransactionFromStandingOrderCZCreated(Harness harness, @Key(Keys.paymentEngine.firstUser) Credentials user1) {

        data.setOrderId(harness.getConfig("payments_cashless.domesticPayment.standingOrderId"));
        data.setDebtorAcc(harness.getConfig("payments_cashless.domesticPayment.accountCZK"));
        data.setCreditorAcc(harness.getConfig("payments_cashless.domesticPayment.debtorAcc"));
        data.setAmount("15,49");
        data.setSettlementDate(DateUtils.getFormattedTodayDate("dd.MM.yyyy"));

        tasks.loginTasks().logInPae(harness, user1)
                .then(tasks.checkStandingOrderStatus(statusValid, data.getOrderId()))
                .then(tasks.checkStandingOrderTransactionsPresence(data));
        harness.withPaymentEngine().closePaymentEngine();

        PaymentEngineUtils.logStandingOrderTransactionStatus(harness, data.getOrderId(), harness.get(TRANSACTION_BY_DATE_ID_KEY), data);
    }
}
