export type OrderRow = {
  id: number
  provider: string
  platform: string
  shop_id: string
  shop_name: string
  platform_order_id: string
  order_status: string
  pay_status: string
  buyer_id: string
  buyer_name: string
  receiver_contact: string
  total_amount: number | string
  currency: string
  raw_payload_json: string | Record<string, unknown>
  paid_at: string | null
  created_at: string
  updated_at: string
}

export type OrderListRow = OrderRow & {
  task_count?: number
}

export type OrderItemRow = {
  id: number
  order_id: number
  sku_code: string
  sku_name: string
  quantity: number
  spec_json: string | Record<string, unknown>
  item_snapshot_json?: string | Record<string, unknown>
  created_at?: string
  updated_at?: string
}

export type TaskRow = {
  id: number
  order_id: number
  order_item_id: number
  platform_order_id: string
  profile_id: number
  task_no: string
  executor_key: string
  task_status: string
  delivery_status: string
  result_code: string
  result_message: string
  inventory_status: string
  automation_mode: string
  requires_claim: boolean
  user_action_status: string
  attempt_count: number
  browser_session_id: string
  login_type: string
  nickname: string
  role_name: string
  role_id: string
  area: string
  partition_name: string
  claim_token: string
  claim_expires_at?: string | null
  primary_claim_token: string
  primary_claim_token_id: number | null
  primary_claim_token_status: string
  primary_claim_expires_at?: string | null
  primary_inventory_item_id: number | null
  primary_inventory_binding_status: string
  inventory_display_value: string
  primary_inventory_display_value: string
  inventory_credential_type?: string
  primary_inventory_credential_type?: string
  artifacts_json: string | Record<string, unknown>
  context_json: string | Record<string, unknown>
  screenshot_path: string
  last_error: string
  retry_count: number
  created_at: string
  updated_at: string
  claimed_at: string | null
  role_confirmed_at: string | null
  redeemed_at: string | null
  sku_code?: string
  sku_name?: string
}

export type InventoryItemRow = {
  id: number
  sku_code: string
  batch_no: string
  credential_type: string
  inventory_group_code: string
  display_value: string
  status: string
  invalid_reason: string
  delivered_at: string | null
  created_at: string
  updated_at: string
  reserved_by_task_id: number | null
  reserved_by_task_no: string
  platform_order_id: string
}

export type InventorySkuSuggestionRow = {
  sku_code: string
  credential_type: string
  inventory_group_code: string
  total_count: number
  available_count: number
  latest_updated_at: string | null
}

export type WebhookEventRow = {
  id: number
  provider: string
  platform: string
  shop_id: string
  shop_name: string
  event_type: string
  event_key: string
  signature_valid: boolean
  headers_json: string | Record<string, unknown>
  query_json: string | Record<string, unknown>
  body_json: string | Record<string, unknown>
  processed: boolean
  process_error: string
  related_order_id: number | null
  created_at: string
}

export type MessageDeliveryRow = {
  id: number
  provider: string
  platform: string
  shop_id: string
  shop_name: string
  channel: string
  order_id: number | null
  task_id: number | null
  platform_order_id: string
  recipient_key: string
  message_content: string
  claim_url: string
  status: string
  request_url: string
  request_headers_json: string | Record<string, unknown>
  request_body_json: string | Record<string, unknown>
  response_status: number
  response_json: string | Record<string, unknown>
  error_message: string
  sent_at: string | null
  created_at: string
  updated_at: string
  task_no?: string
  task_status?: string
}

export type TaskInventoryBindingRow = {
  id: number
  task_id: number
  inventory_item_id: number
  role_key: string
  quantity: number
  binding_status: string
  metadata_json: string | Record<string, unknown>
  invalid_reason: string
  consumed_at: string | null
  released_at: string | null
  created_at: string
  updated_at: string
  sku_code: string
  batch_no: string
  credential_type: string
  inventory_group_code: string
  display_value: string
  inventory_status: string
}

export type TaskInventoryBindingSummaryRow = {
  task_id: number
  total_binding_count: number
  reserved_binding_count: number
  consumed_binding_count: number
  released_binding_count: number
  role_keys: string[]
}

export type ClaimTokenRow = {
  id: number
  token: string
  status: string
  expired_at: string
  created_at?: string
  updated_at?: string
}

export type TaskEventRow = {
  id: number
  event_type: string
  payload_json: string | Record<string, unknown>
  created_at: string
}

export type OrderListQueryResult = {
  items: OrderListRow[]
  total: number
}

export type TaskListQueryResult = {
  items: TaskRow[]
  total: number
}

export type InventoryListQueryResult = {
  items: InventoryItemRow[]
  total: number
}

export type WebhookEventListQueryResult = {
  items: WebhookEventRow[]
  total: number
}

export type MessageDeliveryListQueryResult = {
  items: MessageDeliveryRow[]
  total: number
}
