import { ApiVoidResult } from "@core/Models/Api.VoidResult.type";
import { SetNotificationAutoParams } from "@core/Models/Company.model";
import { BotEventParams, BotView, IWapServer } from "@models/Wap.model";
import ApiRequest from "@services/Api.Service";
import * as Yup from "yup";

const schema = Yup.object().shape({
    companyId: Yup.string().required("No se ha especificado una organización."),
    sessionUser: Yup.string().required(
        "No se ha podido determinar el usuario que intenta realizar esta acción"
    ),
});

interface ValidateErrorResult {
    status: boolean;
    error?: Yup.ValidationError;
}

const validateData = (data: BotEventParams): ValidateErrorResult => {
    let yupErrors: Yup.ValidationError = new Yup.ValidationError("");
    try {
        schema.validate(data, { abortEarly: true });
        return {
            status: true,
        };
    } catch (error) {
        if (error instanceof Yup.ValidationError) {
            yupErrors = error as Yup.ValidationError;
        }
        return {
            status: false,
            error: yupErrors,
        };
    }
};

export const setAutomaticNotificationsStatus = async (
    data: SetNotificationAutoParams
): Promise<ApiVoidResult> => {
    const validation = validateData(data);

    if (validation.status == false) {
        throw new Error(validation.error?.message);
    }

    return ApiRequest.post<ApiVoidResult>("companies/set-notifications-status", data);
};

export const wapServerView = async (data: BotEventParams): Promise<BotView> => {
    const validation = validateData(data);

    if (validation.status == false) {
        throw new Error(validation.error?.message);
    }

    return ApiRequest.post<BotView>("views/wap-server-view", data);
};

export const wapServerQuit = async (data: BotEventParams): Promise<ApiVoidResult> => {
    const validation = validateData(data);

    if (validation.status == false) {
        throw new Error(validation.error?.message);
    }

    return ApiRequest.post<ApiVoidResult>("wapserver/quit", data);
};

export const wapServerAssign = async (data: BotEventParams): Promise<IWapServer> => {
    const validation = validateData(data);

    if (validation.status == false) {
        throw new Error(validation.error?.message);
    }

    return ApiRequest.post<IWapServer>("wapserver/assign", data);
};

export const wapServerBotCreate = async (data: BotEventParams): Promise<ApiVoidResult> => {
    const validation = validateData(data);

    if (validation.status == false) {
        throw new Error(validation.error?.message);
    }

    return ApiRequest.post<ApiVoidResult>("wapserver/create", data);
};

export const wapServerBotStop = async (data: BotEventParams): Promise<ApiVoidResult> => {
    const validation = validateData(data);

    if (validation.status == false) {
        throw new Error(validation.error?.message);
    }

    return ApiRequest.post<ApiVoidResult>("wapserver/stop", data);
};

export const wapServerBotStart = async (data: BotEventParams): Promise<ApiVoidResult> => {
    const validation = validateData(data);

    if (validation.status == false) {
        throw new Error(validation.error?.message);
    }

    return ApiRequest.post<ApiVoidResult>("wapserver/start", data);
};

export const wapServerBotDelete = async (data: BotEventParams): Promise<ApiVoidResult> => {
    const validation = validateData(data);

    if (validation.status == false) {
        throw new Error(validation.error?.message);
    }

    return ApiRequest.post<ApiVoidResult>("wapserver/delete", data);
};

export const wapServerBotGetQR = async (data: BotEventParams): Promise<string> => {
    const validation = validateData(data);

    if (validation.status == false) {
        throw new Error(validation.error?.message);
    }

    return ApiRequest.post<string>("wapserver/qr", data);
};
