import { apiRoute } from '@/lib/api/handler'
import { ApiError, ERROR_CODES } from '@/lib/api/errors'
import { analyseAsset, futureBranches, pickFocusAsset } from '@/lib/services/intelligence-service'

export const dynamic = 'force-dynamic'

/**
 * Three futures side by side (§8).
 *
 * Every branch is measured by re-running the twin with that branch's actions applied, so
 * the figures are simulation results rather than illustrative labels — which is why they
 * are marked as demo estimates wherever they are displayed (§57).
 */
export const GET = apiRoute({ permission: 'simulations.read', limit: 40 }, async ({ searchParams }) => {
  const requested = searchParams.get('assetCode')?.trim()
  const assetCode = requested && requested.length > 0 ? requested : await pickFocusAsset()
  if (!assetCode) throw new ApiError(ERROR_CODES.notFound)

  const intel = await analyseAsset(assetCode)
  if (!intel) throw new ApiError(ERROR_CODES.notFound)

  return { assetCode, isDemoEstimate: true, branches: await futureBranches(intel) }
})
