import {pageQueryShape} from '@crumbs/core/schemas'
import {Hono} from 'hono'
import {z} from 'zod'

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

// recent activity by the viewer and their accepted friends
const feed = new Hono<{Variables: GuardedVars}>().get(
  '/',
  validate('query', z.object(pageQueryShape)),
  async (c) => {
    const me = c.get('user')
    const {limit, cursor} = c.req.valid('query')

    const authorIds = [me.id, ...(await listFriendIds(me.id))]
    const {items, nextCursor} = await listActivityFeed({
      viewerId: me.id,
      authorIds,
      limit,
      cursor,
    })
    return c.json({items, nextCursor})
  },
)

export default feed
