import { storedSkillIdPathParams } from './chunk-UIRQU4GW.js';
import { favoriteToggleResponseSchema } from './chunk-SFPN7SHZ.js';
import { requireBuilderFeature } from './chunk-M6GURVJO.js';
import { getCallerAuthorId, assertReadAccess } from './chunk-SXZS6VZ3.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-skill-favorites.ts
async function getFavoritesContext(mastra) {
  const storage = mastra.getStorage();
  if (!storage) {
    throw new HTTPException(500, { message: "Storage is not configured" });
  }
  const skillStore = await storage.getStore("skills");
  if (!skillStore) {
    throw new HTTPException(500, { message: "Skills 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 { skillStore, favoritesStore };
}
var FAVORITE_STORED_SKILL_ROUTE = createRoute({
  method: "PUT",
  path: "/stored/skills/:storedSkillId/favorite",
  responseType: "json",
  pathParamSchema: storedSkillIdPathParams,
  responseSchema: favoriteToggleResponseSchema,
  summary: "Favorite a stored skill",
  description: "Marks the stored skill as favorited by the calling user. Idempotent.",
  tags: ["Stored Skills"],
  requiresAuth: true,
  requiresPermission: "stored-skills:read",
  handler: async ({ mastra, requestContext, storedSkillId }) => {
    try {
      await requireBuilderFeature(mastra, "favorites");
      const callerId = getCallerAuthorId(requestContext);
      if (!callerId) {
        throw new HTTPException(401, { message: "Authentication required" });
      }
      const { skillStore, favoritesStore } = await getFavoritesContext(mastra);
      const skill = await skillStore.getByIdResolved(storedSkillId);
      if (!skill) {
        throw new HTTPException(404, { message: `Stored skill with id ${storedSkillId} not found` });
      }
      assertStoredResourceScope(skill, await getStoredResourceScope(mastra, requestContext));
      assertReadAccess({ requestContext, resource: "stored-skills", resourceId: storedSkillId, record: skill });
      const result = await favoritesStore.favorite({
        userId: callerId,
        entityType: "skill",
        entityId: storedSkillId
      });
      return result;
    } catch (error) {
      return handleError(error, "Error favoriting stored skill");
    }
  }
});
var UNFAVORITE_STORED_SKILL_ROUTE = createRoute({
  method: "DELETE",
  path: "/stored/skills/:storedSkillId/favorite",
  responseType: "json",
  pathParamSchema: storedSkillIdPathParams,
  responseSchema: favoriteToggleResponseSchema,
  summary: "Unfavorite a stored skill",
  description: "Removes the caller\u2019s favorite from the stored skill. Idempotent.",
  tags: ["Stored Skills"],
  requiresAuth: true,
  requiresPermission: "stored-skills:read",
  handler: async ({ mastra, requestContext, storedSkillId }) => {
    try {
      await requireBuilderFeature(mastra, "favorites");
      const callerId = getCallerAuthorId(requestContext);
      if (!callerId) {
        throw new HTTPException(401, { message: "Authentication required" });
      }
      const { skillStore, favoritesStore } = await getFavoritesContext(mastra);
      const skill = await skillStore.getByIdResolved(storedSkillId);
      if (!skill) {
        throw new HTTPException(404, { message: `Stored skill with id ${storedSkillId} not found` });
      }
      assertStoredResourceScope(skill, await getStoredResourceScope(mastra, requestContext));
      assertReadAccess({ requestContext, resource: "stored-skills", resourceId: storedSkillId, record: skill });
      const result = await favoritesStore.unfavorite({
        userId: callerId,
        entityType: "skill",
        entityId: storedSkillId
      });
      return result;
    } catch (error) {
      return handleError(error, "Error unfavoriting stored skill");
    }
  }
});

export { FAVORITE_STORED_SKILL_ROUTE, UNFAVORITE_STORED_SKILL_ROUTE };
//# sourceMappingURL=chunk-P5NXUYGE.js.map
//# sourceMappingURL=chunk-P5NXUYGE.js.map