export type WorkerUser = {
  workerId: number
  username: string
  displayName: string
  phone: string
  status: string
  reviewNote: string
  inviteCode: string
  inviter: null | {
    workerId: number
    username: string
    displayName: string
  }
  level: null | {
    levelId: number
    levelKey: string
    name: string
    permissions: {
      depositFreeAmount: number
      maxActiveOrders: number
      upgradeThreshold: number
      visibleDelaySeconds: number
    }
  }
  wallet: {
    availableAmount: number
    frozenDepositAmount: number
    pendingUnfreezeAmount: number
    totalCreditedAmount: number
    totalSettledAmount: number
  }
  timeoutOrderCount?: number
  createdAt: string
  updatedAt: string
  reviewedAt: string | null
}

export type WorkerProfileSummary = {
  acceptedOrderCount: number
  timeoutOrderCount: number
  pendingWithdrawAmount: number
  approvedWithdrawAmount: number
  pendingRechargeAmount: number
}

export type WorkerLevelProgress = {
  currentThreshold: number
  nextThreshold: number | null
  progressPercent: number
}

export type WorkerWalletLedger = {
  ledgerId: number
  ledgerType: string
  amount: number
  balanceAfter: number
  frozenAfter: number
  relatedWorkOrderId: number | null
  auditStatus: string
  note: string
  payload: Record<string, unknown>
  createdAt: string
}

export type WorkerFinanceRequest = {
  requestId: number
  requestType: string
  status: string
  amount: number
  accountChannel: string
  accountName: string
  accountNo: string
  note: string
  reviewedNote: string
  payload: Record<string, unknown>
  createdAt: string
  updatedAt: string
  reviewedAt: string | null
  worker: null | {
    workerId: number
    username: string
    displayName: string
    phone: string
  }
}

export type WorkerFinanceConfig = {
  depositUnfreezeDays: number
  recharge: {
    enabled: boolean
    channelName: string
    accountName: string
    accountNo: string
    qrCodeImage: UploadedFile | null
    instructions: string
  }
  withdraw: {
    enabled: boolean
    instructions: string
  }
}

export type WorkerProfileResponse = {
  worker: WorkerUser
  permissions: Record<string, unknown>
  summary: WorkerProfileSummary
  levelProgress: WorkerLevelProgress | null
  financeConfig: WorkerFinanceConfig
}

export type WorkerLevel = {
  levelId: number
  levelKey: string
  name: string
  sortOrder: number
  status: string
  depositFreeAmount: number
  maxActiveOrders: number
  upgradeThreshold: number
  visibleDelaySeconds: number
  createdAt: string
  updatedAt: string
}

export type WorkCategory = {
  categoryId: number
  categoryKey: string
  name: string
  sortOrder: number
  status: string
  createdAt: string
  updatedAt: string
}

export type WorkProductRule = {
  ruleId: number
  ruleKey: string
  provider: string
  platform: string
  shopId: string
  skuCode: string
  productName: string
  matchType: 'contains' | 'exact' | string
  categoryId: number | null
  categoryName: string
  enabled: boolean
  autoCreate: boolean
  rewardAmount: number
  requiredDepositAmount: number
  depositThresholdAmount: number
  sharing?: WorkOrderSharingConfig
  requirement: {
    fields: CollectField[]
  }
  timeoutMinutes?: number
  timeoutPolicy?: 'reopen' | 'cancel_release' | 'cancel_deduct' | string
  sortOrder: number
  createdAt: string
  updatedAt: string
}

export type UploadedFile = {
  objectKey: string
  url: string
  thumbnailUrl: string
  mediumUrl: string
  filename: string
  contentType: string
  size: number
}

export type WorkOrderStatus =
  | 'pending_material'
  | 'unassigned'
  | 'open'
  | 'in_progress'
  | 'pending_acceptance'
  | 'problem'
  | 'accepted'
  | 'cancelled'
  | string

export type WorkOrderSharingConfig = {
  enabled: boolean
  totalQuantity: number
  unitReward: number
}

export type WorkOrderShare = {
  shareId: number
  workOrderId: number
  workerId: number
  quantity: number
  unitReward: number
  shareReward: number
  shareDeposit: number
  status: string
  acceptance: Record<string, unknown>
  submittedAt: string | null
  acceptedAt: string | null
  createdAt: string
  updatedAt: string
  worker: null | {
    workerId: number
    username: string
    displayName: string
  }
}

export type WorkOrder = {
  workOrderId: number
  orderId?: number | null
  orderItemId?: number | null
  workOrderNo: string
  platformOrderId: string
  productName: string
  categoryId?: number | null
  categoryName: string
  status: WorkOrderStatus
  rewardAmount: number
  requiredDepositAmount?: number
  depositThresholdAmount?: number
  freezeDepositAmount?: number
  sharing?: WorkOrderSharingConfig
  myShare?: WorkOrderShare | null
  sharingProgress?: {
    joinedQuantity: number
    pendingSubmissionCount: number
  }
  pendingUnfreezeAmount?: number
  timeoutMinutes?: number
  timeoutPolicy?: 'reopen' | 'cancel_release' | 'cancel_deduct' | string
  deadlineAt?: string | null
  material: Record<string, unknown>
  requirement: Record<string, unknown>
  acceptance: {
    note?: string
    files?: UploadedFile[]
    imageUrls?: string[]
    submittedAt?: string
    [key: string]: unknown
  }
  problemNote: string
  worker: null | {
    workerId: number
    username: string
    displayName: string
  }
  publishedAt: string | null
  assignedAt: string | null
  submittedAt: string | null
  acceptedAt: string | null
  createdAt: string
  updatedAt: string
}

export type Pagination = {
  page: number
  pageSize: number
  total: number
}

export type WorkerLoginResponse = {
  token: string
  expiresAt: string
  worker: WorkerUser
}

export type WorkerListResponse<T> = {
  items: T[]
  pagination: Pagination
}

export type WorkerHallOrdersResponse = {
  items: WorkOrder[]
  pagination: Pagination
  categories: WorkCategory[]
  visibleDelaySeconds?: number
}

export type CollectField = {
  key: string
  label: string
  required: boolean
  type?: 'text' | 'select'
  options?: string[]
}

export type CollectLookupOrder = {
  workOrderId: number
  workOrderNo: string
  platformOrderId: string
  productName: string
  status: string
  fields: CollectField[]
  values?: Record<string, string>
}

export type CollectLookupResponse = {
  order: Pick<
    WorkOrder,
    'workOrderId' | 'workOrderNo' | 'platformOrderId' | 'productName' | 'status'
  >
  fields: CollectField[]
  orders: CollectLookupOrder[]
  pendingCount: number
}
