import { NextResponse } from 'next/server';

import { errorResponse, NotFoundError } from '@/lib/errors';
import { logger } from '@/lib/logger';
import { withPortalAuth } from '@/lib/portal/helpers';
import { getPortalDashboard } from '@/lib/services/portal.service';

export const GET = withPortalAuth(async (_req, session) => {
  try {
    const dashboard = await getPortalDashboard(session.clientId, session.portId);
    if (!dashboard) throw new NotFoundError('client');

    return NextResponse.json({ data: dashboard });
  } catch (error) {
    logger.error({ err: error }, 'Portal dashboard fetch failed');
    return errorResponse(error);
  }
});
