import type {NextRequest} from 'next/server';
import {fail, ok, parseQuery, requireApiSession, simulate} from '@/shared/services/apiRoute';
import {buildStore} from '@/features/stores/mock/stores.mock';

export const dynamic = 'force-dynamic';

export async function GET(
  req: NextRequest,
  {params}: {params: Promise<{storeId: string}>},
) {
  const denied = await requireApiSession();
  if (denied) return denied;
  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);
}
