import { apiRoute } from '@/lib/api/handler'
import { ApiError, ERROR_CODES } from '@/lib/api/errors'
import { getAssetHistory } from '@/lib/services/live-grid-service'

export const dynamic = 'force-dynamic'

/**
 * A longer window at a coarser resolution.
 *
 * The same series as `/telemetry` with a different sampling density: twenty-four hours at
 * one point a minute is fourteen hundred points nobody can read, so the point count is
 * held constant and the interval stretches.
 */
export const GET = apiRoute<{ code: string }>(
  { permission: 'twin.read', limit: 120 },
  async ({ params, searchParams }) => {
    const windowMin = Math.min(1_440, Math.max(15, Number(searchParams.get('window')) || 360))
    const history = await getAssetHistory(params.code, { windowMin, points: 96 })
    if (!history) throw new ApiError(ERROR_CODES.notFound)
    return history
  },
)
