import { apiClient } from '@/shared/api/client'

import type { ApiResponse } from '@/shared/types/types'

export interface SupportGroupMember {
  id: number
  username: string
  nickname: string
  support_status: string
}

export interface SupportGroup {
  id: number
  code: string
  name: string
  description: string
  status: string
  sort_order: number
  members: SupportGroupMember[]
  created_at: string
  updated_at: string
}

export interface SaveSupportGroupRequest {
  code?: string
  name: string
  description?: string
  status?: string
  sort_order?: number
  member_ids: number[]
}

export async function fetchSupportGroups() {
  const { data } = await apiClient.get<ApiResponse<SupportGroup[]>>('/admin/chat-support-groups')
  return data.data
}

export async function fetchSupportGroupAdmins() {
  const { data } = await apiClient.get<ApiResponse<SupportGroupMember[]>>(
    '/admin/chat-support-groups/support-admins'
  )
  return data.data
}

export async function createSupportGroup(req: SaveSupportGroupRequest) {
  const { data } = await apiClient.post<ApiResponse<SupportGroup>>(
    '/admin/chat-support-groups',
    req
  )
  return data.data
}

export async function updateSupportGroup(id: number, req: SaveSupportGroupRequest) {
  const { data } = await apiClient.put<ApiResponse<SupportGroup>>(
    `/admin/chat-support-groups/${id}`,
    req
  )
  return data.data
}

export async function deleteSupportGroup(id: number) {
  const { data } = await apiClient.delete<ApiResponse<{ deleted: boolean }>>(
    `/admin/chat-support-groups/${id}`
  )
  return data.data
}
