import { NextResponse } from 'next/server';

import { withAuth, withPermission } from '@/lib/api/helpers';
import { errorResponse } from '@/lib/errors';
import { listClientOptions } from '@/lib/services/clients.service';

export const GET = withAuth(
  withPermission('clients', 'view', async (req, ctx) => {
    try {
      const search = req.nextUrl.searchParams.get('search') ?? undefined;
      const data = await listClientOptions(ctx.portId, search);
      return NextResponse.json({ data });
    } catch (error) {
      return errorResponse(error);
    }
  }),
);
