import { createMiddleware } from "@tanstack/react-start";
import { getRequestHeaders } from "@tanstack/react-start/server";
import { authClient } from "../lib/auth-client";
import { authMiddleware } from "./auth";

export const dashboardMiddleware = createMiddleware()
	.middleware([authMiddleware])
	.server(async ({ next, context }) => {
		const hasPermissions = await authClient.admin.hasPermission({
			fetchOptions: {
				headers: getRequestHeaders(),
			},
			permissions: {
				dashboard: ["access"],
			},
		});
		return await next({
			context: {
				...context,
				hasPermissions: hasPermissions.data?.success ?? false,
			},
		});
	});
