package cz.moneta.test.regression.transactional.payments.cashless.payment_engine.foreign.outgoing;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.payment_engine.parts_of_menu.menu_search.PaymentEngineSearchPage;
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.enums.CurrencyPAE.*;
import static cz.moneta.test.dsl.payment_engine.enums.TransactionReasonPAE.*;

@TestScenarioWithOrder(name = "Create foreign Xct FI2FI payment in PAE and verify.")
public class OutgoingXctFI2FITest {
    private String TOKEN_CBL;
    private PaymentData data;
    private PaymentEngineTasks tasks;

    @BeforeAll
    void setUpTestData(Harness harness) {
        data = PaymentData.builder()
                .debtorAcc(harness.getConfig("payments_cashless.foreignPayment.debtorAccXCT.FI2FI"))
                .bicBank(harness.getConfig("payments_cashless.foreignPayment.bicBankXCT.FI2FI"))
                .bankCode(harness.getConfig("payments_cashless.foreignPayment.bankCodeXCT.FI2FI"))
                .todayDate(DateUtils.convertRawDateForWso2(DateUtils.getTodayRawDate()))
                .build();
        TOKEN_CBL = harness.tasks().wso2().authorization().getToken("DigitalchannelsSB_CBL");
    }

    @BeforeEach
    void setUpUniqueTestData(Harness harness) {
        data.setAmount(String.valueOf(ThreadLocalRandom.current().nextInt(1, 99)));
        data.setTrxReference("AUTO" + ThreadLocalRandom.current().nextInt(100000, 999999));
        data.setPreviousReference(String.valueOf(ThreadLocalRandom.current().nextInt(100000, 999999)));
        tasks = 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-T3597")
    @TestCase(name = "Create foreign Xct FI2FI payment and verify - without bank account", environments = {Environment.TST1, Environment.PPE})
    void createXctFI2FIPaymentWithoutBankAccountAndVerify(Harness harness, @Key(Keys.paymentEngine.firstUser) Credentials user1,
                                                          @Key(Keys.paymentEngine.secondUser) Credentials user2) {
        data.setBicFi(harness.getConfig("payments_cashless.foreignPayment.bicFiEurXCT.FI2FI"));
        data.setCreditorName(harness.getConfig("payments_cashless.foreignPayment.bankNameEurXCT.FI2FI"));
        data.setClearingPartner("INGB-EUR");

        tasks.loginTasks().logInPae(harness, user1)
                .then(tasks.createXctFI2FIPaymentWithoutBankCode(data, CURRENCY_EUR_LONG));
        String transactionId = PaymentEngineUtils.getTransactionalId(harness);
        harness.withPaymentEngine().closePaymentEngine();

        tasks.loginTasks().logInPae(harness, user2)
                .then(tasks.verifyAndWaitForStatus(transactionId, DISTRIBUTION))
                .storeEquivalentAmountEur()
                .closeTab()
                .then(tasks.sendAndRunXCTDistribution())
                .then(tasks.searchTransactionAndWaitForStatus(ARCHIV, transactionId));
        harness.withPaymentEngine().closePaymentEngine();

        String equivalentAmount = harness.get(PaymentEngineSearchPage.EQUIVALENT_AMOUNT_KEY).toString().replace(".", "").replace(",", ".");
        checkTransactionStatusBooked(harness, data.getDebtorAcc(), equivalentAmount, transactionId);

        PaymentEngineUtils.logResultWithEquivalentAmount(harness, transactionId, data.getDebtorAcc(),
                data.getBicFi() + " (BIC)", data.getAmount() + " EUR", equivalentAmount + " CZK");
    }

    @Order(2)
    @JiraTestCase(id = "PAY-T3598")
    @TestCase(name = "Create foreign XCT FI2FI payment and verify - with bank account", environments = {Environment.TST1, Environment.PPE})
    void createXctFI2FIPaymentWithBankAccountAndVerify(Harness harness, @Key(Keys.paymentEngine.firstUser) Credentials user1,
                                                       @Key(Keys.paymentEngine.secondUser) Credentials user2) {
        data.setBicFi(harness.getConfig("payments_cashless.foreignPayment.bicFiUsdXCT.FI2FI"));
        data.setCreditorName(harness.getConfig("payments_cashless.foreignPayment.bankNameUsdXCT.FI2FI"));
        data.setClearingPartner("CITI-USD");

        tasks.loginTasks().logInPae(harness, user1)
                .then(tasks.createXctFI2FIPaymentWithBankCode(data, CURRENCY_USD_LONG));
        String transactionId = PaymentEngineUtils.getTransactionalId(harness);
        harness.withPaymentEngine().closePaymentEngine();

        tasks.loginTasks().logInPae(harness, user2)
                .then(tasks.verifyAndWaitForStatus(transactionId, DISTRIBUTION))
                .storeEquivalentAmountUsd()
                .closeTab()
                .then(tasks.sendAndRunXCTDistribution())
                .then(tasks.searchTransactionAndWaitForStatus(ARCHIV, transactionId));
        harness.withPaymentEngine().closePaymentEngine();

        String equivalentAmount = harness.get(PaymentEngineSearchPage.EQUIVALENT_AMOUNT_KEY).toString().replace(".", "").replace(",", ".");
        checkTransactionStatusBooked(harness, data.getDebtorAcc(), equivalentAmount, transactionId);

        PaymentEngineUtils.logResultWithEquivalentAmount(harness, transactionId, data.getDebtorAcc(),
                data.getBicFi() + " (BIC)", data.getAmount() + " USD", equivalentAmount + " CZK");
    }
}
