import { db } from '@/lib/db';
import { getSession } from '@/lib/auth';
import { createResponse, handleApiError } from '@/src/lib/api';

export async function PATCH(req: Request, { params }: { params: Promise<{ id: string }> }) {
  try {
    const { id } = await params;
    const session = await getSession();

    const { status } = await req.json();

    await db.portfolioContributor.update({
      where: {
        userId_portfolioId: {
          portfolioId: id,
          userId: session?.user?.id ?? '',
        },
      },
      data: {
        status,
      },
    });

    return createResponse(200, 'Invitation status updated');
  } catch (error) {
    return handleApiError(error);
  }
}
