export default function useUser() {
  function inviteUser({
    email = "",
    app = "",
    role = "",
    name = "",
    org = "",
    site="",
    siteName="",
  } = {}) {
    return useNuxtApp().$api<Record<string, any>>("/api/auth/invite", {
      method: "POST",
      body: { email, app, role, name, org, ...(site && {siteId: site}), ...(siteName && {siteName}) },
    });
  }

  function inviteUserClient({
    email = '',
    app = [],
    role = '',
    name = '',
    org = '',
    site = '',
    siteName = ''
  }: {
    email?: string
    app?: string[]
    role?: string
    name?: string
    org?: string
    site?: string
    siteName?: string
  } = {}) {
    return useNuxtApp().$api<Record<string, any>>('/api/auth/invite/user', {
      method: 'POST',
      body: {
        email,
        app,
        role,
        name,
        org,
        ...(site && { siteId: site }),
        ...(siteName && { siteName })
      }
    })
  }

  function inviteMemberClient({
    email = "",
    app = "",
    role = "",
    name = "",
    org = "",
    site = "",
    siteName = "",
  } = {}) {
    return useNuxtApp().$api<Record<string, any>>("/api/auth/invite/member", {
      method: "POST",
      body: {
        email,
        app,
        role,
        name,
        org,
        ...(site && { siteId: site }),
        ...(siteName && { siteName }),
      },
    });
  }


  function updateName(name = "") {
    return useNuxtApp().$api<Record<string, any>>("/api/users/name", {
      method: "PUT",
      body: { name },
    });
  }

  function updateBirthday(id: string, { month = "", day = 0, year = 0 } = {}) {
    return useNuxtApp().$api<Record<string, any>>(`/api/users/birthday/${id}`, {
      method: "PATCH",
      body: { month, day, year },
    });
  }

  function updateGender(gender = "") {
    return useNuxtApp().$api<Record<string, any>>("/api/users/gender", {
      method: "PUT",
      body: { gender },
    });
  }

  function updateEmail(email = "") {
    return useNuxtApp().$api<Record<string, any>>("/api/users/email", {
      method: "PUT",
      body: { email },
    });
  }

  function updateContact(contact = "") {
    return useNuxtApp().$api<Record<string, any>>("/api/users/contact", {
      method: "PUT",
      body: { contact },
    });
  }

  function updatePassword(
    id: string,
    { currentPassword = "", newPassword = "", passwordConfirmation = "" }
  ) {
    return useNuxtApp().$api<Record<string, any>>(`/api/users/password/${id}`, {
      method: "PATCH",
      body: { currentPassword, newPassword, passwordConfirmation },
    });
  }

  function updateUserFieldById(id = "", field = "", value = "") {
    return useNuxtApp().$api<Record<string, any>>(`/api/users/field/${id}`, {
      method: "PATCH",
      body: { field, value },
    });
  }

  function getUsers({
    status = "active",
    type = "",
    search = "",
    page = 1,
  } = {}) {
    return useNuxtApp().$api<Record<string, any>>("/api/users", {
      method: "GET",
      query: { status, search, page, type },
    });
  }

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

  function createUserByInvite({
    firstName = "",
    lastName = "",
    password = "",
    id = "",
  } = {}) {
    return useNuxtApp().$api<Record<string, any>>(`/api/users/invite/${id}`, {
      method: "POST",
      body: { firstName, lastName, password },
    });
  }

  function createUserByVerification({
    name = "",
    password = "",
    id = "",
    type = "",
  } = {}) {
    return useNuxtApp().$api<Record<string, any>>(`/api/users/invite/${id}`, {
      method: "POST",
      body: { name, password, type },
    });
  }

  function getUserByEmail(email = "") {
    return useNuxtApp().$api<Record<string, any>>(`/api/users/email/${email}`, {
      method: "GET"
    });
  }
  function getUserByEmailV2(email = "") {
    return useNuxtApp().$api<Record<string, any>>(`/api/users/user/${email}`, {
      method: "GET"
    });
  }

  return {
    inviteUser,
    updateName,
    updateBirthday,
    updateGender,
    updateEmail,
    updateContact,
    updatePassword,
    updateUserFieldById,
    getUsers,
    createUserByInvite,
    getById,
    createUserByVerification,
    getUserByEmail,
    getUserByEmailV2,
    inviteUserClient,
    inviteMemberClient
  };
}
