import api from '../utils/request'

export interface User {
  id: number
  mobile: string
  role: string
  status: string
  name?: string
  english_name?: string
  organization_id?: number | null
  organization_name?: string | null
}

export const searchUsers = (keyword: string) => {
  return api.get<User[]>('/users/search', { params: { q: keyword || undefined, limit: 50 } })
}

export const getUserById = (id: number) => {
  return api.get<User>(`/users/${id}`)
}

export interface UserListResponse {
  total: number
  items: User[]
}

export const getUsers = (params: any) => {
  return api.get<UserListResponse>('/users/', { params })
}

export interface DeleteUserRequest {
  password: string
  sms_code: string
}

/** 软删除普通用户：需操作者密码 + 发至本人手机的短信验证码 */
export const deleteUserWithVerification = (userId: number, data: DeleteUserRequest) => {
  return api.post<User>(`/users/${userId}/delete`, data)
}

export interface BatchSetOrganizationRequest {
  user_ids: number[]
  organization_id?: number | null
  admin_password: string
}

export const batchSetOrganization = (data: BatchSetOrganizationRequest) => {
  return api.post<{ success: boolean; count: number }>('/users/batch/organization', data)
}

