import { apiRoute } from '@/lib/api/handler'
import { listTwins } from '@/lib/services/twin-service'

export const dynamic = 'force-dynamic'

/** Every twin the caller may look at, newest level first (§77). */
export const GET = apiRoute({ permission: 'twin.read' }, async ({ searchParams }) => {
  const level = searchParams.get('level')?.trim()
  const twins = await listTwins()
  return {
    twins: level ? twins.filter((twin) => twin.level === level) : twins,
  }
})
