import { request, ResponseProps } from '../request';
import { FieldProps, SingleForm, FormDetails } from '../components/formModeler/FormTypes';
import { ID_ENDPOINT, FORM_ENDPOINT, FORM_DEPLOY } from '../constants/endpoints';

export interface FormProps {
    id: number;
    name: string;
    components?: FieldProps;
    version: number;
}

interface Id {
    id: number;
}

export const CORE_API_ENDPOINT = `${process.env.REACT_APP_API_GATEWAY_URL}${ID_ENDPOINT}`;
export const FORM_API_ENDPOINT = `${process.env.REACT_APP_API_GATEWAY_URL}${FORM_ENDPOINT}`;
export const FORM_DEPLOY_API_ENDPOINT = `${process.env.REACT_APP_WORKFLOW_ENGINE_URL}${FORM_DEPLOY}`;

export const generateUniqueID = async (): Promise<{
    success: boolean;
    data?: Id;
    message?: string;
}> => {
    const r: ResponseProps = (await request.get(CORE_API_ENDPOINT)) as ResponseProps;
    if (r.success) {
        const id = r.data as Id;
        return { success: r.success, data: id, message: r.message };
    }
    return { success: false };
};

export const getAllForms = async (): Promise<{
    success: boolean;
    data?: FormProps[];
}> => {
    const r: ResponseProps = (await request.get(`${FORM_API_ENDPOINT}?include-form-content=false`)) as ResponseProps;

    if (r.success) {
        return { success: r.success, data: r.data as FormProps[] };
    }

    return { success: false };
};

export const searchForms = async (
    searchTerm: string,
): Promise<{ success: boolean; data?: FormProps[]; message?: string }> => {
    const r: ResponseProps = (await request.get(
        `${FORM_API_ENDPOINT}/search?idOrNameLike=${searchTerm}`,
    )) as ResponseProps;

    if (r.success) {
        return {
            success: r.success,
            data: r.data as FormProps[],
            message: r.message,
        };
    }

    return { success: false };
};

export const fetchFormDetails = async (
    id: number,
): Promise<{ success: boolean; data?: SingleForm; message?: string }> => {
    const r: ResponseProps = (await request.get(`${FORM_API_ENDPOINT}/${id}`)) as ResponseProps;

    if (r.success) {
        const form = r.data as SingleForm;
        return { success: r.success, data: form, message: r.message };
    }

    return { success: false };
};

export const saveForm = async (formDetails): Promise<{ success: boolean; message?: string }> => {
    const r: ResponseProps = (await request.post(FORM_API_ENDPOINT, formDetails)) as ResponseProps;

    if (r.success) {
        return { success: r.success, message: r.message };
    }

    return { success: false };
};

export const deployForm = async (formDetails: FormDetails): Promise<{ success: boolean; message?: string }> => {
    const apiData = {
        formName: formDetails.name,
        formKey: formDetails.id,
        formContent: formDetails.components,
        formVersion: formDetails.version,
        tenantId: 1,
    };
    const r: ResponseProps = (await request.post(FORM_DEPLOY_API_ENDPOINT, apiData)) as ResponseProps;

    if (r.success) {
        return { success: r.success, message: r.message };
    }

    return { success: false };
};

export const deleteForm = async (id: number): Promise<{ success: boolean; message?: string }> => {
    const r: ResponseProps = (await request.delete(`${FORM_API_ENDPOINT}/${id}`)) as ResponseProps;

    if (r.success) {
        return { success: r.success, message: r.message };
    }

    return { success: false };
};
