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

import com.google.gson.JsonObject;
import cz.moneta.test.dsl.Harness;
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.dsl.payment_engine.model.CustFeeCalculatorResponse;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.platform.commons.util.StringUtils;

import java.math.BigDecimal;

@TestScenarioWithOrder(name = "Fee calculator tests for foreign payments")
public class FeeCalculator {

    private String TOKEN_CBL;

    @BeforeAll
    void testSetUp(Harness harness) {
        TOKEN_CBL = harness.tasks().wso2().authorization().getToken("DigitalchannelsSB_CBL");
    }

    @JiraTestCase(id = "PAY-T1664")
    @TestCase(name = "Call calculator with accountId", environments = {Environment.TST1, Environment.PPE})
    void callCalculatorWithAccountId(Harness harness) {
        String accountId = harness.getConfig("payments_cashless.feeCalculator.account");
        String request = createRequest(accountId, "", "USD", new BigDecimal(700), "PMFAUS66", "NORM");

        CustFeeCalculatorResponse response = harness.withWso2()
                .prepareRequest()
                .withPath("/t/payments.cluster/PAE/cust-fee-calculator/v1/calculate")
                .withHeader("Content-Type", "application/json")
                .withHeader("Accept", "application/json")
                .withHeader("Authorization", TOKEN_CBL)
                .withHeader("cache-control", "no-cache,no-cache")
                .withPayload(request)
                .post()
                .andAssertStatus(200)
                .mapResponseIntoObject(CustFeeCalculatorResponse.class);

        Assertions.assertEquals(new BigDecimal("220"), response.fees.get(0).getFeeAmount().getValue());
        Assertions.assertEquals("BOF_STP", response.fees.get(0).getFeeCode().toString());

    }

    @JiraTestCase(id = "PAY-T1665")
    @TestCase(name = "Call calculator with Iban", environments = {Environment.TST1, Environment.PPE})
    void callCalculatorWithIban(Harness harness) {
        String iban = harness.getConfig("payments_cashless.feeCalculator.iban");
        String request = createRequest("", iban, "USD", new BigDecimal(700), "PMFAUS66", "NORM");

        CustFeeCalculatorResponse response = harness.withWso2()
                .prepareRequest()
                .withPath("/t/payments.cluster/PAE/cust-fee-calculator/v1/calculate")
                .withHeader("Content-Type", "application/json")
                .withHeader("Accept", "application/json")
                .withHeader("Authorization", TOKEN_CBL)
                .withHeader("cache-control", "no-cache,no-cache")
                .withPayload(request)
                .post()
                .andAssertStatus(200)
                .mapResponseIntoObject(CustFeeCalculatorResponse.class);

        Assertions.assertEquals(new BigDecimal("220"), response.fees.get(0).getFeeAmount().getValue());
        Assertions.assertEquals("BOF_STP", response.fees.get(0).getFeeCode().toString());
    }

    private String createRequest(String accountId, String iban, String currency, BigDecimal value, String bic, String priority) {
        JsonObject json = new JsonObject();
        JsonObject account = new JsonObject();
        JsonObject amount = new JsonObject();

        if (StringUtils.isBlank(accountId)) {
            account.addProperty("iban", iban);
        } else {
            account.addProperty("accountIdentification", accountId);
        }

        amount.addProperty("currency", currency);
        amount.addProperty("value", value);

        json.add("account", account);
        json.add("amount", amount);
        json.addProperty("creditorBic", bic);
        json.addProperty("instructionPriority", priority);
        return json.toString();
    }
}

