import { json } from '@sveltejs/kit';
import type { RequestHandler } from './$types';
import { getVerseForDate } from '$lib/server/daily-verse';
import { getBookById } from '$lib/server/bible';

export const POST: RequestHandler = async ({ request }) => {
	const body = await request.json();
	const { date } = body;

	if (!date || !/^\d{4}-\d{2}-\d{2}$/.test(date)) {
		return json({ error: 'A valid date (YYYY-MM-DD) is required' }, { status: 400 });
	}

	const dateStr = date;

	const dailyVerse = await getVerseForDate(dateStr);
	const correctBook = getBookById(dailyVerse.bookId) ?? null;

	return json({
		dailyVerse,
		correctBookId: dailyVerse.bookId,
		correctBook,
	});
};
