package cz.moneta.test.regression.transactional.payments.cashless.ufo.banka.analytical;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.payment_engine.tasks.PaymentEngineTasks;
import cz.moneta.test.dsl.payment_engine.utils.PaymentEngineUtils;
import cz.moneta.test.dsl.ufo.banka.tasks.BankaTasks;
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.JiraTestCase;
import cz.moneta.test.harness.annotations.TestCase;
import cz.moneta.test.harness.annotations.TestScenario;
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.enums.TransactionReasonPAE.ARCHIV;
import static cz.moneta.test.dsl.ufo.banka.pages.common.pages.OBR001_S001_PrintInstructionPage.ROOT_ID_KEY;
import static cz.moneta.test.harness.annotations.Environment.*;

@TestScenario(name = "Create Priority Payment UFO Banka")
public class PriorityPaymentUfoBanka {
    private BankaTasks bankaTasks;
    private PaymentEngineTasks paymentEngineTasks;
    private PaymentData data;
    private String TOKEN_CBL;

    @BeforeAll
    void setUpTestData(Harness harness) {
        data = PaymentData.builder()
                .todayDate(DateUtils.convertRawDateForWso2(DateUtils.getTodayRawDate()))
                .debtorAcc(harness.getConfig("payments_cashless.domesticPayment.debtorAccUFO"))
                .creditorAcc(harness.getConfig("payments_cashless.domesticPayment.creditorAccInh"))
                .bankCode("0600")
                .build();
        TOKEN_CBL = harness.tasks().wso2().authorization().getToken("DigitalchannelsSB_CBL");
    }

    @BeforeEach
    void setUpUniqueData(Harness harness) {
        data.setAmount(String.valueOf(ThreadLocalRandom.current().nextInt(1, 299)));
        bankaTasks = harness.tasks().ufo().banka();
        paymentEngineTasks = harness.tasks().paymentEngineTasks();
    }

    private void checkTransactionStatusBooked(Harness harness, String account, String amountICBS, String transactionId) {
        String request = harness.tasks()
                .wso2()
                .payments()
                .createRequestForTransactionCheck(account, data.getTodayDate(), amountICBS);
        harness.tasks()
                .wso2()
                .payments()
                .checkTransactionStatusBooked(TOKEN_CBL, request, transactionId);
    }

    @Order(1)
    @JiraTestCase(id = "PAY-T3542")
    @TestCase(name = "Create Priority Payment in UFO Banka and check in PAE and ICBS", environments = {TST1, PPE})
    void createPriorityPaymentInUfoAndCheckInPaeAndIcbs(Harness harness, @Key(Keys.ufo.banka.banker) Credentials ufoCredentials,
                                                        @Key(Keys.paymentEngine.firstUser) Credentials paeCredentials) {
        harness.withUfo()
                .openLoginPage()
                .then(bankaTasks.login().withCredentials(ufoCredentials))
                .then(bankaTasks.payment().createPriorityPaymentMaxData(data))
                .then(bankaTasks.payment().savePaymentOrder());
        harness.withUfo().closeApplication();

        String transactionId = harness.get(ROOT_ID_KEY);

        paymentEngineTasks.loginTasks().logInPae(harness, paeCredentials)
                .then(paymentEngineTasks.searchTransactionAndWaitForStatus(ARCHIV, transactionId));
        harness.withPaymentEngine().closePaymentEngine();

        checkTransactionStatusBooked(harness, data.getDebtorAcc(), data.getAmount(), transactionId);
        checkTransactionStatusBooked(harness, data.getCreditorAcc(), data.getAmount(), transactionId);

        PaymentEngineUtils.logResultUfoWithPaeAndICBS(harness, transactionId, data);
    }
}
