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

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.util.DateUtils;
import cz.moneta.test.harness.endpoints.ods.OdsEndpoint;

import java.util.Calendar;

public class SetInsuranceStatusTask {
    private final Harness harness;

    public SetInsuranceStatusTask(Harness harness) {
        this.harness = harness;
    }

    private String findOdsKeyActiveInsurance(String birthNumber) {
        return harness.getEndpoint(OdsEndpoint.class)
                .executeSql("select distinct prod_inst.prod_inst_key\n" +
                        "from ODS_L1_OWNER.INST_PT i\n" +
                        "inner join ODS_L1_OWNER.PROD_INST_PT_ROLE prod_inst_pt_role\n" +
                        "on prod_inst_pt_role.inst_pt_key = i.inst_pt_key\n" +
                        "and prod_inst_pt_role.src_sys_id = 'ICB31'\n" +
                        "and prod_inst_pt_role.pt_role_tp_key = 4\n" +
                        "inner join ODS_L1_OWNER.Prod_Inst prod_inst on\n" +
                        "prod_inst.prod_inst_key = prod_inst_pt_role.prod_inst_key\n" +
                        "inner join ODS_L1_OWNER.addt_srvc_inst on\n" +
                        "prod_inst.prod_inst_key = addt_srvc_inst.prod_inst_key\n" +
                        "where i.rc_num = '" + birthNumber + "'\n" +
                        "and addt_srvc_inst.cancel_date is null\n")
                .stream()
                .map(s -> s.getValue(0).toString())
                .findFirst()
                .orElse(null);
    }

    private String setYearOldDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.YEAR, -1);
        return DateUtils.getDateWithFormat(calendar.getTime(), "yyyy-MM-dd");
    }

    private void callApiCancelInsurance(String prodInstKey) {
        String tokenUfo = harness.tasks().wso2().authorization().getToken("UFO");
        String oneYearOldDate = setYearOldDate();

        //920181021 is SSO GEBTHA7
        harness.withWso2()
                .prepareRequest()
                .withPath("/t/insurance.cluster/SET_INSUR_DATA/001")
                .withHeader("Authorization", tokenUfo)
                .withHeader("accept", "application/json")
                .withHeader("activityId", harness.tasks().wso2().restRequest().getRandomId())
                .withHeader("requestId", harness.tasks().wso2().restRequest().getRandomId())
                .withHeader("userid_l2", "GEBTHA7")
                .withPayload("{\n" +
                        "  \"USER_SSO\": \"920181021\",\n" +
                        "  \"ACC_NUM_IN\": \"\",\n" +
                        "  \"PROD_INST_KEY_IN\": " + prodInstKey + ",\n" +
                        "  \"INSUR_ID\": \"\",\n" +
                        "  \"START_DATE\": \"\",\n" +
                        "  \"END_DATE\": \"" + oneYearOldDate + "\",\n" +
                        "  \"CANCEL_DATE\": \"" + oneYearOldDate + "\",\n" +
                        "  \"CANCEL_RSN_TP_ID\": \"85\",\n" +
                        "  \"POS_ID\": \"\",\n" +
                        "  \"DIST_CNL_ID\": \"CALLCENTER\",\n" +
                        "  \"PRICE_TOTAL_AMT\": null,\n" +
                        "  \"DEL_FLAG\": null,\n" +
                        "  \"INSUR_INST_PT_KEY\": null,\n" +
                        "  \"INSUR_SRC_SYS_ID\": \"\",\n" +
                        "  \"PT_PT_REL_TP_ID\": \"\",\n" +
                        "  \"ASSIGNEE_INST_PT_KEY\": null,\n" +
                        "  \"ASSIGNEE_PT_PT_REL_TP_ID\": \"\"\n" +
                        "}")
                .post()
                .andAssertStatus(200);
    }

    public void cancelInsurance(String birthNumber) {
        String odsKeyActiveInsurance = findOdsKeyActiveInsurance(birthNumber);
        if (odsKeyActiveInsurance == null) {
            harness.log("The user with birth number " + birthNumber + " doesn't have active insurance");
            return;
        }

        callApiCancelInsurance(odsKeyActiveInsurance);
        harness.log("User with birth number " + birthNumber + " - insurance canceled");
    }
}