import { error } from '@sveltejs/kit';
import { GetTeamReportStore } from '$houdini';
import type { PageServerLoad } from './$types';

export const load: PageServerLoad = async (event) => {
	const { params, locals, parent } = event;
	const reportId = params.report;
	const fetchParams = { event, metadata: { cookie: locals.cookie } };

	// Get the lookups from parent layout data
	const parentData = await parent();

	// Fetch report details
	const reportResult = await new GetTeamReportStore().fetch({
		...fetchParams,
		variables: { id: reportId }
	});

	const report = reportResult.data?.report;
	if (!report) {
		throw error(404, 'Report not found');
	}

	return {
		report,
		accountLookup: parentData.accountLookup,
		customerLookup: parentData.customerLookup
	};
};
