import { favoriteToggleResponseSchema } from './chunk-SFPN7SHZ.js';
import { requireBuilderFeature } from './chunk-M6GURVJO.js';
import { getCallerAuthorId, assertReadAccess } from './chunk-SXZS6VZ3.js';
import { storedAgentIdPathParams } from './chunk-SPVKRK56.js';
import { assertStoredResourceScope, getStoredResourceScope } from './chunk-QIQWHDPH.js';
import { handleError } from './chunk-7ZWJX3AN.js';
import { createRoute } from './chunk-PIKPS6Q6.js';
import { HTTPException } from './chunk-6QWQZI4Q.js';

// src/server/handlers/stored-agent-favorites.ts
async function getFavoritesContext(mastra) {
  const storage = mastra.getStorage();
  if (!storage) {
    throw new HTTPException(500, { message: "Storage is not configured" });
  }
  const agentStore = await storage.getStore("agents");
  if (!agentStore) {
    throw new HTTPException(500, { message: "Agents storage domain is not available" });
  }
  const favoritesStore = await storage.getStore("favorites");
  if (!favoritesStore) {
    throw new HTTPException(500, { message: "Favorites storage domain is not available" });
  }
  return { agentStore, favoritesStore };
}
var FAVORITE_STORED_AGENT_ROUTE = createRoute({
  method: "PUT",
  path: "/stored/agents/:storedAgentId/favorite",
  responseType: "json",
  pathParamSchema: storedAgentIdPathParams,
  responseSchema: favoriteToggleResponseSchema,
  summary: "Favorite a stored agent",
  description: "Marks the stored agent as favorited by the calling user. Idempotent.",
  tags: ["Stored Agents"],
  requiresAuth: true,
  requiresPermission: "stored-agents:read",
  handler: async ({ mastra, requestContext, storedAgentId }) => {
    try {
      await requireBuilderFeature(mastra, "favorites");
      const callerId = getCallerAuthorId(requestContext);
      if (!callerId) {
        throw new HTTPException(401, { message: "Authentication required" });
      }
      const { agentStore, favoritesStore } = await getFavoritesContext(mastra);
      const agent = await agentStore.getById(storedAgentId);
      if (!agent) {
        throw new HTTPException(404, { message: `Stored agent with id ${storedAgentId} not found` });
      }
      assertStoredResourceScope(agent, await getStoredResourceScope(mastra, requestContext));
      assertReadAccess({ requestContext, resource: "stored-agents", resourceId: storedAgentId, record: agent });
      const result = await favoritesStore.favorite({
        userId: callerId,
        entityType: "agent",
        entityId: storedAgentId
      });
      return result;
    } catch (error) {
      return handleError(error, "Error favoriting stored agent");
    }
  }
});
var UNFAVORITE_STORED_AGENT_ROUTE = createRoute({
  method: "DELETE",
  path: "/stored/agents/:storedAgentId/favorite",
  responseType: "json",
  pathParamSchema: storedAgentIdPathParams,
  responseSchema: favoriteToggleResponseSchema,
  summary: "Unfavorite a stored agent",
  description: "Removes the caller\u2019s favorite from the stored agent. Idempotent.",
  tags: ["Stored Agents"],
  requiresAuth: true,
  requiresPermission: "stored-agents:read",
  handler: async ({ mastra, requestContext, storedAgentId }) => {
    try {
      await requireBuilderFeature(mastra, "favorites");
      const callerId = getCallerAuthorId(requestContext);
      if (!callerId) {
        throw new HTTPException(401, { message: "Authentication required" });
      }
      const { agentStore, favoritesStore } = await getFavoritesContext(mastra);
      const agent = await agentStore.getById(storedAgentId);
      if (!agent) {
        throw new HTTPException(404, { message: `Stored agent with id ${storedAgentId} not found` });
      }
      assertStoredResourceScope(agent, await getStoredResourceScope(mastra, requestContext));
      assertReadAccess({ requestContext, resource: "stored-agents", resourceId: storedAgentId, record: agent });
      const result = await favoritesStore.unfavorite({
        userId: callerId,
        entityType: "agent",
        entityId: storedAgentId
      });
      return result;
    } catch (error) {
      return handleError(error, "Error unfavoriting stored agent");
    }
  }
});

export { FAVORITE_STORED_AGENT_ROUTE, UNFAVORITE_STORED_AGENT_ROUTE };
//# sourceMappingURL=chunk-Z5YBWU6K.js.map
//# sourceMappingURL=chunk-Z5YBWU6K.js.map