import { NextResponse } from 'next/server'
import { getOrCreateOpenCart, priceCart } from '@/lib/data/purchases'
import { requireCartAccess } from '@/lib/purchase-api'

/** The caller's open cart for this trip, priced live from the catalog (task §4.1). */
export async function GET(_req: Request, ctx: { params: Promise<{ id: string }> }) {
  const { id } = await ctx.params
  const guard = await requireCartAccess(id)
  if (!guard.ok) return guard.response
  const cart = await getOrCreateOpenCart(guard.trip.id, guard.user.id)
  return NextResponse.json({ ok: true, cart: await priceCart(cart) })
}
