

export type WorkerLevelRow = {
  id: number
  level_key: string
  name: string
  sort_order: number
  status: string
  permission_json: string | Record<string, unknown>
  created_at: string
  updated_at: string
}

export type WorkerUserRow = {
  id: number
  username: string
  password_hash: string
  display_name: string
  phone: string
  level_id: number | null
  status: string
  review_note: string
  session_version: number
  created_at: string
  updated_at: string
  reviewed_at: string | null
  inviter_id: number | null
  invite_code: string
  inviter_username?: string
  inviter_display_name?: string
  level_key?: string
  level_name?: string
  level_permission_json?: string | Record<string, unknown>
  available_amount?: number
  frozen_deposit_amount?: number
  pending_unfreeze_amount?: number
  total_credited_amount?: number
  total_settled_amount?: number
}

export type WorkerWalletRow = {
  worker_id: number
  available_amount: number
  frozen_deposit_amount: number
  pending_unfreeze_amount: number
  total_credited_amount: number
  total_settled_amount: number
  created_at: string
  updated_at: string
}

export type WorkerDepositUnfreezeRow = {
  id: number
  worker_id: number
  work_order_id: number
  amount: number
  unfreeze_at: string
  status: string
  released_at: string | null
  created_at: string
  updated_at: string
}

export type WorkerWalletLedgerRow = {
  id: number
  worker_id: number
  ledger_type: string
  amount: number
  balance_after: number
  frozen_after: number
  related_work_order_id: number | null
  audit_status: string
  note: string
  payload_json: string | Record<string, unknown>
  created_at: string
}

export type WorkerFinanceRequestRow = {
  id: number
  worker_id: number
  request_type: string
  status: string
  amount: number
  account_channel: string
  account_name: string
  account_no: string
  note: string
  reviewed_note: string
  payload_json: string | Record<string, unknown>
  created_at: string
  updated_at: string
  reviewed_at: string | null
  worker_username?: string
  worker_display_name?: string
  worker_phone?: string
}

export type WorkCategoryRow = {
  id: number
  category_key: string
  name: string
  sort_order: number
  status: string
  created_at: string
  updated_at: string
}

export type WorkProductRuleRow = {
  id: number
  rule_key: string
  provider: string
  platform: string
  shop_id: string
  sku_code: string
  product_name: string
  match_type: string
  category_id: number | null
  enabled: boolean
  auto_create: boolean
  reward_amount: number
  required_deposit_amount: number
  deposit_threshold_amount: number
  sharing_enabled: boolean
  sharing_total_quantity: number
  sharing_unit_reward: number
  timeout_minutes: number
  timeout_policy: string
  requirement_json: string | Record<string, unknown>
  sort_order: number
  created_at: string
  updated_at: string
  category_name?: string
}

export type WorkOrderShareRow = {
  id: number
  work_order_id: number
  worker_id: number
  quantity: number
  unit_reward: number
  share_reward: number
  share_deposit: number
  status: string
  acceptance_json: string | Record<string, unknown>
  submitted_at: string | null
  accepted_at: string | null
  created_at: string
  updated_at: string
  worker_username?: string
  worker_display_name?: string
}

export type WorkOrderRow = {
  id: number
  work_order_no: string
  order_id: number | null
  order_item_id: number | null
  task_id: number | null
  platform_order_id: string
  product_name: string
  category_id: number | null
  status: string
  reward_amount: number
  required_deposit_amount: number
  deposit_threshold_amount: number
  assigned_worker_id: number | null
  last_assigned_worker_id: number | null
  material_json: string | Record<string, unknown>
  requirement_json: string | Record<string, unknown>
  acceptance_json: string | Record<string, unknown>
  problem_note: string
  sharing_enabled: boolean
  sharing_total_quantity: number
  sharing_unit_reward: number
  published_at: string | null
  assigned_at: string | null
  submitted_at: string | null
  accepted_at: string | null
  deadline_at: string | null
  timeout_minutes: number
  timeout_policy: string
  created_at: string
  updated_at: string
  category_name?: string
  worker_username?: string
  worker_display_name?: string
}

export type ProblemWorkOrderResolutionAction =
  | 'return_to_worker'
  | 'reopen'
  | 'cancel_release'
  | 'cancel_deduct'

export type GrabWorkOrderFailureReason =
  | 'worker_active_order_limit'
  | 'worker_deposit_insufficient'
  | 'work_order_not_open'

export type GrabWorkOrderResult = {
  order: WorkOrderRow | null
  failureReason: GrabWorkOrderFailureReason | null
}

export type ListInput = {
  page?: number
  pageSize?: number
  status?: string
  keyword?: string
  workerId?: number
  categoryId?: number
  workerSharingId?: number
  visibleAfterIso?: string
}

export type ProductRuleListInput = {
  enabled?: boolean | null
  keyword?: string
}

export type WalletLedgerListInput = {
  page?: number
  pageSize?: number
  workerId?: number
  ledgerType?: string
}

export type FinanceRequestListInput = {
  page?: number
  pageSize?: number
  workerId?: number
  status?: string
  requestType?: string
  keyword?: string
}

export type CreateWorkerInput = {
  username: string
  passwordHash: string
  displayName: string
  phone: string
  levelId: number | null
  status: string
  inviterId?: number | null
  inviteCode: string
  now: string
}

export type CreateWorkOrderInput = {
  workOrderNo: string
  orderId?: number | null
  orderItemId?: number | null
  taskId?: number | null
  platformOrderId: string
  productName: string
  categoryId?: number | null
  status: string
  rewardAmount: number
  requiredDepositAmount: number
  depositThresholdAmount: number
  sharingEnabled?: boolean
  sharingTotalQuantity?: number
  sharingUnitReward?: number
  timeoutMinutes?: number
  timeoutPolicy?: string
  materialJson: string
  requirementJson: string
  now: string
}
