import { NextResponse } from 'next/server';

import { type RouteHandler } from '@/lib/api/helpers';
import { errorResponse } from '@/lib/errors';
import { setPrimary } from '@/lib/services/company-memberships.service';

export const setPrimaryHandler: RouteHandler = async (_req, ctx, params) => {
  try {
    const membership = await setPrimary(params.mid!, ctx.portId, {
      userId: ctx.userId,
      portId: ctx.portId,
      ipAddress: ctx.ipAddress,
      userAgent: ctx.userAgent,
    });
    return NextResponse.json({ data: membership });
  } catch (error) {
    return errorResponse(error);
  }
};
