import { OpenAPIHono, createRoute, z } from "@hono/zod-openapi";
import { OrgRoles, SpiceDbProjectPermissions, SpiceDbResourceTypes, checkBulkPermissions, commonGetErrorResponses, createApiError } from "@inkeep/agents-core";

//#region src/domains/manage/routes/projectPermissions.ts
const app = new OpenAPIHono();
const ProjectPermissionsParamsSchema = z.object({
	tenantId: z.string(),
	projectId: z.string()
});
const ProjectPermissionsResponseSchema = z.object({ data: z.object({
	canView: z.boolean(),
	canUse: z.boolean(),
	canEdit: z.boolean()
}) });
app.openapi(createRoute({
	method: "get",
	path: "/",
	summary: "Get Project Permissions",
	description: "Get the current user's permissions for a project. Returns which actions the user can perform.",
	operationId: "get-project-permissions",
	tags: ["Project Permissions"],
	request: { params: ProjectPermissionsParamsSchema },
	responses: {
		200: {
			description: "Project permissions for the current user",
			content: { "application/json": { schema: ProjectPermissionsResponseSchema } }
		},
		...commonGetErrorResponses
	}
}), async (c) => {
	const { projectId } = c.req.valid("param");
	const userId = c.get("userId");
	const tenantRole = c.get("tenantRole");
	if (process.env.ENVIRONMENT === "test") return c.json({ data: {
		canView: true,
		canUse: true,
		canEdit: true
	} });
	if (tenantRole === OrgRoles.OWNER || tenantRole === OrgRoles.ADMIN) return c.json({ data: {
		canView: true,
		canUse: true,
		canEdit: true
	} });
	if (!userId) throw createApiError({
		code: "unauthorized",
		message: "User not found"
	});
	const permissions = await checkBulkPermissions({
		resourceType: SpiceDbResourceTypes.PROJECT,
		resourceId: projectId,
		permissions: [
			SpiceDbProjectPermissions.VIEW,
			SpiceDbProjectPermissions.USE,
			SpiceDbProjectPermissions.EDIT
		],
		subjectType: SpiceDbResourceTypes.USER,
		subjectId: userId
	});
	return c.json({ data: {
		canView: permissions[SpiceDbProjectPermissions.VIEW] ?? false,
		canUse: permissions[SpiceDbProjectPermissions.USE] ?? false,
		canEdit: permissions[SpiceDbProjectPermissions.EDIT] ?? false
	} });
});
var projectPermissions_default = app;

//#endregion
export { projectPermissions_default as default };