import { json } from '@sveltejs/kit';
import { findSimilarVerses } from '$lib/server/bible-embeddings';
import type { RequestHandler } from './$types';

export const POST: RequestHandler = async ({ request }) => {
	const formData = await request.json();
	const { sentence, topK = 10 }: { sentence: string; topK?: number } = formData;

	if (!sentence || typeof sentence !== 'string') {
		return json({ error: 'Invalid sentence' }, { status: 400 });
	}

	try {
		const results = await findSimilarVerses(sentence, topK);
		return json({ results });
	} catch (error) {
		console.error('Error finding similar verses:', error);
		return json({ error: 'Failed to find similar verses' }, { status: 500 });
	}
};