import { apiGet, apiPost } from '@/lib/http'
import type {
  AdminNinetyoneOrderActionResult,
  AdminNinetyoneOrderListResult,
} from '@/types/admin'

export function fetchAdminNinetyoneOrders(
  params: {
    page?: number
    pageSize?: number
    status?: string
  } = {},
) {
  const searchParams = new URLSearchParams()
  if (params.page) {
    searchParams.set('page', String(params.page))
  }
  if (params.pageSize) {
    searchParams.set('pageSize', String(params.pageSize))
  }
  if (params.status) {
    searchParams.set('status', params.status)
  }
  const queryString = searchParams.toString()
  return apiGet<AdminNinetyoneOrderListResult>(
    `/api/v1/admin/platform-config/ninetyone/orders${queryString ? `?${queryString}` : ''}`,
  )
}

export function retryAdminNinetyoneOrder(orderId: number | string) {
  return apiPost<AdminNinetyoneOrderActionResult>(
    `/api/v1/admin/platform-config/ninetyone/orders/${orderId}/retry`,
    {},
  )
}

export function failAdminNinetyoneOrder(orderId: number | string, payload: { reason?: string }) {
  return apiPost<AdminNinetyoneOrderActionResult>(
    `/api/v1/admin/platform-config/ninetyone/orders/${orderId}/fail`,
    payload,
  )
}
