import type { KnownTaskStatus } from '@/domain/task-status'

export type ClaimTokenStatus = 'active' | 'used' | 'expired' | 'revoked' | (string & {})

export type ClaimTaskStatus = KnownTaskStatus | (string & {})

/** 与后端 ClaimIdentityPayload 对齐 */
export interface ClaimIdentityInfo {
  expectedUid: string
  submittedAt: string | null
  source: string
  ready: boolean
}

export interface ClaimTaskInfo {
  taskId: number
  taskNo: string
  status: ClaimTaskStatus
  executorKey: string
  requiresSupportReview: boolean
  expiresAt: string | null
  claimedAt: string | null
  roleConfirmedAt: string | null
  redeemedAt: string | null
  loginType: string
  lastError: string
  runtimeSessionId: string
}

export interface ClaimOrderInfo {
  orderId: number
  platform: string
  platformOrderId: string
  payStatus: string
  orderStatus: string
  totalAmount: string
  totalAmountFen: number
  currency: string
}

export interface ClaimOrderItemInfo {
  orderItemId: number
  skuCode: string
  skuName: string
  quantity: number
}

export interface ClaimProductInfo {
  title: string
  skuCode: string
  quantity: number
  isBundle: boolean
  items: Array<{
    cloudSkuId: number
    name: string
    quantity: number
  }>
}

export interface ClaimResultInfo {
  resultCode: string
  resultMessage: string
  screenshotReady: boolean
  screenshotUrl: string
}

export interface ClaimKuaishouCloudFlowInfo {
  flowType: 'kuaishou_cloud'
  shopId: string
  shopName: string
  guideImages: string[]
  ticket: {
    code: string
    status: string
    capturedAt: string | null
    verifiedAt: string | null
    goodsTitle: string
    leftCount: number
  }
  binding: {
    prepareStatus: string
    cloudSourceKeys: string[]
    resolvedSourceKey: string
    skuId: number
    skuName: string
    vnKey: string
    vnId: number
    bindUrl: string
    bindPreparedAt: string | null
    bindExpiresAt: string | null
    bindProbeAt: string | null
    bindProbeStatus: string
    bindProbeMessage: string
    vnPhone: string
    roleName: string
    roleId: string
  }
  role: {
    status: string
    name: string
    rid: string
    refreshedAt: string | null
    errorMessage: string
    defaultName: string
    defaultRid: string
    defaultCapturedAt: string | null
    defaultCaptureStatus: string
    defaultErrorMessage: string
    isDefaultRole: boolean
  }
  purchase: {
    autoBuyEnabled: boolean
    minAssetReserve: number
    usedKnapsack: boolean
    purchaseTriggered: boolean
    assetBefore: number
    assetAfter: number
    purchaseAt: string | null
  }
  dispatch: {
    status: string
    dispatchAt: string | null
    failedAt: string | null
    failedStage: string
    errorCode: string
    errorMessage: string
    note: string
  }
  returnNumber: {
    status: string
    returnedAt: string | null
  }
  consume: {
    status: string
    shopId: string
    shopName: string
    consumedAt: string | null
    errorMessage: string
  }
  rebind?: {
    currentAttempt: number
    history: Array<Record<string, unknown>>
  }
}

export interface ClaimKuaishouFeifeiFlowInfo {
  flowType: 'kuaishou_feifei'
  productCode: string
  productName: string
  platformOrderNo: string
  orderNo: string
  rechargeStatus: number
  rechargeStatusLabel: string
  rechargeResultMessage: string
  claimUrl: string
  consumeStatus: string
  h5: {
    entryUrl: string
    rechargeUrl: string
  }
  h5UrlWithUid: string
  lastSyncedAt: string | null
}

export interface ClaimAffiliateDashFlowInfo {
  flowType: 'affiliate_dash'
  sku: string
  productName: string
  orderNo: string
  clientOrderNo: string
  orderStatus: string
  canShip: boolean
  cannotShipReason: string
  providerOrderNo: string
  failureReason: string
  amount: number
  currency: string
  bindUuid: string
  bindUrl: string
  qrUrl: string
  gameAccount: string
  expectedGameAccount: string
  bindMismatch: boolean
  bound: boolean
  boundAccount: string
  gameChannel: string
  submitStatus: string
  consumeStatus: string
  lastSyncedAt: string | null
}

export interface ClaimDetailData {
  tokenStatus: ClaimTokenStatus
  claimUrl: string
  flowType:
    | 'kuaishou_cloud'
    | 'kuaishou_ct_assisted'
    | 'kuaishou_feifei'
    | 'affiliate_dash'
    | (string & {})
  claimIdentity: ClaimIdentityInfo | null
  task: ClaimTaskInfo
  order: ClaimOrderInfo
  orderItem: ClaimOrderItemInfo
  product: ClaimProductInfo
  session: unknown | null
  kuaishouCloudFulfillment: ClaimKuaishouCloudFlowInfo | null
  kuaishouFeifei: ClaimKuaishouFeifeiFlowInfo | null
  affiliateDash: ClaimAffiliateDashFlowInfo | null
  result: ClaimResultInfo | null
}
