import type { PageServerLoad } from './$types';
import { Report, User } from '$lib/server/database';
import { redirect } from '@sveltejs/kit';
import { env } from '$env/dynamic/public';

export const load: PageServerLoad = async ({ params }) => {
	const report = await Report.findOne({
		where: { url_hash: params.url_hash },
		include: [
			{ model: User, as: 'reporter' },
			{ model: User, as: 'reported' }
		]
	});

	if (report == null) throw redirect(302, `${env.PUBLIC_BASE_PATH}/`);

	return {
		draft: report.draft,
		status: report.status,
		reason: report.subject,
		reporter: {
			name: report.reporter.username
		},
		reported: {
			name: report.reported.username
		}
	};
};
