import { z } from "zod";
import { Notification } from "../data/entities.js";
import { listNotificationsSchema, createNotificationSchema } from "../data/validators.js";
import { toNotificationDto } from "../lib/notificationMapper.js";
import {
  NOTIFICATION_RESOURCE_KIND,
  notificationCrudErrorResponse,
  notificationValidationErrorResponse,
  resolveNotificationContext,
  runGuardedNotificationWrite
} from "../lib/routeHelpers.js";
import {
  buildNotificationsCrudOpenApi,
  createPagedListResponseSchema,
  notificationItemSchema
} from "./openapi.js";
const metadata = {
  GET: { requireAuth: true },
  POST: { requireAuth: true, requireFeatures: ["notifications.create"] }
};
async function GET(req) {
  const { ctx, scope } = await resolveNotificationContext(req);
  const em = ctx.container.resolve("em");
  const url = new URL(req.url);
  const queryParams = Object.fromEntries(url.searchParams.entries());
  const input = listNotificationsSchema.parse(queryParams);
  const filters = {
    recipientUserId: scope.userId,
    tenantId: scope.tenantId
  };
  if (input.status) {
    filters.status = Array.isArray(input.status) ? { $in: input.status } : input.status;
  } else {
    filters.status = { $ne: "dismissed" };
  }
  if (input.type) {
    filters.type = input.type;
  }
  if (input.severity) {
    filters.severity = input.severity;
  }
  if (input.sourceEntityType) {
    filters.sourceEntityType = input.sourceEntityType;
  }
  if (input.sourceEntityId) {
    filters.sourceEntityId = input.sourceEntityId;
  }
  if (input.since) {
    filters.createdAt = { $gt: new Date(input.since) };
  }
  const [notifications, total] = await Promise.all([
    em.find(Notification, filters, {
      orderBy: { createdAt: "desc" },
      limit: input.pageSize,
      offset: (input.page - 1) * input.pageSize
    }),
    em.count(Notification, filters)
  ]);
  const items = notifications.map(toNotificationDto);
  return Response.json({
    items,
    total,
    page: input.page,
    pageSize: input.pageSize,
    totalPages: Math.ceil(total / input.pageSize)
  });
}
async function POST(req) {
  const { service, scope, ctx } = await resolveNotificationContext(req);
  const body = await req.json().catch(() => ({}));
  const parsed = createNotificationSchema.safeParse(body);
  if (!parsed.success) {
    return notificationValidationErrorResponse(parsed.error);
  }
  try {
    const guarded = await runGuardedNotificationWrite(
      ctx.container,
      scope,
      req,
      {
        resourceKind: NOTIFICATION_RESOURCE_KIND,
        operation: "create",
        payload: parsed.data
      },
      () => service.create(parsed.data, scope)
    );
    if (!guarded.ok) return guarded.response;
    return Response.json({ id: guarded.result.id }, { status: 201 });
  } catch (error) {
    const errorResponse = notificationCrudErrorResponse(error);
    if (errorResponse) return errorResponse;
    throw error;
  }
}
const openApi = buildNotificationsCrudOpenApi({
  resourceName: "Notification",
  querySchema: listNotificationsSchema,
  listResponseSchema: createPagedListResponseSchema(notificationItemSchema),
  create: {
    schema: createNotificationSchema,
    responseSchema: z.object({ id: z.string().uuid() }),
    description: "Creates a notification for a user."
  }
});
export {
  GET,
  POST,
  metadata,
  openApi
};
//# sourceMappingURL=route.js.map
