import { requireAdmin } from '@/lib/auth/require-admin' import { createBulkIncidents, type ExtractedIncidentMetadata } from '@/lib/incidents/bulk-import' import { NextRequest, NextResponse } from 'next/server' export async function POST(request: NextRequest) { const { session } = await requireAdmin() if (!session?.sub) return new NextResponse('Unauthorized', { status: 401 }) const userId = session.sub try { const body = await request.json() const { folders } = body as { folders: Array<{ id: string folder_path?: string description: string date: string location: { site: string; zone: string } incident_type?: string priority?: string }> } const metadata: ExtractedIncidentMetadata[] = folders.map((f) => ({ description: f.description, date: f.date, location: f.location, incident_type: f.incident_type as 'env' | 'asset' | 'security' | 'fire' | undefined, priority: f.priority as 'low' | 'med' | 'high' | undefined })) // Create incidents with user ID const created = await createBulkIncidents(metadata, userId, []) return NextResponse.json({ status: 'success', created_incidents: created, total_files_imported: 0 }) } catch (error) { console.error('Batch creation failed:', error) return NextResponse.json( { error: 'Batch creation failed', details: String(error) }, { status: 500 } ) } }