/** 后端统一响应格式 */
export interface ApiResponse<T = unknown> {
  code: number
  message: string
  data: T
}

/** 分页数据结构 */
export interface PaginatedData<T> {
  items: T[]
  total: number
  page: number
  per_page: number
}

/** 分页查询参数 */
export interface PaginationParams {
  page?: number
  per_page?: number
}

/** 父订单摘要（订单项详情接口返回） */
export interface ParentOrderSummary {
  id: number
  platform_order_id: string
  order_status_id: number
  total_amount: string
  total_paid: string
  created_date: string | null
  paid_date: string | null
}

/** 订单项详情（含父订单摘要） */
export interface OrderItemDetail {
  id: number
  company_id: number
  platform_id: number
  store_id: number
  order_id: number
  platform_order_id: string
  sub_order_id: string | null
  sub_order_type_id: number
  product_id: number
  platform_product_id: string
  product_sku: string | null
  product_barcode: string | null
  unit_price: string
  quantity: number
  discount: string
  total: string
  created_date: string | null
  ext: Record<string, unknown> | null
  created_at: string
  updated_at: string
  parent_order: ParentOrderSummary | null
}

/** 订单项筛选参数 */
export interface OrderItemFilters {
  platform_order_id: string
  platform_product_id: string
  product_sku: string
  created_date_range: [string, string] | null
}

/** 退款列表记录（14 字段） */
export interface RefundRecord {
  id: number
  company_id: number
  platform_id: number
  store_id: number
  platform_refund_id: string
  platform_order_id: string
  refund_status_id: number
  refund_type_id: number
  refund_amount: string
  freight_refund: string
  refund_total: string
  currency: string
  created_date: string | null
  completed_date: string | null
}

/** 退款详情（含扩展字段） */
export interface RefundDetail extends RefundRecord {
  order_id: number
  buyer_user_id: string | null
  reason: string | null
  order_created_date: string | null
  order_paid_date: string | null
  updated_date: string | null
  ext: Record<string, unknown> | null
  created_at: string
  updated_at: string
}

/** 退款筛选参数 */
export interface RefundFilters {
  refund_status_id: number | undefined
  refund_type_id: number | undefined
  platform_refund_id: string
  platform_order_id: string
  created_date_range: [string, string] | null
}

/** 退款项列表记录（15 字段） */
export interface RefundItemRecord {
  id: number
  company_id: number
  platform_id: number
  store_id: number
  refund_id: number
  platform_refund_id: string
  platform_order_id: string
  platform_sub_order_id: string | null
  platform_product_id: string
  refund_status_id: number
  refund_type_id: number
  quantity: number
  refund_amount: string
  currency: string
  created_date: string | null
}

/** 退款项详情（含扩展字段） */
export interface RefundItemDetail extends RefundItemRecord {
  platform_parent_refund_id: string | null
  reason: string | null
  buyer_user_id: string | null
  order_created_date: string | null
  order_paid_date: string | null
  updated_date: string | null
  completed_date: string | null
  ext: Record<string, unknown> | null
  created_at: string
  updated_at: string
}

/** 退款项筛选参数 */
export interface RefundItemFilters {
  refund_id: number | undefined
  refund_status_id: number | undefined
  refund_type_id: number | undefined
  platform_refund_id: string
  platform_order_id: string
  platform_product_id: string
  created_date_range: [string, string] | null
}

/** ─── MQ 状态监控 ─── */

export type MqQueueStatusEnum = 'high_load' | 'processing' | 'active' | 'empty' | 'error'

export interface MqQueueInfo {
  queue: string
  messages: number | string
  consumers: number | string
  status: MqQueueStatusEnum
}

export interface MqSummary {
  total_messages: number
  total_consumers: number
}

export interface MqStatusData {
  business_queues: MqQueueInfo[]
  retry_queues: MqQueueInfo[]
  error_queue: MqQueueInfo | []
  summary: MqSummary
  fetched_at: string
}

export type MqQueueType = 'orders' | 'products' | 'refunds' | 'inventory'

/** ─── 失败消息 ─── */

export type FailedMessageDataType = 'order' | 'product' | 'refund' | 'inventory'

/** 失败消息列表记录 */
export interface FailedMessageRecord {
  id: number
  error_id: string
  data_type: FailedMessageDataType
  platform: string
  platform_id: number
  company_id: number
  store_id: number
  error_type: string
  error_message: string
  retry_count: number
  message_id: string
  failed_at: string
  created_at: string
}

/** 失败消息详情（含错误堆栈和原始消息） */
export interface FailedMessageDetail extends FailedMessageRecord {
  error_code: string | null
  error_trace: string | null
  original_message: Record<string, unknown> | null
}

/** 失败消息筛选参数 */
export interface FailedMessageFilters {
  data_type: FailedMessageDataType | undefined
  platform_id: number | undefined
  failed_at_range: [string, string] | null
}

/** ─── Dashboard 统计 ─── */

export interface SuccessFailedCount {
  success: number
  failed: number
}

export interface DataTypeCount extends SuccessFailedCount {
  data_type: string
}

export interface DashboardOverview {
  today: SuccessFailedCount
  this_week: SuccessFailedCount
  this_month: SuccessFailedCount
  by_type: DataTypeCount[]
}

export interface DashboardTrendPoint {
  date: string
  success: number
  failed: number
}

export interface DashboardTrendParams {
  from?: string
  to?: string
  group_by?: 'day' | 'week' | 'month'
  data_type?: string
}

export interface DashboardBreakdownItem {
  id: number
  name: string
  success: number
  failed: number
}

export interface DashboardBreakdownParams {
  dimension?: 'company' | 'platform' | 'store'
  from?: string
  to?: string
  data_type?: string
}

/** ─── 请求日志 ─── */

export interface RequestLogRecord {
  id: number
  user_id: number | null
  method: string
  path: string
  status_code: number
  ip: string
  response_code: number
  duration_ms: number
  created_at: string
}

export interface RequestLogDetail extends RequestLogRecord {
  user_agent: string | null
  request_body: Record<string, unknown> | null
}

export interface RequestLogFilters {
  user_id: number | undefined
  method: string | undefined
  path: string
  status_code: number | undefined
  created_at_range: [string, string] | null
}

/** ─── 操作日志 ─── */

export interface OperationLogRecord {
  id: number
  user_id: number | null
  action: string
  target_type: string
  target_id: number
  description: string
  ip: string
  created_at: string
}

export interface OperationLogDetail extends OperationLogRecord {
  detail: Record<string, unknown> | null
}

export interface OperationLogFilters {
  user_id: number | undefined
  action: string | undefined
  target_type: string | undefined
  created_at_range: [string, string] | null
}

/** ─── API Key ─── */

export interface ApiKeyRecord {
  id: number
  user_id: number
  name: string
  key_prefix: string
  last_used_at: string | null
  expires_at: string | null
  enabled: boolean
  created_at: string
  user?: { id: number; username: string; email?: string; api_key_enabled?: boolean }
}

export interface ApiKeyCreateParams {
  name: string
  expires_at?: string | null
}

export interface ApiKeyCreateResult {
  plain_key: string
  api_key: ApiKeyRecord
}

export interface AdminApiKeyFilters {
  username: string | undefined
  email: string | undefined
  enabled: boolean | undefined
}

/** 业务异常 */
export class ApiError extends Error {
  code: number
  constructor(message: string, code: number) {
    super(message)
    this.code = code
    this.name = 'ApiError'
  }
}
