import type { NextApiRequest, NextApiResponse } from "next" import { db, schema } from "../../../lib/db/connection" import { eq } from "drizzle-orm" interface BulkImportItem { domain: string riskLevel: number categories: string[] description?: string } export default async function handler(req: NextApiRequest, res: NextApiResponse) { if (req.method !== "POST") return res.status(405).json({ error: "Method not allowed" }) const { sources } = req.body as { sources: BulkImportItem[] } if (!sources || !Array.isArray(sources)) { return res.status(400).json({ error: "Sources array required" }) } try { let imported = 0 let skipped = 0 for (const source of sources) { if (!source.domain || !source.riskLevel) { skipped++ continue } // Check if source already exists const existing = await db.select() .from(schema.sources) .where(eq(schema.sources.domain, source.domain)) .limit(1) if (existing.length > 0) { skipped++ continue } // Insert new source try { const url = `https://${source.domain}` await db.insert(schema.sources).values({ url, domain: source.domain, title: source.domain, description: source.description || `Imported source: ${source.domain}`, type: 'website', status: 'pending', riskLevel: source.riskLevel, language: 'sk', reportedBy: 'bulk-import' }) imported++ } catch (error) { console.error('Failed to import source:', source.domain, error) skipped++ } } res.json({ success: true, imported, skipped, message: `Imported ${imported} sources, skipped ${skipped}` }) } catch (error: any) { console.error('Bulk import error:', error) res.status(500).json({ error: "Import failed" }) } }