import type { NextApiRequest, NextApiResponse } from "next" import sqlite3 from "sqlite3" import path from "path" interface BulkImportItem { domain: string risk_level: 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" }) } const dbPath = path.join(process.cwd(), "database", "antihoax.db") const db = new sqlite3.Database(dbPath) try { let imported = 0 let skipped = 0 for (const source of sources) { if (!source.domain || !source.risk_level) { skipped++ continue } // Check if domain already exists const existing = await new Promise((resolve, reject) => { db.get( "SELECT id FROM sources WHERE domain = ?", [source.domain], (err, row) => { if (err) reject(err) else resolve(row) } ) }) if (existing) { skipped++ continue } // Insert new source await new Promise((resolve, reject) => { db.run( `INSERT INTO sources (domain, title, risk_level, status, description, created_at) VALUES (?, ?, ?, 'verified', ?, datetime('now'))`, [source.domain, source.domain, source.risk_level, source.description || ''], function(err) { if (err) reject(err) else resolve() } ) }) imported++ } res.json({ success: true, imported, skipped, total: sources.length }) } catch (error) { console.error('Bulk import error:', error) res.status(500).json({ error: "Import failed" }) } finally { db.close() } }