package cz.moneta.test.dsl.util.task.monetaportal;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.util.data.client.NonClient;
import cz.moneta.test.dsl.util.task.monetaportal.responses.GetContactsResponse;
import cz.moneta.test.dsl.util.task.monetaportal.responses.GetOpportunitiesResponse;

public class Wso2Tasks {
    private final Harness harness;

    public Wso2Tasks(Harness harness) {
        this.harness = harness;
    }
    public GetOpportunitiesResponse getOpportunitiesForClient(String date, NonClient client, String branchId) {
        return harness
                .tasks()
                .wso2()
                .restRequest()
                .prepareEmptyRequestFromHub("/t/sme.cluster/IPE/GET_OPPORTUNITIES/001")
                .addField("SOURCE", "2")
                .addField("ID_BRANCH", branchId) //branchId for CESKE_BUDEJOVICE
                .addField("ICO", client.ico())
                .addField("CREATED_AT_FROM", date)
                .post()
                .andAssertStatus(200)
                .andAssertPresent("RS_DATA")
                .mapResponseIntoObject(GetOpportunitiesResponse.class);
    }

    public GetContactsResponse getContacts(NonClient client) {
        String instPtKey = harness
                .tasks()
                .wso2()
                .restRequest()
                .prepareEmptyRequestFromUfo("/t/data.cluster/GET_INST_PT_KEY/001")
                .addField("SRC_SYS_ID", "SFC01")
                .addField("ICO_NUM_IN", client.ico())
                .post().extract("RS_1").findValuesAsText("INST_PT_KEY").get(0);

        String subjectId = harness
                .tasks()
                .wso2()
                .restRequest()
                .prepareEmptyRequestFromUfo("/t/sme.cluster/GET_SUBJECTS/001")
                .addField("SESSION_ID", "")
                .addField("ID_SUBJECT_EXT", instPtKey)
                .post().extract("RS_DATA").findValuesAsText("ID_SUBJECT").get(0);

        return harness
                .tasks()
                .wso2()
                .restRequest()
                .prepareEmptyRequestFromUfo("/t/sme.cluster/GET_CONTACTS/001")
                .addField("SESSION_ID", "")
                .addField("ID_SUBJECT", subjectId)
                .post().mapResponseIntoObject(GetContactsResponse.class);
    }

    public int getCampaignsCount(Harness harness, String telephone, String campaignName) {
        return harness
                .tasks()
                .wso2()
                .restRequest()
                .prepareEmptyRequestFromUfo("/t/callcentrum.cluster/GET_TLS_CAMPAIGNS_BYPHONE_W/001")
                .addField("TELEFON", telephone)
                .post()
                .extract("RS_DATA")
                .findValuesAsText("NAZEV")
                .stream()
                .filter(x -> x.contains(campaignName))
                .toList()
                .size();
    }
}
