import {RecommendedCrumbsQuerySchema} from '@crumbs/core/schemas'
import {Hono} from 'hono'

import {type GuardedVars} from '../auth/middleware'
import {validate} from '../lib/validate'
import {listFriendIds} from '../services/friends'
import {recommendFriendCrumbs} from '../services/recommendations'

// nearby standout crumbs from places the viewer's accepted friends have crumbed
const recommendations = new Hono<{Variables: GuardedVars}>().get(
  '/crumbs',
  validate('query', RecommendedCrumbsQuerySchema),
  async (c) => {
    const me = c.get('user')
    const {lat, lng, radiusKm, limit} = c.req.valid('query')

    const friendIds = await listFriendIds(me.id)
    const crumbs = await recommendFriendCrumbs({
      viewerId: me.id,
      friendIds,
      center: {latitude: lat, longitude: lng},
      radiusKm,
      limit,
    })
    return c.json({crumbs})
  },
)

export default recommendations
