export type TFormEntryFields = Record<string | number, string | number | boolean | null>;

export type TFormEntryCreate = {
  _id?: string;
  formType: string;
  block?: string;
  level?: string;
  unit?: string;
  name?: string;
  phoneNumber?: string;
  fields: TFormEntryFields;
  status?: string;
  org?: string;
  site?: string;
  createdAt?: string;
  updatedAt?: string | null;
  deletedAt?: string | null;
};

export type TFormEntryUpdate = {
  _id?: string;
  formType: string;
  fields: Record<string, string | number | boolean | null>;
  status?: string;
  updatedAt?: string | null;
  deletedAt?: string | null;
};

export default function useOnlineFormConfiguration() {
  function getAll({
    page = 1,
    search = "",
    limit = 10,
    status = "",
    org = "",
    site = "",
  } = {}) {
    return useNuxtApp().$api<Record<string, any>>("/api/online-form-configurations", {
      method: "GET",
      query: { page, search, limit, status, org, site },
    });
  }

  function getById(id: string) {
    return useNuxtApp().$api<Record<string, any>>(`/api/online-form-configurations/id/${id}`, {
      method: "GET",
    });
  }

  function create(value: TFormEntryCreate) {
    return useNuxtApp().$api<Record<string, any>>("/api/online-form-configurations", {
      method: "POST",
      body: value,
    });
  }

  function updateById(id: string, value: TFormEntryUpdate) {
    return useNuxtApp().$api<Record<string, any>>(`/api/online-form-configurations/id/${id}`, {
      method: "PUT",
      body: value,
    });
  }

  function deleteById(id: string) {
    return useNuxtApp().$api<Record<string, any>>(`/api/online-form-configurations/id/${id}`, {
      method: "PATCH",
    });
  }

  return { getAll, getById, create, updateById, deleteById };
}
