/** 精选菜品列表 POST body，见 docs/app-featured-dish-list.md */
export type FeaturedDishQueryBody = {
  hasMemberPrice?: any
  stockMin?: any
  stockMax?: any
  recipeCategoryId?: any
  createBeginTime?: any
  createEndTime?: any
  salesSort?: 'asc' | 'desc'
  isNew?: any
}

export type RouteQueryMap = Record<string, string | undefined>
/** 将 URL 上可选筛选参数合并进 body（分页仍走 query） */
export function mergeRouteQueryIntoBody(
  base: FeaturedDishQueryBody,
  routeQuery: RouteQueryMap = {},
): FeaturedDishQueryBody {
  const body: FeaturedDishQueryBody = { ...base }
  const recipeCategoryId = routeQuery.recipeCategoryId
  const stockMin = routeQuery.stockMin
  const stockMax = routeQuery.stockMax
  const createBeginTime = routeQuery.createBeginTime
  const createEndTime = routeQuery.createEndTime
  const hasMemberPrice = routeQuery.hasMemberPrice
  const salesSort = routeQuery.salesSort

  if (recipeCategoryId != null) body.recipeCategoryId = recipeCategoryId
  if (stockMin != null) body.stockMin = stockMin
  if (stockMax != null) body.stockMax = stockMax
  if (createBeginTime != null) body.createBeginTime = createBeginTime
  if (createEndTime != null) body.createEndTime = createEndTime
  if (hasMemberPrice != null) body.hasMemberPrice = hasMemberPrice
  if (salesSort === 'asc' || salesSort === 'desc') body.salesSort = salesSort

  return body
}

/** 会员专区：仅查有会员价菜品 */
export function buildMemberZoneQuery(routeQuery?: RouteQueryMap): FeaturedDishQueryBody {
  return mergeRouteQueryIntoBody({ hasMemberPrice: true }, routeQuery)
}

/** 海鲜菜谱分类 ID（保持字符串，勿转 number） */
const SEAFOOD_RECIPE_CATEGORY_ID = '2037471880338415618'

/** 限量空运活海鲜：海鲜分类 + 库存范围 */
export function buildLiveSeafoodAirQuery(routeQuery?: RouteQueryMap): FeaturedDishQueryBody {
  return mergeRouteQueryIntoBody(
    {
      recipeCategoryId: SEAFOOD_RECIPE_CATEGORY_ID,
      stockMin: 1,
      stockMax: 200,
    },
    routeQuery,
  )
}

/** 必吃榜：按销量降序 */
export function buildMustEatListQuery(routeQuery?: RouteQueryMap): FeaturedDishQueryBody {
  return mergeRouteQueryIntoBody({ salesSort: 'desc' }, routeQuery)
}

/** 当天 00:00:00 ~ 23:59:59（本地时区，10 位秒级时间戳） */
export function getTodayCreateTimeRange(): { createBeginTime: number; createEndTime: number } {
  const now = new Date()
  const createBeginTime = Math.floor(
    new Date(
      now.getFullYear(),
      now.getMonth(),
      now.getDate(),
      0,
      0,
      0,
      0,
    ).getTime() / 1000,
  )
  const createEndTime = Math.floor(
    new Date(
      now.getFullYear(),
      now.getMonth(),
      now.getDate(),
      23,
      59,
      59,
      0,
    ).getTime() / 1000,
  )
  return { createBeginTime, createEndTime }
}

/** 上新日历：当天创建 + 新品（isNew = 1） */
export function buildNewCalendarQuery(routeQuery?: RouteQueryMap): FeaturedDishQueryBody {
  return mergeRouteQueryIntoBody(
    {
      ...getTodayCreateTimeRange(),
      isNew: 1,
    },
    routeQuery,
  )
}

/** 今日现打海鲜：海鲜分类 + 当天创建时间（秒级时间戳） */
export function buildFreshSeafoodTodayQuery(routeQuery?: RouteQueryMap): FeaturedDishQueryBody {
  return mergeRouteQueryIntoBody(
    {
      recipeCategoryId: SEAFOOD_RECIPE_CATEGORY_ID,
      ...getTodayCreateTimeRange(),
    },
    routeQuery,
  )
}

export const TOPIC_QUERY_BUILDERS: Record<
  string,
  (routeQuery?: RouteQueryMap) => FeaturedDishQueryBody
> = {
  'member-zone': buildMemberZoneQuery,
  'live-seafood-air': buildLiveSeafoodAirQuery,
  'must-eat-list': buildMustEatListQuery,
  'new-calendar': buildNewCalendarQuery,
  'fresh-seafood-today': buildFreshSeafoodTodayQuery,
}
