import { NextRequest, NextResponse } from 'next/server';
import { getUserFromRequest, jsonError } from '@/lib/auth-helpers';
import { cachedBackendJson } from '@/services/odds-cache';

export const dynamic = 'force-dynamic';

export async function GET(req: NextRequest) {
  // The page itself shows the blurred preview for non-Desk users, so we
  // still return a small set of signals (so the timeline scaffolding
  // looks alive behind the blur). For Desk users, return full feed.
  const user = await getUserFromRequest(req);
  if (!user) return jsonError(401, 'Not signed in.');

  const limit = user.tier === 'desk' ? 50 : 8;

  try {
    const data = await cachedBackendJson<{ signals: unknown[] }>(
      `intelligence:feed:${limit}`,
      'mixed',
      'intelligence_feed',
      `/api/intelligence/feed?limit=${limit}`,
      60,
    );
    return NextResponse.json({ signals: Array.isArray(data?.signals) ? data.signals : [] });
  } catch {
    return NextResponse.json({ signals: [] });
  }
}
