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.*;
import static cz.moneta.test.dsl.payment_engine.parts_of_menu.menu_search.PaymentEngineSearchTransactionDetailLifeCycle.TRANSACTION_BY_TYPE_ID_KEY;

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

    @BeforeAll
    void setUpTestData(Harness harness) {
        data = PaymentData.builder()
                .transactionType("HD:11 Úhrada klientská")
                .channelId("PAE - Payment Engine")
                .debtorName("TEST")
                .icbsProfile("COU11 - HD:11")
                .bankCode("0600")
                .bankName("MONETA Money Bank, a.s.")
                .nextDayDist("Ne")
                .payScheme("CZ CT Non-priority")
                .submissionDate(DateUtils.getFormattedTodayDateTime("dd.MM.yyyy HH:mm:ss"))
                .documentDate(DateUtils.getFormattedTodayDate("dd.MM.yyyy"))
                .todayDate(DateUtils.convertRawDateForWso2(DateUtils.getTodayRawDate()))
                .documentId(String.valueOf(ThreadLocalRandom.current().nextInt(100, 999)))
                .dateXpath("//following-sibling::td[1]/a")
                .debtorAcc(harness.getConfig("payments_cashless.domesticPayment.debtorAcc"))
                .creditorAcc(harness.getConfig("payments_cashless.domesticPayment.creditorAccInh"))
                .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);
    }

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

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

    protected void verifyAndWaitForStatus(Harness harness, Credentials user2, TransactionReasonPAE status) {
        tasks.loginTasks().logInPae(harness, user2)
                .then(tasks.verifyAndWaitForStatus(transactionId, status));
        harness.withPaymentEngine().closePaymentEngine();
    }

    @Disabled("Not currently maintained")
    @Order(1)
    @JiraTestCase(id = "PAY-T2829")
    @TestCase(name = "Create outgoing standard payment and verify - MIN data", environments = {Environment.TST1, Environment.PPE})
    void createOutgoingStandardPaymentAndVerify(Harness harness, @Key(Keys.paymentEngine.firstUser) Credentials user1,
                                                @Key(Keys.paymentEngine.secondUser) Credentials user2) {
        data.setBankCode(harness.getConfig("payments_cashless.domesticPayment.bankCodeCr"));
        data.setBankName(harness.getConfig("payments_cashless.domesticPayment.bankNameCr"));

        createDomesticPayment(harness, user1);

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

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

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

    @Disabled("Not currently maintained")
    @Order(2)
    @JiraTestCase(id = "PAY-T2830")
    @TestCase(name = "Create inhouse standard payment and verify", environments = {Environment.TST1, Environment.PPE})
    void createInhouseStandardPaymentAndVerify(Harness harness, @Key(Keys.paymentEngine.firstUser) Credentials user1,
                                               @Key(Keys.paymentEngine.secondUser) Credentials user2) {
        data.setBankCode("0600 - MONETA Money Bank, a.s.");
        data.setBankName("MONETA Money Bank, a.s.");

        createDomesticPayment(harness, user1);
        verifyAndWaitForStatus(harness, user2, ARCHIV);

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

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

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

        createDomesticPaymentWithMaxData(harness, user1);
        verifyAndWaitForStatus(harness, user2, ARCHIV);

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

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

    @Order(4)
    @JiraTestCase(id = "PAY-T2876")
    @TestCase(name = "Create outgoing standard payment - D+1", environments = {Environment.TST1, Environment.PPE})
    void createOutgoingStandardPaymentNextDay(Harness harness, @Key(Keys.paymentEngine.firstUser) Credentials user1,
                                              @Key(Keys.paymentEngine.secondUser) Credentials user2) {
        data.setCreditorAcc(harness.getConfig("payments_cashless.domesticPayment.creditorAcc"));
        data.setBankCode(harness.getConfig("payments_cashless.domesticPayment.bankCodeCr"));
        data.setBankName(harness.getConfig("payments_cashless.domesticPayment.bankNameCr"));

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

        verifyAndWaitForStatus(harness, user2, FUTURE_MATURITY);

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

    @Order(5)
    @JiraTestCase(id = "PAY-T2873")
    @TestCase(name = "Create outgoing standard payment - NSF", environments = {Environment.TST1, Environment.PPE})
    void createOutgoingStandardPaymentAndNSF(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.creditorAcc"));
        data.setBankCode(harness.getConfig("payments_cashless.domesticPayment.bankCodeCr"));
        data.setBankName(harness.getConfig("payments_cashless.domesticPayment.bankNameCr"));

        createDomesticPayment(harness, user1);
        verifyAndWaitForStatus(harness, user2, REPEAT);

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

    @Disabled("Not currently maintained")
    @Order(6)
    @JiraTestCase(id = "PAY-T3003")
    @TestCase(name = "Create inhouse standard payment with maximal data filling", environments = {Environment.TST1, Environment.PPE})
    void createInhouseStandardPaymentMaxAmount(Harness harness, @Key(Keys.paymentEngine.firstUser) Credentials user1,
                                               @Key(Keys.paymentEngine.secondUser) Credentials user2) {
        data.setDebtorAcc(harness.getConfig("payments_cashless.domesticPayment.creditorAccMax1"));
        data.setCreditorAcc(harness.getConfig("payments_cashless.domesticPayment.debtorAccMax1"));
        data.setAmount("999999999");

        createDomesticPaymentWithMaxData(harness, user1);
        String transactionId1 = transactionId;

        verifyAndWaitForStatus(harness, user2, ARCHIV);

        data.setDebtorAcc(harness.getConfig("payments_cashless.domesticPayment.debtorAccMax1"));
        data.setCreditorAcc(harness.getConfig("payments_cashless.domesticPayment.creditorAccMax1"));

        createDomesticPaymentWithMaxData(harness, user1);
        String transactionId2 = transactionId;

        verifyAndWaitForStatus(harness, user2, ARCHIV);

        checkTransactionStatusBooked(harness, data.getDebtorAcc(), data.getAmount(), transactionId1);
        checkTransactionStatusBooked(harness, data.getCreditorAcc(), data.getAmount(), transactionId2);

        PaymentEngineUtils.logResultLongWithICBS(harness, transactionId1, transactionId2, data);
    }

    @Order(7)
    @JiraTestCase(id = "PAY-T3594")
    @TestCase(name = "Create outgoing standard payment and verify - MAX data", environments = {Environment.TST1, Environment.PPE})
    void createOutgoingStandardPaymentAndVerifyMaxData(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.creditorAcc"));
        data.setBankCode(harness.getConfig("payments_cashless.domesticPayment.bankCodeCr"));
        data.setBankName(harness.getConfig("payments_cashless.domesticPayment.bankNameCr"));

        createDomesticPaymentWithMaxData(harness, user1);

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

        tasks.loginTasks().logInPae(harness, user2)
                .then(tasks.verifyAndWaitForStatus(transactionId, RELEASE))
                .closeTab()
                .then(tasks.runReleaseAfterDispoCheck(DISTRIBUTION, transactionId))
                .closeTab()
                .then(tasks.sendAndRunCZCTDDistribution(data, ARCHIV, transactionId));
        harness.withPaymentEngine().closePaymentEngine();

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

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

    @Order(8)
    @JiraTestCase(id = "PAY-T3560")
    @TestCase(name = "Create Inhouse HD:32 Payment And Invalid Mandate - Max Data", environments = {Environment.TST1, Environment.PPE})
    void createInhouseHD32PaymentAndInvalidMandateMaxData(Harness harness, @Key(Keys.paymentEngine.firstUser) Credentials user1,
                                                          @Key(Keys.paymentEngine.secondUser) Credentials user2) {
        data.setTransactionType("HD:32 Žádost o inkaso");
        data.setBankCode("0600");
        data.setBankName("MONETA Money Bank, a.s.");
        data.setDebtorAcc(harness.getConfig("payments_cashless.domesticPayment.creditorAccInh"));
        data.setCreditorAcc(harness.getConfig("payments_cashless.domesticPayment.debtorAcc"));

        createDomesticPaymentWithMaxData(harness, user1);
        verifyAndWaitForStatus(harness, user2, REJECT);

        data.setTransactionType("HD:12 odchozi");

        tasks.loginTasks().logInPae(harness, user1)
                .then(tasks.searchTransactionAndWaitForStatus(REJECT, transactionId))
                .then(tasks.checkLifeCycleAndWaitForStatus(data, WITHOUT_MANDATE))
                .storeTransactionIdByType(data.getTransactionType());
        harness.withPaymentEngine().closePaymentEngine();
        PaymentEngineUtils.logHD32Result(harness, transactionId, harness.get(TRANSACTION_BY_TYPE_ID_KEY), data, REJECT, WITHOUT_MANDATE);
    }

    @Order(9)
    @JiraTestCase(id = "PAY-T3561")
    @TestCase(name = "Create Inhouse HD:32 Payment And Valid Mandate - Max Data", environments = {Environment.TST1, Environment.PPE})
    void createInhouseHD32PaymentAndValidMandateMaxData(Harness harness, @Key(Keys.paymentEngine.firstUser) Credentials user1,
                                                        @Key(Keys.paymentEngine.secondUser) Credentials user2) {
        data.setTransactionType("HD:32 Žádost o inkaso");
        data.setBankCode("0600");
        data.setBankName("MONETA Money Bank, a.s.");
        data.setDebtorAcc(harness.getConfig("payments_cashless.domesticPayment.accountCZK"));
        data.setCreditorAcc(harness.getConfig("payments_cashless.domesticPayment.creditorAccInh"));

        createDomesticPaymentWithMaxData(harness, user1);
        verifyAndWaitForStatus(harness, user2, SENT_WAIT);

        data.setTransactionType("HD:12 odchozi");

        tasks.loginTasks().logInPae(harness, user1)
                .then(tasks.searchTransactionAndWaitForStatus(SENT_WAIT, transactionId))
                .then(tasks.checkLifeCycleAndWaitForStatus(data, RELEASE))
                .storeTransactionIdByType(data.getTransactionType())
                .closeTab()
                .then(tasks.runReleaseAfterDispoCheck(ARCHIV, harness.get(TRANSACTION_BY_TYPE_ID_KEY)))
                .closeTab()
                .then(tasks.searchTransactionAndWaitForStatus(ARCHIV, transactionId));
        harness.withPaymentEngine().closePaymentEngine();

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

        PaymentEngineUtils.logHD32Result(harness, transactionId, harness.get(TRANSACTION_BY_TYPE_ID_KEY), data, ARCHIV, ARCHIV);

    }
}
