import type { RequestHandler } from '@sveltejs/kit';
import { Report, User } from '$lib/server/database';

export const POST = (async ({ request, params }) => {
	const report = await Report.findOne({ where: { url_hash: params.url_hash } });

	if (report == null) return new Response(null, { status: 400 });

	const data: { reported: string | null; subject: string; body: string } = await request.json();

	const user =
		data.reported != null ? await User.findOne({ where: { username: data.reported } }) : undefined;
	if (user === null) {
		return new Response(null, { status: 400 });
	}

	report.draft = false;
	report.subject = data.subject ?? '';
	report.body = data.body ?? '';
	report.reported_id = user?.id;
	await report.save();

	return new Response(null, { status: 200 });
}) satisfies RequestHandler;
