import type {NextRequest} from 'next/server';
import {fail, ok, parseQuery, simulate} from '@/lib/api/server';
import {buildStore} from '@/lib/mock/stores';

export const dynamic = 'force-dynamic';

export async function GET(
  req: NextRequest,
  {params}: {params: Promise<{storeId: string}>},
) {
  const q = parseQuery(req);
  const simulated = await simulate(q);
  if (simulated) return simulated;

  const {storeId} = await params;
  const store = buildStore(storeId, q.range, q.nowMs);
  if (!store) {
    return fail('not_found', `No store with id "${storeId}"`, 404);
  }
  return ok(store, q);
}
