import { createApiError } from "@inkeep/agents-core";
import { createMiddleware } from "hono/factory";
import { HTTPException } from "hono/http-exception";

//#region src/middleware/sessionAuth.ts
/**
* Middleware to enforce session-based authentication.
* Requires that a user has already been authenticated via Better Auth session.
* Used primarily for manage routes that require an active user session.
*/
const sessionAuth = () => createMiddleware(async (c, next) => {
	try {
		const user = c.get("user");
		if (!user) throw createApiError({
			code: "unauthorized",
			message: "Please log in to access this resource"
		});
		c.set("userId", user.id);
		c.set("userEmail", user.email);
		await next();
	} catch (error) {
		if (error instanceof HTTPException) throw error;
		throw createApiError({
			code: "unauthorized",
			message: "Authentication failed"
		});
	}
});
/**
* Global session middleware - sets user and session in context for all routes
* Used for all routes that require an active user session.
*/
const sessionContext = () => createMiddleware(async (c, next) => {
	const auth = c.get("auth");
	if (!auth) {
		c.set("user", null);
		c.set("session", null);
		await next();
		return;
	}
	const headers$1 = new Headers(c.req.raw.headers);
	const forwardedCookie = headers$1.get("x-forwarded-cookie");
	if (forwardedCookie && !headers$1.get("cookie")) headers$1.set("cookie", forwardedCookie);
	const session = await auth.api.getSession({ headers: headers$1 });
	if (!session) {
		c.set("user", null);
		c.set("session", null);
		await next();
		return;
	}
	c.set("user", session.user);
	c.set("session", session.session);
	await next();
});

//#endregion
export { sessionAuth, sessionContext };