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

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 rebindAdminTaskKuaishouCloudRole(taskId: number | string) {
  return apiPost<AdminTaskActionResponse>(
    `/api/v1/admin/tasks/${taskId}/kuaishou-cloud/rebind-role`,
    {},
  )
}

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

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

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

export function consumeAdminTaskKuaishouIndustryVoucher(
  taskId: number | string,
  payload: {
    consumeType?: string
    storeName?: string
    storeAddress?: string
    expressCode?: string
    expressNo?: string
  } = {},
) {
  return apiPost<AdminTaskActionResponse>(
    `/api/v1/admin/tasks/${taskId}/kuaishou-industry/consume`,
    payload,
  )
}
