export interface AdminTaskListItem {
  taskId: number
  taskNo: string
  platformOrderId: string
  skuCode: string
  skuName: string
  status: string
  executorKey: string
  deliveryStatus: string
  resultCode: string
  resultMessage: string
  resourceStatus: string
  customerStatus: string
  loginType: string
  roleName: string
  roleId: string
  runtimeSessionId: string
  claimedAt: string | null
  roleConfirmedAt: string | null
  redeemedAt: string | null
  retryCount: number
  lastError: string
  createdAt: string
  updatedAt: string
  claimToken: string
  screenshotPath: string
}

export interface AdminTaskOperations {
  canRetry: boolean
  canRegenerateClaimLink: boolean
  canClose: boolean
  canMarkManualReview: boolean
  canCompleteManualDispatch: boolean
  canPrepareKuaishouCloudFulfillment: boolean
  canRefreshKuaishouCloudRoleInfo: boolean
  canRebindKuaishouCloudRole: boolean
  canDispatchKuaishouCloudFulfillment: boolean
  canReturnKuaishouCloudFulfillment: boolean
  canResendKuaishouIndustryVoucherCode?: boolean
  canConsumeKuaishouIndustryVoucher?: boolean
  canViewSensitiveTaskData: boolean
}

export interface AdminTaskActionResponse {
  task: {
    taskId: number
    taskNo: string
    status: string
    deliveryStatus: string
    resultCode: string
    resultMessage: string
    primaryClaimTokenId: number | null
    lastError: string
    updatedAt: string
  }
  claimUrl?: string
  token?: string
}

/** 与后端 ClaimIdentityAdminSummary / AdminClaimIdentitySummary 对齐 */
export interface AdminClaimIdentitySummary {
  expectedUid: string
  submittedAt: string | null
  source: string
  ready: boolean
  boundUid: string
  boundRoleName: string
  uidMatched: boolean | null
  compatibilityMode: 'uid' | 'legacy_no_uid'
  note: string
  /** shipped=发货快照；live=当前绑定 */
  displaySource?: 'shipped' | 'live'
  shippedUid?: string
  shippedRoleName?: string
  shippedAt?: string | null
}

export interface AdminTaskDetail {
  task: AdminTaskListItem
  order: null | {
    orderId: number
    provider: string
    platform: string
    shopId: string
    shopName: string
    platformOrderId: string
    payStatus: string
    orderStatus: string
  }
  orderItem: null | {
    orderItemId: number
    skuCode: string
    skuName: string
    quantity: number
  }
  claimToken: null | {
    primaryClaimTokenId: number
    token: string
    status: string
    expiredAt: string
    claimUrl: string
  }
  artifacts: Record<string, unknown>
  screenshotUrl: string
  review: {
    required: boolean
    screenshotCapturedAt: string | null
    roleId: string
    roleName: string
  }
  redeemResolution: null | {
    status: string
    taskStatus: string
    replacementCount: number
    finishedAt: string | null
    attempts: Array<{
      attempt: number
      codeMasked: string
      credentialType: string
      outcome: string
      resultCode: string
      resultMessage: string
    }>
  }
  claimIdentity: AdminClaimIdentitySummary | null
  kuaishouCloudFulfillment: null | {
    flowType: string
    configId: string
    internalSkuCode: string
    internalSkuName: string
    ticket: {
      code: string
      capturedAt: string | null
      status: string
      verifiedAt: string | null
      goodsTitle: string
      leftCount: number
    }
    binding: {
      prepareStatus: string
      cloudSourceKeys: string[]
      cloudSourceLabels: string[]
      resolvedSourceKey: string
      resolvedSourceLabel: string
      skuId: number
      skuName: string
      vnKey: string
      vnId: number
      vnPhone: string
      bindUrl: string
      bindPreparedAt: string | null
      bindExpiresAt: string | null
      bindProbeAt: string | null
      bindProbeStatus: string
      bindProbeMessage: string
      roleName?: string
      roleId?: string
    }
    role: {
      status: string
      name: string
      rid: string
      refreshedAt: string | null
      errorMessage: string
      rawInfo: Record<string, unknown> | null
    }
    purchase: {
      autoBuyEnabled: boolean
      minAssetReserve: number
      usedKnapsack: boolean
      purchaseTriggered: boolean
      assetBefore: number
      assetAfter: number
      purchaseAt: string | null
    }
    dispatch: {
      status: string
      dispatchAt: string | null
      sendType: number
      note: string
    }
    returnNumber: {
      status: string
      returnedAt: string | null
      autoReturnEnabled: boolean
    }
    consume: {
      status: string
      shopId: string
      shopName: string
      autoConsumeEnabled: boolean
      consumedAt: string | null
      errorMessage: string
    }
    rebind: {
      currentAttempt: number
      history: Array<{
        attempt: number
        source: string
        requestedAt: string | null
        requestedBy: Record<string, unknown> | null
        status: string
        errorMessage: string
        oldBinding: {
          vnKey: string
          vnId: number
          vnPhone: string
          bindUrl: string
          bindPreparedAt: string | null
          bindExpiresAt: string | null
          roleName: string
          roleId: string
        }
        newBinding: null | {
          vnKey: string
          vnId: number
          vnPhone: string
          bindUrl: string
          bindPreparedAt: string | null
          bindExpiresAt: string | null
        }
      }>
    }
    notes: string
  }
  kuaishouIndustryVoucher?: null | {
    oid: string
    voucherCode: string
    unitIndex: number
    status: string
    sendCallbackStatus: string
    sendCallbackLastError: string
    sendCallbackAttemptCount: number
    sendCallbackSentAt: string | null
    consumeSerialNum: string
    consumeDetails: Array<Record<string, unknown>>
    consumedAt: string | null
    destroyedAt: string | null
  }
  manualDispatch: null | {
    outcome: string
    deliveryReference: string
    deliveredCredential: string
    resultMessage: string
    completedAt: string | null
    completedBy: null | {
      userId: number
      username: string
      role: string
    }
  }
  events: Array<{
    eventId: number
    eventType: string
    payload: Record<string, unknown>
    createdAt: string
  }>
  operations: AdminTaskOperations
}
