export default function useMember() {
  const members = useState<Array<TMember>>("members", () => []);
  const page = useState("page", () => 1);
  const pages = useState("pages", () => 0);
  const pageRange = useState("pageRange", () => "-- - -- of --");

  const member = useState<TMember>("member", () => ({
    _id: "",
    org: "",
    orgName: "",
    roleName: "",
    name: "",
    user: "",
    role: "",
    type: "",
    customerOrgId: "",
    customerSiteId: "",
    status: "",
    createdAt: "",
    updatedAt: "",
    deletedAt: "",
  }));

  function getByUserId(user: string) {
    return useNuxtApp().$api<TMember>(`/api/members/user/${user}`);
  }

  function getAllByUserId(user: string) {
    return useNuxtApp().$api<TMember>(`/api/members/users/${user}`);
  }
  function getByMemberId(user: string) {
    return useNuxtApp().$api<TMember>(`/api//user/${user}`);
  }

  function getByUserIdType(user: string, type: string) {
    return useNuxtApp().$api<TMember>(`/api/members/user/${user}/app/${type}`);
  }

  function getByUserType(user: string, type: string, org?: string) {
    return useNuxtApp().$api<TMember>(`/api/members/user/${user}/app/${type}`, {
      method: "GET",
      query: { org },
    });
  }

  async function getAll({
    page = 1,
    search = "",
    limit = 10,
    user = "",
    org = "",
    type = "",
    status = "active",
    siteId = "",
  } = {}) {
    return useNuxtApp().$api<Record<string, any>>("/api/members", {
      method: "GET",
      query: { page, limit, search, user, org, type, status, siteId },
    });
  }
  function createUserByVerification(
    verificationId: string,
    payload: TMemberUserCreateVerification
  ) {
    return useNuxtApp().$api<TMemberUserCreateVerification>(
      `/api/users/invite/${verificationId}`,
      {
        method: "POST",
        body: payload,
      }
    );
  }

  function createMemberInvite(verificatonId: string) {
    return useNuxtApp().$api<TMember>(
      `/api/members/verification/${verificatonId}`,
      {
        method: "POST",
      }
    );
  }
  function updateMemberStatus(id: string, status: string) {
    return useNuxtApp().$api<Record<string, any>>(
      `/api/members/${id}/${status}`,
      {
        method: "PUT",
      }
    );
  }

  function updateMemberRole(
    id: string,
    role: string,
    type: string,
    org: string
  ) {
    return useNuxtApp().$api<Record<string, any>>(
      `/api/members/id/${id}/role/${role}/type/${type}/org/${org}`,
      {
        method: "PUT",
      }
    );
  }

  function createMemberDirect(payload: {
    userId: string;
    orgId: string;
    roleId: string;
    app: string;
    siteId?: string;
    siteName?: string;
  }) {
    return useNuxtApp().$api<TMember>("/api/members/direct", {
      method: "POST",
      body: payload,
    });
  }

  function updateMemberSite(id: string, siteId: string, siteName: string) {
    return useNuxtApp().$api(`/api/members/site`, {
      method: "PUT",
      body: {
        id,
        siteId,
        siteName,
      },
    });
  }

  function getByUserTypeOrg(user: string, type: string, org: string) {
    return useNuxtApp().$api<TMember>(
      `/api/members/user/${user}/app/${type}/org`,
      {
        method: "GET",
        query: { org },
      }
    );
  }

  return {
    members,
    member,
    page,
    pages,
    pageRange,
    getByUserType,
    getAll,
    getByUserId,
    createUserByVerification,
    createMemberInvite,
    getByUserIdType,
    getByMemberId,
    updateMemberStatus,
    updateMemberRole,
    createMemberDirect,
    updateMemberSite,
    getAllByUserId,
    getByUserTypeOrg,
  };
}
