import { NextRequest, NextResponse } from 'next/server';

import { withAuth, withPermission } from '@/lib/api/helpers';
import { getRevenueForecast } from '@/lib/services/tenancy-reports.service';
import { assertTenanciesModuleEnabled } from '@/lib/services/tenancies-module.service';

export const GET = withAuth(
  withPermission('reports', 'view_dashboard', async (req: NextRequest, ctx) => {
    await assertTenanciesModuleEnabled(ctx.portId);
    const url = new URL(req.url);
    const horizonQuarters = Number(url.searchParams.get('horizonQuarters') ?? 8);
    const data = await getRevenueForecast(ctx.portId, { horizonQuarters });
    return NextResponse.json({ data });
  }),
);
