import { checkPermissions } from "$lib/permissions";
import { redirect } from "@sveltejs/kit";
import type { PageServerLoad } from "./$types";

export const load: PageServerLoad = async ({ locals, parent }) => {
  const accessToken = locals.session?.accessToken;
  if (!accessToken) {
    throw redirect(303, "/login");
  }

  const parentData = await parent();
  const permissions = parentData?.permissions ?? {};

  // Gate access to reports
  if (permissions["ui.navigation.reports.view"] === false) {
    throw redirect(303, "/admin");
  }

  return {
    accessToken,
  };
};
