import { NextResponse } from 'next/server';

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

export const GET = withAuth(async (_req, ctx) => {
  try {
    const prefs = await notificationsService.getPreferences(ctx.userId, ctx.portId);
    return NextResponse.json({ data: prefs });
  } catch (error) {
    return errorResponse(error);
  }
});

export const PUT = withAuth(async (req, ctx) => {
  try {
    const body = await parseBody(req, updatePreferencesSchema);
    const prefs = await notificationsService.updatePreferences(ctx.userId, ctx.portId, body);
    return NextResponse.json({ data: prefs });
  } catch (error) {
    return errorResponse(error);
  }
});
