import { extractItems } from '~/utils/api'

export const createUserSiteRole = async (payload: {
  userId: number
  siteId: number
  role: string
}) => {
  const api = useApi()
  return api.post('/user_site_roles', {
    user: `/api/users/${payload.userId}`,
    site: `/api/sites/${payload.siteId}`,
    role: payload.role
  }, {
    toast: false
  })
}

export type UserSiteRole = {
  id: number
  user: { id: number }
  site: { id: number; name?: string }
  role: string
}

export const listUserSiteRoles = async () => {
  const api = useApi()
  const data = await api.get<UserSiteRole[] | { 'hydra:member'?: UserSiteRole[] }>(
    '/user_site_roles',
    {},
    { toast: false }
  )
  return extractItems<UserSiteRole>(data)
}

export const deleteUserSiteRole = async (id: number) => {
  const api = useApi()
  return api.delete(`/user_site_roles/${id}`, {}, { toast: false })
}
