import { NextRequest, NextResponse } from 'next/server'; import { withAuth } from '@/lib/api/helpers'; import { ALL_RANGES, getLeadSourceAttribution, getOccupancyTimeline, getPipelineFunnel, getRevenueBreakdown, type DateRange, type MetricBase, } from '@/lib/services/analytics.service'; const METRICS: Record Promise> = { pipeline_funnel: getPipelineFunnel, occupancy_timeline: getOccupancyTimeline, revenue_breakdown: getRevenueBreakdown, lead_source_attribution: getLeadSourceAttribution, }; export const GET = withAuth(async (req: NextRequest, ctx) => { const url = new URL(req.url); const metric = url.searchParams.get('metric') as MetricBase | null; const range = (url.searchParams.get('range') ?? '30d') as DateRange; if (!metric || !(metric in METRICS)) { return NextResponse.json({ error: 'Invalid or missing metric' }, { status: 400 }); } if (!ALL_RANGES.includes(range)) { return NextResponse.json({ error: 'Invalid range' }, { status: 400 }); } const data = await METRICS[metric](ctx.portId, range); return NextResponse.json({ metric, range, data }); });