import { NextResponse } from 'next/server';

import { withAuth, withPermission } from '@/lib/api/helpers';
import { parseBody } from '@/lib/api/route-helpers';
import { snoozeReminder } from '@/lib/services/reminders.service';
import { snoozeReminderSchema } from '@/lib/validators/reminders';
import { errorResponse } from '@/lib/errors';

export const POST = withAuth(
  withPermission('reminders', 'edit_own', async (req, ctx, params) => {
    try {
      const body = await parseBody(req, snoozeReminderSchema);
      const data = await snoozeReminder(params.id!, ctx.portId, body, {
        userId: ctx.userId,
        portId: ctx.portId,
        ipAddress: ctx.ipAddress,
        userAgent: ctx.userAgent,
      });
      return NextResponse.json({ data });
    } catch (error) {
      return errorResponse(error);
    }
  }),
);
