package cz.moneta.test.regression.commercial.smallbusiness.digital.backendtests;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.wso2.objects.smallbusiness.*;
import cz.moneta.test.harness.annotations.JiraTestCase;
import cz.moneta.test.harness.annotations.TestCase;
import cz.moneta.test.harness.annotations.TestScenario;

import static cz.moneta.test.harness.annotations.Environment.TST1;
import static org.junit.jupiter.api.Assertions.assertNotNull;

@TestScenario(name = "Current account opening FOP")
public class CurrentAccountTest {

    private static final String CIF = "9070123752";

    @TestCase(name = "Current account opening FOP", environments = {TST1})
    @JiraTestCase(id = "SBD-T1201")
    public void currentAccount_opening(Harness harness) {
        //Get NACE for customer
        String nace = getNaceFromCif(harness, CIF);

        //Get SME deposit offer for given NACE
        getDepositOfferNyNace(harness, nace);

        //Load individual IR plan
        getDepositIndividualRatePlan(harness);

        //Open current account
        String currentAccountNumber = openCurrentAccount(harness, CIF);

        //Get info for created CA
        getSelectedCurrentAccountInfo(harness, currentAccountNumber);

        harness.log("Current account flow finished successfully. CA number=", currentAccountNumber);

        //Cleanup
        cancelCurrentAccount(harness, currentAccountNumber);
    }

    private String getNaceFromCif(Harness harness, String CIF) {
        GetCustCommercialByCifResponse response = harness.tasks()
                .wso2()
                .restRequest()
                .prepareRequestFromUfo("/t/customer.cluster/GET_CUST_COMMERCIAL_BY_CIF/001")
                .withPojoPayload(GetCustCommercialByCifRequest.builder()
                        .cif(CIF)
                        .znovuNacistUpominky(0)
                        .build())
                .post()
                .andAssertStatus(200)
                .mapResponseIntoObject(GetCustCommercialByCifResponse.class);

        assertNotNull(response, "GET_CUST_COMMERCIAL_BY_CIF response must not be null");
        String nace = response.getOkec();
        assertNotNull(nace, "NACE must not be null");
        return nace;
    }


    private void getDepositOfferNyNace(Harness harness, String nace) {
        harness.tasks().wso2().restRequest()
                .prepareRequestFromUfo("/t/small.business/IPE/GET_SELECTED_SME_DEPOSIT_OFFER/001")
                .withPojoPayload(GetSelectedSmeDepositOfferRequest.builder()
                        .channelId("POS")
                        .nace(nace)
                        .residence("1")
                        .productClass("12")
                        .legalFormId("101")
                        .clientType("2")
                        .build())
                .post()
                .andAssertStatus(200)
                .mapResponseIntoObject(GetSelectedSmeDepositOfferResponse.class);
    }

    private void getDepositIndividualRatePlan(Harness harness) {
        harness.tasks().wso2().restRequest()
                .prepareRequestFromUfo("/t/small.business/NAS/GET_SME_DEPOSIT_INDIVIDUAL_IR_PLAN/001")
                .withPojoPayload(GetSmeDepositIndividualIrPlanRequest.builder()
                        .cif(CIF)
                        .prod_code("1202013")
                        .channel_id("POS")
                        .prod_class("12")
                        .build())
                .post()
                .andAssertStatus(200)
                .mapResponseIntoObject(GetSmeDepositIndividualIrPlanResponse.class);
    }

    private String openCurrentAccount(Harness harness, String CIF) {
        CrtAccountIcbResponse response = harness.tasks().wso2().restRequest()
                .prepareRequestFromUfo("/t/deposit.cluster/IPE/CRT_ACCOUNT_ICB/001")
                .withPojoPayload(CrtAccountIcbRequest.builder()
                        .obchodniJmeno("Dagmar Dokoupilová")
                        .produktKod("1202013")
                        .primarniUrednik("GEBTDEA")
                        .obchodniMistoAlternativni("86001340")
                        .postovniPoukazka("0")
                        .pouzitaAA("1")
                        .nuloveUroceni("1")
                        .cif(CIF)
                        .distribuceVypisu("1")
                        .periodaVypisu("2")
                        .poplatkovyPlanMatersky("J")
                        .aaZeme("52")
                        .obchodniMistoKod("86001340")
                        .datumOtevreni("2026-01-27")
                        .vypisNaKonciMesice(true)
                        .build())
                .post()
                .andAssertStatus(200)
                .mapResponseIntoObject(CrtAccountIcbResponse.class);

        assertNotNull(response, "CRT_ACCOUNT_ICB response must not be null");
        String currentAccountNumber = response.getCisloUctu();
        assertNotNull(currentAccountNumber, "Current account number must not be null");
        return currentAccountNumber;
    }

    private void getSelectedCurrentAccountInfo(Harness harness, String currentAccountNumber) {
        harness.tasks().wso2().restRequest()
                .prepareRequestFromUfo("/t/digitalchannels.ib/IPE/GET_SELECTED_BU_INFO/001")
                .withPojoPayload(GetSelectedBuInfoRequest.builder()
                        .cisloUctu(currentAccountNumber)
                        .build())
                .post()
                .andAssertStatus(200)
                .mapResponseIntoObject(GetSelectedBuInfoResponse.class);
    }

    private void cancelCurrentAccount(Harness harness, String currentAccountNumber) {
        harness.tasks().wso2().restRequest()
                .prepareRequestFromUfo("/t/payments.cluster/IPE/DO_CANCEL_CURRENT_ACCOUNT/001/")
                .withPojoPayload(DoCancelCurrentAccountRequest.builder()
                        .cisloUctu(currentAccountNumber)
                        .cisloUctuPrijemceKodBanky("0100")
                        .branchId("86001340")
                        .cisloCostCentra(3134L)
                        .produktKod("1202013")
                        .cisloUctuPrijemceVS("3004464615")
                        .cisluUctuPrijemceInterni("301030")
                        .sdeleniID(10L)
                        .sso("543831687")
                        .poplatekZaZruseni((double) 0)
                        .sdeleniText("Klient: Nechce uvést")
                        .channelId("UFO")
                        .build())
                .post()
                .andAssertStatus(200);
    }
}