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

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

@TestScenarioWithOrder(name = "Create domestic instant payment")
public class InstantPayment {
    private PaymentData data;
    private String TOKEN_CBL;
    private PaymentEngineTasks tasks;
    private String transactionId;

    @BeforeAll
    void setUpTestData(Harness harness) {
        data = PaymentData.builder()
                .bankCode("0600 - MONETA Money Bank, a.s.")
                .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, 299)));
        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);
    }

    private void createAndVerifyTransactionWithoutPrefix(Harness harness, Credentials user1, Credentials user2, TransactionReasonPAE status) {
        tasks.loginTasks().logInPae(harness, user1)
                .then(tasks.createInstantTransactionWithoutPrefix(data));
        transactionId = PaymentEngineUtils.getTransactionalId(harness);
        harness.withPaymentEngine().closePaymentEngine();

        tasks.loginTasks().logInPae(harness, user2)
                .then(tasks.verifyAndWaitForStatus(transactionId, status));
        harness.withPaymentEngine().closePaymentEngine();
    }

    private void createAndVerifyTransactionWithMaxData(Harness harness, Credentials user1, Credentials user2, TransactionReasonPAE status) {
        tasks.loginTasks().logInPae(harness, user1)
                .then(tasks.createInstantTransactionWithMaxData(data));
        transactionId = PaymentEngineUtils.getTransactionalId(harness);
        harness.withPaymentEngine().closePaymentEngine();

        tasks.loginTasks().logInPae(harness, user2)
                .then(tasks.verifyAndWaitForStatus(transactionId, status));
        harness.withPaymentEngine().closePaymentEngine();
    }

    @Order(1)
    @JiraTestCase(id = "PAY-T2541")
    @TestCase(name = "Create instant payment and verify", environments = {Environment.TST1, Environment.PPE})
    void createInstantPaymentAndVerify(Harness harness, @Key(Keys.paymentEngine.firstUser) Credentials user1,
                                       @Key(Keys.paymentEngine.secondUser) Credentials user2) {
        data.setDebtorAcc(harness.getConfig("payments_cashless.domesticPayment.debtorAcc"));
        data.setCreditorAcc(harness.getConfig("payments_cashless.domesticPayment.creditorAccInh"));

        createAndVerifyTransactionWithoutPrefix(harness, user1, user2, ARCHIV);
        checkTransactionStatusBooked(harness, data.getDebtorAcc(), data.getAmount(), transactionId);

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

    @Order(2)
    @JiraTestCase(id = "PAY-T2819")
    @TestCase(name = "Create instant payment - rejected", environments = {Environment.TST1, Environment.PPE})
    void createInstantPaymentAndReject(Harness harness, @Key(Keys.paymentEngine.firstUser) Credentials user1,
                                       @Key(Keys.paymentEngine.secondUser) Credentials user2) {
        data.setDebtorAcc(harness.getConfig("payments_cashless.domesticPayment.debtorAcc"));
        data.setCreditorAcc(harness.getConfig("payments_cashless.domesticPayment.creditorAccInvalid"));

        createAndVerifyTransactionWithoutPrefix(harness, user1, user2, REJECT);

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

    @Order(3)
    @JiraTestCase(id = "PAY-T2824")
    @TestCase(name = "Create instant payment - NSF", environments = {Environment.TST1, Environment.PPE})
    void createInstantPaymentAndNSF(Harness harness, @Key(Keys.paymentEngine.firstUser) Credentials user1,
                                    @Key(Keys.paymentEngine.secondUser) Credentials user2) {
        data.setDebtorAcc(harness.getConfig("payments_cashless.domesticPayment.debtorAccNSF"));
        data.setCreditorAcc(harness.getConfig("payments_cashless.domesticPayment.creditorAccInh"));
        data.setAmount("999999999");

        createAndVerifyTransactionWithoutPrefix(harness, user1, user2, BUSINESS_ERROR);

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

    @Order(4)
    @JiraTestCase(id = "PAY-T3004")
    @TestCase(name = "Create instant payment - Max VS,KS and SS", environments = {Environment.TST1, Environment.PPE})
    void createInstantPaymentMaxData(Harness harness, @Key(Keys.paymentEngine.firstUser) Credentials user1,
                                     @Key(Keys.paymentEngine.secondUser) Credentials user2) {
        data.setDebtorAcc(harness.getConfig("payments_cashless.domesticPayment.creditorAccMax2"));
        data.setCreditorAcc(harness.getConfig("payments_cashless.domesticPayment.debtorAccMax2"));

        createAndVerifyTransactionWithMaxData(harness, user1, user2, ARCHIV);
        checkTransactionStatusBooked(harness, data.getDebtorAcc(), data.getAmount(), transactionId);
        checkTransactionStatusBooked(harness, data.getCreditorAcc(), data.getAmount(), transactionId);

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

    @Order(5)
    @JiraTestCase(id = "PAY-T3005")
    @TestCase(name = "Create outgoing instant payment - Max VS,KS and SS", environments = {Environment.TST1, Environment.PPE})
    void createOutgoingInstantPaymentMaxData(Harness harness, @Key(Keys.paymentEngine.firstUser) Credentials user1,
                                             @Key(Keys.paymentEngine.secondUser) Credentials user2) {
        data.setDebtorAcc(harness.getConfig("payments_cashless.domesticPayment.debtorAcc"));
        data.setCreditorAcc(harness.getConfig("payments_cashless.domesticPayment.creditorAccOut"));
        data.setBankCode(harness.getConfig("payments_cashless.domesticPayment.bankCodeCreditorOut"));

        createAndVerifyTransactionWithMaxData(harness, user1, user2, NOSTRO);
        checkTransactionStatusBooked(harness, data.getDebtorAcc(), data.getAmount(), transactionId);

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