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

import { withAuth, withPermission } from '@/lib/api/helpers';
import { getOccupancyHeatmap } 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 from = url.searchParams.get('from');
    const to = url.searchParams.get('to');
    const now = new Date();
    const range = {
      from: from ? new Date(from) : new Date(now.getFullYear(), now.getMonth() - 11, 1),
      to: to ? new Date(to) : now,
    };
    const data = await getOccupancyHeatmap(ctx.portId, range);
    return NextResponse.json({ data });
  }),
);
