import type { LegalRole } from '~/server/utils/permissions'

export function useOrganizationApi() {
  const { organization } = useAuth()

  async function createOrganization(name: string, slug: string, logo?: string) {
    return organization.create({ name, slug, logo })
  }

  async function deleteOrganization(organizationId: string) {
    return organization.delete({ organizationId })
  }

  async function getInvitation(invitationId: string) {
    return organization.getInvitation({ query: { id: invitationId } })
  }

  async function listInvitations(organizationId?: string) {
    return organization.listInvitations(organizationId ? { query: { organizationId: organizationId } } : undefined)
  }

  async function inviteMember(email: string, role: LegalRole) {
    return organization.inviteMember({ email, role })
  }

  async function acceptInvitation(invitationId: string) {
    return organization.acceptInvitation({ invitationId })
  }

  async function cancelSentInvitation(invitationId: string) {
    return organization.cancelInvitation({ invitationId })
  }

  async function rejectInvitation(invitationId: string) {
    return organization.rejectInvitation({ invitationId })
  }

  async function loadOrganizations() {
    return organization.list()
  }

  async function checkSlugAvailability(slug: string) {
    return organization.checkSlug({ slug })
  }

  async function setActiveOrganization(organizationId: string) {
    return organization.setActive({ organizationId })
  }

  return {
    createOrganization,
    deleteOrganization,
    getInvitation,
    listInvitations,
    inviteMember,
    acceptInvitation,
    cancelSentInvitation,
    rejectInvitation,
    loadOrganizations,
    checkSlugAvailability,
    setActiveOrganization
  }
}
