import { apiClient } from '@/shared/api/client'
import type { ApiResponse, PaginatedResult } from '@/shared/types/types'
import type { AdminPickup } from '@/features/admin/api/adminPickup'

export interface SellerPickupListQuery {
  page?: number
  page_size?: number
  status?: string
}

function cleanParams(query: object) {
  return Object.fromEntries(
    Object.entries(query).filter(([, value]) => value !== '' && value !== undefined)
  )
}

// 复用 AdminPickup 类型：卖家视角字段集合相同（admin_id 也会返回，前端不展示即可）
export type SellerPickup = AdminPickup

export async function fetchSellerPickups(query: SellerPickupListQuery = {}) {
  const { data } = await apiClient.get<ApiResponse<PaginatedResult<SellerPickup>>>(
    '/seller/pickups',
    { params: cleanParams(query) }
  )
  const result = data.data
  return {
    items: Array.isArray(result?.items) ? result.items : [],
    total: Number(result?.total ?? 0),
    page: Number(result?.page ?? query.page ?? 1),
    page_size: Number(result?.page_size ?? query.page_size ?? 20),
  }
}
