import { NextRequest, NextResponse } from 'next/server' import { auth } from '@/auth' import { prisma } from '@/lib/prisma' import { semanticSearchService } from '@/lib/ai/services/semantic-search.service' export async function POST(req: NextRequest) { try { const session = await auth() if (!session?.user?.id) { return NextResponse.json({ success: false, error: 'Unauthorized' }, { status: 401 }) } const userId = session.user.id const notes = await prisma.note.findMany({ where: { userId, trashedAt: null }, select: { id: true } }) let processedCount = 0 let failedCount = 0 const BATCH_SIZE = 20 for (let i = 0; i < notes.length; i += BATCH_SIZE) { const batch = notes.slice(i, i + BATCH_SIZE) const results = await Promise.allSettled( batch.map(note => semanticSearchService.indexNote(note.id, { force: true })) ) for (const r of results) { if (r.status === 'fulfilled') processedCount++ else failedCount++ } } return NextResponse.json({ success: true, count: processedCount, failed: failedCount, total: notes.length }) } catch (error) { console.error('Reindex error:', error) return NextResponse.json( { success: false, error: 'Failed to reindex notes' }, { status: 500 } ) } }