package cz.moneta.test.regression.channels.digital.ib.newib.payment.fon;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.newib.tasks.NewIbTasks;
import cz.moneta.test.dsl.newib.util.PaymentFormData;
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.harness.support.auth.Credentials;
import org.junit.jupiter.api.*;

import java.text.DecimalFormat;
import java.util.concurrent.ThreadLocalRandom;

@TestScenarioWithOrder(name = "Other payments tests")
public class OtherPaymentsTest {
    Credentials clientCredentials;
    PaymentFormData formData;
    String generatedPayment;

    private String generateRandomPayment() {
        double doubleNumber = ThreadLocalRandom.current().nextDouble(10001.00, 10999.00);
        return new DecimalFormat("#.00").format(doubleNumber).replace(".", ",");
    }

    @BeforeAll
    public void setup(Harness harness) {
        String clientBirthNumber = harness.getConfig("nib.rc.payment-over-limit");
        String recipientAccountNumber = harness.getConfig("nib.recipientaccount.payments-one-account");
        generatedPayment = generateRandomPayment();
        harness.log("Generated payment is " + generatedPayment);

        clientCredentials = harness.tasks()
                .udebs()
                .findCredentialsByRC(clientBirthNumber);

        formData = PaymentFormData.builder()
                .recipientAccountNumber(recipientAccountNumber)
                .bankCode("0600")
                .recipientAccountNumberEur("DE89 3704 0044 0532 013000")
                .currency("CZK")
                .recipientName("Mirek Kereškéni")
                .fromAccountNumber("609-206971718/0600")
                .recipientAccountNumberCzk("3681-7128151/0600")
                .category("denní")
                .build();
    }

    //payment over week limit is in settings test SB-T9339
    @Order(1)
    @TestCase(name = "Payment over limit", environments = {Environment.TST1, Environment.PPE})
    @JiraTestCase(id = "SB-T9207")
    public void paymentOverLimit(Harness harness) {
        NewIbTasks newIbTasks = harness.tasks().newIb();
        formData.setAmount(generatedPayment);

        harness.withNewIb()
                .openLoginPage()
                .then(newIbTasks.loginWithUsernamePasswordAndDefaultMobKey(clientCredentials.getUsername(), clientCredentials.getPassword()))
                .then(newIbTasks.setPaymentOverLimit(formData));
    }

    @Order(2)
    @TestCase(name = "Download cash receipts", environments = {Environment.PPE})
    @JiraTestCase(id = "SB-T9211")
    public void downloadCashReceiptsFon(Harness harness) {
        NewIbTasks newIbTasks = harness.tasks().newIb();
        clientCredentials = harness.tasks()
                .udebs()
                .findCredentialsByRC("6306083531");

        harness.withNewIb()
                .openLoginPage()
                .then(newIbTasks.loginWithUsernamePasswordAndDefaultMobKey(clientCredentials.getUsername(), clientCredentials.getPassword()))
                .then(newIbTasks.downloadCashReceipts(formData));

        harness.withOpenApi()
                .prepareUnsecuredRequest()
                .withPath("/account/transaction/receipt")
                .withHeader("dispo-context-id", "10062336")
                .withPayload("{\"transactionId\":\"0206971718:20250917:00013:250917C01846101\"}")
                .post()
                .andAssertFieldValue("status", "OK")
                .saveBase64FieldAsFile("responseObject.base64PdfReceipt");
    }
}