import runDbClient_default from "../data/db/runDbClient.js";
import { createApiError, withEntitlementLock } from "@inkeep/agents-core";
import { registerEntitlementMeta } from "@inkeep/agents-core/middleware";
import { createMiddleware } from "hono/factory";
import { HTTPException } from "hono/http-exception";

//#region src/middleware/requireEntitlement.ts
const requireEntitlement = (config) => {
	const { resourceType, countFn, label } = config;
	const displayLabel = label ?? resourceType;
	const mw = createMiddleware(async (c, next) => {
		const tenantId = c.get("tenantId");
		if (!tenantId) {
			await next();
			return;
		}
		try {
			await withEntitlementLock(runDbClient_default, tenantId, resourceType, async (limit, _tx) => {
				if (limit === null) {
					await next();
					return;
				}
				const current = await countFn(tenantId);
				if (current >= limit) throw createApiError({
					code: "payment_required",
					message: `${displayLabel} limit reached (${current}/${limit})`,
					instance: c.req.path,
					extensions: {
						resourceType,
						current,
						limit
					}
				});
				await next();
			});
		} catch (error) {
			if (error instanceof HTTPException) throw error;
			const errorMessage = error instanceof Error ? error.message : "Unknown error";
			throw createApiError({
				code: "internal_server_error",
				message: "Failed to verify entitlement",
				instance: c.req.path,
				extensions: {
					resourceType,
					internalError: errorMessage
				}
			});
		}
	});
	registerEntitlementMeta(mw, {
		resourceType,
		description: `Subject to ${resourceType} limit when configured for the organization`
	});
	return mw;
};

//#endregion
export { requireEntitlement };