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.enums.CurrencyPAE;
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.ARCHIV;

@TestScenarioWithOrder(name = "Create IFCY payment and verify")
public class InhouseIFCY {
    private String TOKEN_CBL;
    private PaymentData data;
    private PaymentEngineTasks tasks;
    private String transactionId;

    @BeforeAll
    void setUpTestData(Harness harness) {
        data = PaymentData.builder()
                .channelId("PAE - Payment Engine")
                .icbsProfile("COUFX - FX konverze")
                .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(10, 99)));
        tasks = new PaymentEngineTasks(harness);
    }

    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);
    }

    private void createInhouseIfcyPaymentInEur(Harness harness, Credentials user1, CurrencyPAE currencyPAE) {
        tasks.loginTasks().logInPae(harness, user1)
                .then(tasks.createInhouseIfcyPaymentInEur(data, currencyPAE));
        transactionId = PaymentEngineUtils.getTransactionalId(harness);
        harness.withPaymentEngine().closePaymentEngine();
    }

    private void createInhouseIfcyPaymentInCzk(Harness harness, Credentials user1, CurrencyPAE currencyPAE) {
        tasks.loginTasks().logInPae(harness, user1)
                .then(tasks.createInhouseIfcyPaymentInCzk(data, currencyPAE));
        transactionId = PaymentEngineUtils.getTransactionalId(harness);
        harness.withPaymentEngine().closePaymentEngine();
    }

    @Order(1)
    @JiraTestCase(id = "PAY-T3556")
    @TestCase(name = "Create inhouse IFCY payment with maximal data filling - CZK to EUR - currency EUR", environments = {Environment.TST1, Environment.PPE})
    void createInhouseIfcyPaymentCzkToEurInEurMaxData(Harness harness, @Key(Keys.paymentEngine.firstUser) Credentials user1,
                                                      @Key(Keys.paymentEngine.secondUser) Credentials user2) {
        data.setDebtorAcc(harness.getConfig("payments_cashless.domesticPayment.accountCZK"));
        data.setCreditorAcc(harness.getConfig("payments_cashless.domesticPayment.accountEUR"));

        createInhouseIfcyPaymentInEur(harness, user1, CURRENCY_EUR_LONG);

        tasks.loginTasks().logInPae(harness, user2)
                .then(tasks.verifyAndWaitForStatus(transactionId, ARCHIV))
                .wait5sec() //ICBS cannot book IFCY transaction faster than 5 seconds
                .storeEquivalentAmountEur();
        harness.withPaymentEngine().closePaymentEngine();

        data.setEquivalentAmount(harness.get(PaymentEngineSearchPage.EQUIVALENT_AMOUNT_KEY).toString().replace(".", "").replace(",", "."));

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

        PaymentEngineUtils.logResultWithEquivalentAmount(harness, transactionId, data.getDebtorAcc(),
                data.getCreditorAcc(), data.getAmount() + " EUR", data.getEquivalentAmount() + " CZK");
    }

    @Order(2)
    @JiraTestCase(id = "PAY-T3557")
    @TestCase(name = "Create inhouse IFCY payment with maximal data filling - EUR to CZK - currency EUR", environments = {Environment.TST1, Environment.PPE})
    void createInhouseIfcyPaymentEurToCzkInEurMaxData(Harness harness, @Key(Keys.paymentEngine.firstUser) Credentials user1,
                                                      @Key(Keys.paymentEngine.secondUser) Credentials user2) {
        data.setDebtorAcc(harness.getConfig("payments_cashless.domesticPayment.accountEUR"));
        data.setCreditorAcc(harness.getConfig("payments_cashless.domesticPayment.accountCZK"));

        createInhouseIfcyPaymentInCzk(harness, user1, CURRENCY_EUR_LONG);

        tasks.loginTasks().logInPae(harness, user2)
                .then(tasks.verifyAndWaitForStatus(transactionId, ARCHIV))
                .clickLifeCycle()
                .refreshLifeCycle()
                .wait5sec() //ICBS cannot book IFCY transaction faster than 5 seconds
                .storeEquivalentAmountCzk("1");
        harness.withPaymentEngine().closePaymentEngine();

        data.setEquivalentAmount(harness.get(PaymentEngineSearchPage.EQUIVALENT_AMOUNT_KEY).toString().replace(".", "").replace(",", "."));

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

        PaymentEngineUtils.logResultWithEquivalentAmount(harness, transactionId, data.getDebtorAcc(),
                data.getCreditorAcc(), data.getAmount() + " EUR", data.getEquivalentAmount() + " CZK");
    }

    @Order(3)
    @JiraTestCase(id = "PAY-T3558")
    @TestCase(name = "Create inhouse IFCY payment with maximal data filling - CZK to EUR - currency CZK", environments = {Environment.TST1, Environment.PPE})
    void createInhouseIfcyPaymentCzkToEurInCzkMaxData(Harness harness, @Key(Keys.paymentEngine.firstUser) Credentials user1,
                                                      @Key(Keys.paymentEngine.secondUser) Credentials user2) {
        data.setDebtorAcc(harness.getConfig("payments_cashless.domesticPayment.accountCZK"));
        data.setCreditorAcc(harness.getConfig("payments_cashless.domesticPayment.accountEUR"));

        createInhouseIfcyPaymentInCzk(harness, user1, CURRENCY_CZK_LONG);

        tasks.loginTasks().logInPae(harness, user2)
                .then(tasks.verifyAndWaitForStatus(transactionId, ARCHIV))
                .clickLifeCycle()
                .refreshLifeCycle()
                .wait5sec() //ICBS cannot book IFCY transaction faster than 5 seconds
                .storeEquivalentAmountEur();
        harness.withPaymentEngine().closePaymentEngine();

        data.setEquivalentAmount(harness.get(PaymentEngineSearchPage.EQUIVALENT_AMOUNT_KEY).toString().replace(".", "").replace(",", "."));

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

        PaymentEngineUtils.logResultWithEquivalentAmount(harness, transactionId, data.getDebtorAcc(),
                data.getCreditorAcc(), data.getAmount() + " CZK", data.getEquivalentAmount() + " EUR");
    }

    @Order(4)
    @JiraTestCase(id = "PAY-T3559")
    @TestCase(name = "Create inhouse IFCY payment with maximal data filling - EUR to CZK - currency CZK", environments = {Environment.TST1, Environment.PPE})
    void createInhouseIfcyPaymentEurToCzkInCzkMaxData(Harness harness, @Key(Keys.paymentEngine.firstUser) Credentials user1,
                                                      @Key(Keys.paymentEngine.secondUser) Credentials user2) {
        data.setDebtorAcc(harness.getConfig("payments_cashless.domesticPayment.accountEUR"));
        data.setCreditorAcc(harness.getConfig("payments_cashless.domesticPayment.accountCZK"));

        createInhouseIfcyPaymentInEur(harness, user1, CURRENCY_CZK_LONG);

        tasks.loginTasks().logInPae(harness, user2)
                .then(tasks.verifyAndWaitForStatus(transactionId, ARCHIV))
                .wait5sec() //ICBS cannot book IFCY transaction faster than 5 seconds
                .storeEquivalentAmountCzk();
        harness.withPaymentEngine().closePaymentEngine();

        data.setEquivalentAmount(harness.get(PaymentEngineSearchPage.EQUIVALENT_AMOUNT_KEY).toString().replace(".", "").replace(",", "."));

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

        PaymentEngineUtils.logResultWithEquivalentAmount(harness, transactionId, data.getDebtorAcc(),
                data.getCreditorAcc(), data.getAmount() + " CZK", data.getEquivalentAmount() + " EUR");
    }
}
