import { apiRequest } from '../lib/api'
import type { RolesListResponse } from '../types/user'

export interface AddTeamMemberPayload {
  cellNumber: string
  firstName: string
  lastName: string
  roleSlug: string
  email?: string
  password?: string
}

export async function listTeamRoles(signal?: AbortSignal) {
  return apiRequest<RolesListResponse>('/roles?scope=team', { auth: true, signal })
}

export async function addTeamMember(businessId: number | string, payload: AddTeamMemberPayload) {
  return apiRequest(`/businesses/${businessId}/team`, {
    method: 'POST',
    auth: true,
    body: payload,
  })
}

export async function updateTeamMemberRole(
  businessId: number | string,
  memberId: number | string,
  roleSlug: string,
) {
  return apiRequest(`/businesses/${businessId}/team/${memberId}`, {
    method: 'PATCH',
    auth: true,
    body: { roleSlug },
  })
}
