package cz.moneta.test.regression.transactional.payments.cashlessFirstTests.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.Disabled;
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 priority payment")
public class PriorityPayment {
    private String TOKEN_CBL;
    private PaymentData data;
    private PaymentEngineTasks tasks;
    private String transactionId;

    @BeforeAll
    void setUpTestData(Harness harness) {
        data = PaymentData.builder()
                .transactionType("HD:01 Prioritní úhrada klientská")
                .channelId("PAE - Payment Engine")
                .nextDayDist("Ne")
                .payScheme("CZ CT Priority")
                .debtorName("TEST")
                .icbsProfile("COU01 - HD:01")
                .bankCode(harness.getConfig("payments_cashless.domesticPayment.bankCodeCr"))
                .bankName(harness.getConfig("payments_cashless.domesticPayment.bankNameCr"))
                .todayDate(DateUtils.convertRawDateForWso2(DateUtils.getTodayRawDate()))
                .submissionDate(DateUtils.getFormattedTodayDateTime("dd.MM.yyyy HH:mm:ss"))
                .documentDate(DateUtils.getFormattedTodayDate("dd.MM.yyyy"))
                .documentId(String.valueOf(ThreadLocalRandom.current().nextInt(100, 999)))
                .dateXpath("")
                .debtorAcc(harness.getConfig("payments_cashless.domesticPayment.debtorAcc"))
                .creditorAcc(harness.getConfig("payments_cashless.domesticPayment.creditorAcc"))
                .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 = 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);
    }

    public void createDomesticPayment(Harness harness, Credentials user1) {
        tasks.loginTasks().logInPae(harness, user1)
                .then(tasks.createDomesticPayment(data));
        transactionId = PaymentEngineUtils.getTransactionalId(harness);
        harness.withPaymentEngine().closePaymentEngine();
    }

    public void createDomesticPaymentMaxData(Harness harness, Credentials user1) {
        tasks.loginTasks().logInPae(harness, user1)
                .then(tasks.createDomesticPaymentWithMaxData(data));
        transactionId = PaymentEngineUtils.getTransactionalId(harness);
        harness.withPaymentEngine().closePaymentEngine();
    }

    public void verifyAndWaitForStatusAndRunCZCTDDistribution(Harness harness, Credentials user2,
                                                              TransactionReasonPAE status1, TransactionReasonPAE status2) {
        tasks.loginTasks().logInPae(harness, user2)
                .then(tasks.verifyAndWaitForStatus(transactionId, status1))
                .closeTab()
                .then(tasks.sendAndRunCZCTDDistribution(data, status2, transactionId));
        harness.withPaymentEngine().closePaymentEngine();
    }

    @Order(1)
    @JiraTestCase(id = "PAY-T2827")
    @TestCase(name = "Create outgoing priority payment and verify", environments = {Environment.TST1, Environment.PPE})
    void CreateOutgoingPriorityPaymentAndVerify(Harness harness, @Key(Keys.paymentEngine.firstUser) Credentials user1,
                                                @Key(Keys.paymentEngine.secondUser) Credentials user2) {
        createDomesticPayment(harness, user1);
        data.setBankName("MONETA Money Bank, a.s.");

        verifyAndWaitForStatusAndRunCZCTDDistribution(harness, user2, DISTRIBUTION, ARCHIV);
        checkTransactionStatusBooked(harness, data.getDebtorAcc(), data.getAmount(), transactionId);

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

    @Order(2)
    @JiraTestCase(id = "PAY-T3001")
    @TestCase(name = "Create outgoing priority payment - Max SS, VS, KS", environments = {Environment.TST1, Environment.PPE})
    void CreateOutgoingPriorityPaymentMaxData(Harness harness, @Key(Keys.paymentEngine.firstUser) Credentials user1,
                                              @Key(Keys.paymentEngine.secondUser) Credentials user2) {
        data.setBankName(harness.getConfig("payments_cashless.domesticPayment.bankNameCr"));

        createDomesticPaymentMaxData(harness, user1);

        data.setBankName("MONETA Money Bank, a.s.");

        verifyAndWaitForStatusAndRunCZCTDDistribution(harness, user2, DISTRIBUTION, ARCHIV);
        checkTransactionStatusBooked(harness, data.getDebtorAcc(), data.getAmount(), transactionId);

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

    @Order(3)
    @JiraTestCase(id = "PAY-T3553")
    @TestCase(name = "Create inhouse priority payment - Max SS, VS, KS", environments = {Environment.TST1, Environment.PPE})
    void CreateInhousePriorityPaymentMaxData(Harness harness, @Key(Keys.paymentEngine.firstUser) Credentials user1,
                                             @Key(Keys.paymentEngine.secondUser) Credentials user2) {
        data.setCreditorAcc(harness.getConfig("payments_cashless.domesticPayment.creditorAccInh"));
        data.setBankCode("0600");
        data.setBankName("MONETA Money Bank, a.s.");

        createDomesticPaymentMaxData(harness, user1);

        tasks.loginTasks().logInPae(harness, user2)
                .then(tasks.verifyAndWaitForStatus(transactionId, ARCHIV))
                .checkSelectedTransactionData("HD:11 odchozi");
        harness.withPaymentEngine().closePaymentEngine();

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

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

    @Disabled("Not currently maintained")
    @Order(4)
    @JiraTestCase(id = "PAY-T2828")
    @TestCase(name = "Create inhouse priority payment and verify", environments = {Environment.TST1, Environment.PPE})
    void CreateInhousePriorityPaymentAndVerify(Harness harness, @Key(Keys.paymentEngine.firstUser) Credentials user1,
                                               @Key(Keys.paymentEngine.secondUser) Credentials user2) {
        data.setCreditorAcc(harness.getConfig("payments_cashless.domesticPayment.creditorAccInh"));
        data.setBankCode("0600");
        data.setBankName("MONETA Money Bank, a.s.");

        createDomesticPayment(harness, user1);

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

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

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