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

import cz.moneta.test.dsl.Harness;
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.TestScenario;
import cz.moneta.test.harness.support.auth.Credentials;
import cz.moneta.test.harness.support.auth.Key;
import cz.moneta.test.dsl.payment_engine.model.FileForInputPreparation;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Order;

import java.io.*;
import java.util.concurrent.ThreadLocalRandom;

import static cz.moneta.test.dsl.payment_engine.enums.CurrencyPAE.CURRENCY_EUR_SHORT;
import static cz.moneta.test.dsl.payment_engine.parts_of_menu.menu_monitoring.PaymentEngineMonitoringInstanceFileRecords.TRANSACTION_ID_KEY;
import static cz.moneta.test.dsl.payment_engine.enums.TransactionReasonPAE.DISTRIBUTION;

@TestScenario(name = "Foreign Payment From Capitol controller")
public class ForeignPaymentFromCapitolTest extends FileForInputPreparation {
    private String PACS009_FILE;
    private String PAIN001_FILE;
    private PaymentData data;
    private PaymentEngineTasks tasks;

    @BeforeAll
    void testSetUp(Harness harness) {
        PACS009_FILE = harness.getConfig("payments_cashless.foreignPayment.pacs009File");
        PAIN001_FILE = harness.getConfig("payments_cashless.foreignPayment.pain001File");
    }

    @BeforeEach
    public void testSetUpUniqueData(Harness harness) {
        data = PaymentData.builder()
                .amount(ThreadLocalRandom.current().nextInt(1, 9) + "." + ThreadLocalRandom.current().nextInt(10, 99))
                .messageId(String.valueOf(ThreadLocalRandom.current().nextInt(100, 999)))
                .random(String.valueOf(ThreadLocalRandom.current().nextInt(10000, 99999)))
                .build();
        tasks = harness.tasks().paymentEngineTasks();
    }

    @Order(1)
    @JiraTestCase(id = "PAY-T3595")
    @TestCase(name = "Create Input Pacs009 From Capitol", environments = {Environment.TST1, Environment.PPE})
    void createInputPacs009FromCapitol(Harness harness, @Key(Keys.paymentEngine.firstUser) Credentials user1) throws IOException {

        String newFileName = "Pacs.009_" + DateUtils.getFormattedTodayDate("yyyyMMdd") + data.getRandom() + ".xml";
        File file = createTemporaryFileForInput(PACS009_FILE, newFileName);

        try {
            tasks.loginTasks().logInPae(harness, user1)
                    .then(tasks.inputPacs009FromCapitol(file.getAbsolutePath()))
                    .then(tasks.checkStatusOfPacs009FromCapitol(DISTRIBUTION, newFileName));
            harness.withPaymentEngine().closePaymentEngine();

            PaymentEngineUtils.logResultOfInput(harness, newFileName, harness.get(TRANSACTION_ID_KEY), data.getAmount(), CURRENCY_EUR_SHORT.getCurrency());
        } catch (Exception e) {
            throw e;
        } finally {
            file.delete();
        }
    }

    @Order(2)
    @JiraTestCase(id = "PAY-T3596")
    @TestCase(name = "Create Input Pain001 From Capitol", environments = {Environment.TST1, Environment.PPE})
    void createInputPain001FromCapitol(Harness harness, @Key(Keys.paymentEngine.firstUser) Credentials user1) throws IOException {

        String newFileName = "Pain.001_" + DateUtils.getFormattedTodayDate("yyyyMMdd") + data.getRandom() + ".xml";
        File file = createTemporaryFileForInput(PAIN001_FILE, newFileName);
        try {
            tasks.loginTasks().logInPae(harness, user1)
                    .then(tasks.inputPain001FromCapitol(file.getAbsolutePath()))
                    .then(tasks.checkStatusOfPain001FromCapitol(DISTRIBUTION, newFileName));
            harness.withPaymentEngine().closePaymentEngine();

            PaymentEngineUtils.logResultOfInput(harness, newFileName, harness.get(TRANSACTION_ID_KEY), data.getAmount(), CURRENCY_EUR_SHORT.getCurrency());
        } catch (Exception e) {
            throw e;
        } finally {
            file.delete();
        }
    }
}
