import { apiGet, apiGetBlob, apiPost } from '@/lib/http'
import type {
  AdminPagination,
  AdminTaskActionResponse,
  AdminTaskDetail,
  AdminTaskListItem,
} from '@/types/admin'

export function fetchAdminTasks(params?: Record<string, unknown>) {
  return apiGet<{ items: AdminTaskListItem[]; pagination: AdminPagination }>(
    '/api/v1/admin/tasks',
    params,
  )
}

export function fetchAdminTaskDetail(taskId: number | string) {
  return apiGet<AdminTaskDetail>(`/api/v1/admin/tasks/${taskId}`)
}

export function fetchAdminTaskScreenshot(taskId: number | string) {
  return apiGetBlob(`/api/v1/admin/tasks/${taskId}/screenshot`)
}

export function retryAdminTask(taskId: number | string) {
  return apiPost<AdminTaskActionResponse>(`/api/v1/admin/tasks/${taskId}/retry`, {})
}

export function releaseAdminTaskInventory(taskId: number | string) {
  return apiPost<AdminTaskActionResponse>(`/api/v1/admin/tasks/${taskId}/release-inventory`, {})
}

export function releaseAdminTaskInventoryBinding(
  taskId: number | string,
  bindingId: number | string,
) {
  return apiPost<AdminTaskActionResponse>(
    `/api/v1/admin/tasks/${taskId}/inventory-bindings/${bindingId}/release`,
    {},
  )
}

export function regenerateAdminTaskClaimLink(taskId: number | string) {
  return apiPost<AdminTaskActionResponse>(`/api/v1/admin/tasks/${taskId}/regenerate-claim-link`, {})
}

export function confirmAdminTaskAssistedRole(taskId: number | string) {
  return apiPost<AdminTaskActionResponse>(`/api/v1/admin/tasks/${taskId}/support-confirm-role`, {})
}

export function redeemAdminTaskAssisted(taskId: number | string) {
  return apiPost<AdminTaskActionResponse>(`/api/v1/admin/tasks/${taskId}/support-redeem`, {})
}

export function closeAdminTask(taskId: number | string) {
  return apiPost<AdminTaskActionResponse>(`/api/v1/admin/tasks/${taskId}/close`, {})
}

export function markAdminTaskManualReview(taskId: number | string) {
  return apiPost<AdminTaskActionResponse>(`/api/v1/admin/tasks/${taskId}/mark-manual-review`, {})
}

export function completeAdminTaskManualDispatch(
  taskId: number | string,
  payload: {
    outcome: 'delivered' | 'failed'
    resultMessage?: string
    deliveryReference?: string
    deliveredCredential?: string
  },
) {
  return apiPost<AdminTaskActionResponse>(
    `/api/v1/admin/tasks/${taskId}/complete-manual-dispatch`,
    payload,
  )
}

export function prepareAdminTaskKuaishouCloudFulfillment(taskId: number | string) {
  return apiPost<AdminTaskActionResponse>(
    `/api/v1/admin/tasks/${taskId}/kuaishou-cloud/prepare`,
    {},
  )
}

export function refreshAdminTaskKuaishouCloudRoleInfo(taskId: number | string) {
  return apiPost<AdminTaskActionResponse>(
    `/api/v1/admin/tasks/${taskId}/kuaishou-cloud/refresh-role-info`,
    {},
  )
}

export function dispatchAdminTaskKuaishouCloudFulfillment(
  taskId: number | string,
  payload: {
    ticketCode?: string
  } = {},
) {
  return apiPost<AdminTaskActionResponse>(
    `/api/v1/admin/tasks/${taskId}/kuaishou-cloud/dispatch`,
    payload,
  )
}

export function returnNumberAdminTaskKuaishouCloudFulfillment(taskId: number | string) {
  return apiPost<AdminTaskActionResponse>(
    `/api/v1/admin/tasks/${taskId}/kuaishou-cloud/return-number`,
    {},
  )
}
