import { NextResponse } from 'next/server';
import { withErrorHandler, RouteContext } from '@server/middleware/withErrorHandler';
import { withAuth, AuthedRequest } from '@server/middleware/withAuth';
import { withValidationAuthed } from '@server/middleware/withValidation';
import { markReadSchema } from '@server/validators/notifications.validator';
import { markNotificationRead, deleteNotification } from '@server/services/notifications.service';

export const PATCH = withErrorHandler(
  withAuth(
    withValidationAuthed(markReadSchema, async (req, ctx: RouteContext) => {
      const { id } = await ctx.params;
      const { restaurantId, userId, role } = req.session;
      const notification = await markNotificationRead(id, restaurantId!, role === 'admin' ? undefined : userId);
      return NextResponse.json({ notification });
    })
  )
);

export const DELETE = withErrorHandler(
  withAuth(async (req: AuthedRequest, ctx: RouteContext) => {
    const { id } = await ctx.params;
    const { restaurantId, userId, role } = req.session;
    await deleteNotification(id, restaurantId!, role === 'admin' ? undefined : userId);
    return NextResponse.json({ success: true });
  })
);
