import { NextResponse } from 'next/server';

import { withAuth, withPermission } from '@/lib/api/helpers';
import { parseBody } from '@/lib/api/route-helpers';
import { errorResponse } from '@/lib/errors';
import { remove, update } from '@/lib/services/interest-contact-log.service';
import { updateContactLogSchema } from '@/lib/validators/interest-contact-log';

export const PATCH = withAuth(
  withPermission('interests', 'edit', async (req, ctx, params) => {
    try {
      const body = await parseBody(req, updateContactLogSchema);
      const entry = await update(params.id!, ctx.portId, ctx.userId, {
        occurredAt: body.occurredAt,
        channel: body.channel,
        direction: body.direction,
        summary: body.summary,
        voiceTranscript: body.voiceTranscript,
        templateUsed: body.templateUsed,
        followUpAt: body.followUpAt,
      });
      return NextResponse.json({ data: entry });
    } catch (error) {
      return errorResponse(error);
    }
  }),
);

export const DELETE = withAuth(
  withPermission('interests', 'edit', async (_req, ctx, params) => {
    try {
      await remove(params.id!, ctx.portId);
      return new NextResponse(null, { status: 204 });
    } catch (error) {
      return errorResponse(error);
    }
  }),
);
