import { NextResponse } from 'next/server';

import { withAuth } from '@/lib/api/helpers';
import { parseQuery } from '@/lib/api/route-helpers';
import { errorResponse } from '@/lib/errors';
import { listNotificationsSchema } from '@/lib/validators/notifications';
import * as notificationsService from '@/lib/services/notifications.service';

export const GET = withAuth(async (req, ctx) => {
  try {
    const query = parseQuery(req, listNotificationsSchema);
    const result = await notificationsService.listNotifications(ctx.userId, ctx.portId, query);
    // L34 carve-out: deliberate bare-shape response (NOT the `{ data }`
    // envelope). The notifications bell/list consumers read the fields
    // off the JSON root; wrapping them would break them. Left as-is.
    return NextResponse.json(result);
  } catch (error) {
    return errorResponse(error);
  }
});
