import { listScorerVersionsResponseSchema, listScorerVersionsQuerySchema, scorerVersionPathParams, createScorerVersionResponseSchema, createScorerVersionBodySchema, getScorerVersionResponseSchema, scorerVersionIdPathParams, activateScorerVersionResponseSchema, restoreScorerVersionResponseSchema, deleteScorerVersionResponseSchema, compareScorerVersionsResponseSchema, compareScorerVersionsQuerySchema } from './chunk-VK3T6KKQ.js';
import { extractConfigFromVersion, calculateChangedFields, createVersionWithRetry, enforceRetentionLimit, computeVersionDiffs } from './chunk-33QPJPK4.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/scorer-versions.ts
var SNAPSHOT_CONFIG_FIELDS = [
  "name",
  "description",
  "type",
  "model",
  "instructions",
  "scoreRange",
  "presetConfig",
  "defaultSampling"
];
var LIST_SCORER_VERSIONS_ROUTE = createRoute({
  method: "GET",
  path: "/stored/scorers/:scorerId/versions",
  requiresAuth: true,
  responseType: "json",
  pathParamSchema: scorerVersionPathParams,
  queryParamSchema: listScorerVersionsQuerySchema,
  responseSchema: listScorerVersionsResponseSchema,
  summary: "List scorer versions",
  description: "Returns a paginated list of all versions for a stored scorer",
  tags: ["Scorer Versions"],
  handler: async ({ mastra, scorerId, page, perPage, orderBy, requestContext }) => {
    try {
      const storage = mastra.getStorage();
      if (!storage) {
        throw new HTTPException(500, { message: "Storage is not configured" });
      }
      const scorerStore = await storage.getStore("scorerDefinitions");
      if (!scorerStore) {
        throw new HTTPException(500, { message: "Scorer definitions storage domain is not available" });
      }
      const scorer = await scorerStore.getById(scorerId);
      if (!scorer) {
        throw new HTTPException(404, { message: `Scorer with id ${scorerId} not found` });
      }
      assertStoredResourceScope(scorer, await getStoredResourceScope(mastra, requestContext));
      const result = await scorerStore.listVersions({
        scorerDefinitionId: scorerId,
        page,
        perPage,
        orderBy
      });
      return result;
    } catch (error) {
      return handleError(error, "Error listing scorer versions");
    }
  }
});
var CREATE_SCORER_VERSION_ROUTE = createRoute({
  method: "POST",
  path: "/stored/scorers/:scorerId/versions",
  requiresAuth: true,
  responseType: "json",
  pathParamSchema: scorerVersionPathParams,
  bodySchema: createScorerVersionBodySchema,
  responseSchema: createScorerVersionResponseSchema,
  summary: "Create scorer version",
  description: "Creates a new version snapshot of the current scorer configuration",
  tags: ["Scorer Versions"],
  handler: async ({ mastra, scorerId, changeMessage, requestContext }) => {
    try {
      const storage = mastra.getStorage();
      if (!storage) {
        throw new HTTPException(500, { message: "Storage is not configured" });
      }
      const scorerStore = await storage.getStore("scorerDefinitions");
      if (!scorerStore) {
        throw new HTTPException(500, { message: "Scorer definitions storage domain is not available" });
      }
      const scorer = await scorerStore.getById(scorerId);
      if (!scorer) {
        throw new HTTPException(404, { message: `Scorer with id ${scorerId} not found` });
      }
      assertStoredResourceScope(scorer, await getStoredResourceScope(mastra, requestContext));
      let currentConfig = {};
      if (scorer.activeVersionId) {
        const activeVersion = await scorerStore.getVersion(scorer.activeVersionId);
        if (activeVersion) {
          currentConfig = extractConfigFromVersion(
            activeVersion,
            SNAPSHOT_CONFIG_FIELDS
          );
        }
      }
      const latestVersion = await scorerStore.getLatestVersion(scorerId);
      if (!scorer.activeVersionId && latestVersion) {
        currentConfig = extractConfigFromVersion(
          latestVersion,
          SNAPSHOT_CONFIG_FIELDS
        );
      }
      const previousConfig = latestVersion ? extractConfigFromVersion(latestVersion, SNAPSHOT_CONFIG_FIELDS) : null;
      const changedFields = calculateChangedFields(previousConfig, currentConfig);
      const { versionId } = await createVersionWithRetry(
        scorerStore,
        scorerId,
        "scorerDefinitionId",
        currentConfig,
        changedFields,
        { changeMessage }
      );
      const version = await scorerStore.getVersion(versionId);
      if (!version) {
        throw new HTTPException(500, { message: "Failed to retrieve created version" });
      }
      await enforceRetentionLimit(
        scorerStore,
        scorerId,
        "scorerDefinitionId",
        scorer.activeVersionId
      );
      return version;
    } catch (error) {
      return handleError(error, "Error creating scorer version");
    }
  }
});
var GET_SCORER_VERSION_ROUTE = createRoute({
  method: "GET",
  path: "/stored/scorers/:scorerId/versions/:versionId",
  requiresAuth: true,
  responseType: "json",
  pathParamSchema: scorerVersionIdPathParams,
  responseSchema: getScorerVersionResponseSchema,
  summary: "Get scorer version",
  description: "Returns a specific version of a scorer by its version ID",
  tags: ["Scorer Versions"],
  handler: async ({ mastra, scorerId, versionId, requestContext }) => {
    try {
      const storage = mastra.getStorage();
      if (!storage) {
        throw new HTTPException(500, { message: "Storage is not configured" });
      }
      const scorerStore = await storage.getStore("scorerDefinitions");
      if (!scorerStore) {
        throw new HTTPException(500, { message: "Scorer definitions storage domain is not available" });
      }
      const version = await scorerStore.getVersion(versionId);
      if (!version) {
        throw new HTTPException(404, { message: `Version with id ${versionId} not found` });
      }
      if (version.scorerDefinitionId !== scorerId) {
        throw new HTTPException(404, {
          message: `Version with id ${versionId} not found for scorer ${scorerId}`
        });
      }
      const scorer = await scorerStore.getById(scorerId);
      assertStoredResourceScope(scorer, await getStoredResourceScope(mastra, requestContext));
      return version;
    } catch (error) {
      return handleError(error, "Error getting scorer version");
    }
  }
});
var ACTIVATE_SCORER_VERSION_ROUTE = createRoute({
  method: "POST",
  path: "/stored/scorers/:scorerId/versions/:versionId/activate",
  requiresAuth: true,
  responseType: "json",
  pathParamSchema: scorerVersionIdPathParams,
  responseSchema: activateScorerVersionResponseSchema,
  summary: "Activate scorer version",
  description: "Sets a specific version as the active version for the scorer",
  tags: ["Scorer Versions"],
  handler: async ({ mastra, scorerId, versionId, requestContext }) => {
    try {
      const storage = mastra.getStorage();
      if (!storage) {
        throw new HTTPException(500, { message: "Storage is not configured" });
      }
      const scorerStore = await storage.getStore("scorerDefinitions");
      if (!scorerStore) {
        throw new HTTPException(500, { message: "Scorer definitions storage domain is not available" });
      }
      const scorer = await scorerStore.getById(scorerId);
      if (!scorer) {
        throw new HTTPException(404, { message: `Scorer with id ${scorerId} not found` });
      }
      assertStoredResourceScope(scorer, await getStoredResourceScope(mastra, requestContext));
      const version = await scorerStore.getVersion(versionId);
      if (!version) {
        throw new HTTPException(404, { message: `Version with id ${versionId} not found` });
      }
      if (version.scorerDefinitionId !== scorerId) {
        throw new HTTPException(404, {
          message: `Version with id ${versionId} not found for scorer ${scorerId}`
        });
      }
      await scorerStore.update({
        id: scorerId,
        activeVersionId: versionId,
        status: "published"
      });
      mastra.getEditor()?.scorer.clearCache(scorerId);
      return {
        success: true,
        message: `Version ${version.versionNumber} is now active`,
        activeVersionId: versionId
      };
    } catch (error) {
      return handleError(error, "Error activating scorer version");
    }
  }
});
var RESTORE_SCORER_VERSION_ROUTE = createRoute({
  method: "POST",
  path: "/stored/scorers/:scorerId/versions/:versionId/restore",
  requiresAuth: true,
  responseType: "json",
  pathParamSchema: scorerVersionIdPathParams,
  responseSchema: restoreScorerVersionResponseSchema,
  summary: "Restore scorer version",
  description: "Restores the scorer configuration from a version, creating a new version",
  tags: ["Scorer Versions"],
  handler: async ({ mastra, scorerId, versionId, requestContext }) => {
    try {
      const storage = mastra.getStorage();
      if (!storage) {
        throw new HTTPException(500, { message: "Storage is not configured" });
      }
      const scorerStore = await storage.getStore("scorerDefinitions");
      if (!scorerStore) {
        throw new HTTPException(500, { message: "Scorer definitions storage domain is not available" });
      }
      const scorer = await scorerStore.getById(scorerId);
      if (!scorer) {
        throw new HTTPException(404, { message: `Scorer with id ${scorerId} not found` });
      }
      assertStoredResourceScope(scorer, await getStoredResourceScope(mastra, requestContext));
      const versionToRestore = await scorerStore.getVersion(versionId);
      if (!versionToRestore) {
        throw new HTTPException(404, { message: `Version with id ${versionId} not found` });
      }
      if (versionToRestore.scorerDefinitionId !== scorerId) {
        throw new HTTPException(404, {
          message: `Version with id ${versionId} not found for scorer ${scorerId}`
        });
      }
      const restoredConfig = extractConfigFromVersion(
        versionToRestore,
        SNAPSHOT_CONFIG_FIELDS
      );
      await scorerStore.update({
        id: scorerId,
        ...restoredConfig
      });
      const latestVersion = await scorerStore.getLatestVersion(scorerId);
      const previousConfig = latestVersion ? extractConfigFromVersion(latestVersion, SNAPSHOT_CONFIG_FIELDS) : null;
      const changedFields = calculateChangedFields(previousConfig, restoredConfig);
      const { versionId: newVersionId } = await createVersionWithRetry(
        scorerStore,
        scorerId,
        "scorerDefinitionId",
        restoredConfig,
        changedFields,
        {
          changeMessage: `Restored from version ${versionToRestore.versionNumber}`
        }
      );
      const newVersion = await scorerStore.getVersion(newVersionId);
      if (!newVersion) {
        throw new HTTPException(500, { message: "Failed to retrieve created version" });
      }
      await enforceRetentionLimit(
        scorerStore,
        scorerId,
        "scorerDefinitionId",
        scorer.activeVersionId
      );
      mastra.getEditor()?.scorer.clearCache(scorerId);
      return newVersion;
    } catch (error) {
      return handleError(error, "Error restoring scorer version");
    }
  }
});
var DELETE_SCORER_VERSION_ROUTE = createRoute({
  method: "DELETE",
  path: "/stored/scorers/:scorerId/versions/:versionId",
  requiresAuth: true,
  responseType: "json",
  pathParamSchema: scorerVersionIdPathParams,
  responseSchema: deleteScorerVersionResponseSchema,
  summary: "Delete scorer version",
  description: "Deletes a specific version (cannot delete the active version)",
  tags: ["Scorer Versions"],
  handler: async ({ mastra, scorerId, versionId, requestContext }) => {
    try {
      const storage = mastra.getStorage();
      if (!storage) {
        throw new HTTPException(500, { message: "Storage is not configured" });
      }
      const scorerStore = await storage.getStore("scorerDefinitions");
      if (!scorerStore) {
        throw new HTTPException(500, { message: "Scorer definitions storage domain is not available" });
      }
      const scorer = await scorerStore.getById(scorerId);
      if (!scorer) {
        throw new HTTPException(404, { message: `Scorer with id ${scorerId} not found` });
      }
      assertStoredResourceScope(scorer, await getStoredResourceScope(mastra, requestContext));
      const version = await scorerStore.getVersion(versionId);
      if (!version) {
        throw new HTTPException(404, { message: `Version with id ${versionId} not found` });
      }
      if (version.scorerDefinitionId !== scorerId) {
        throw new HTTPException(404, {
          message: `Version with id ${versionId} not found for scorer ${scorerId}`
        });
      }
      if (scorer.activeVersionId === versionId) {
        throw new HTTPException(400, {
          message: "Cannot delete the active version. Activate a different version first."
        });
      }
      await scorerStore.deleteVersion(versionId);
      mastra.getEditor()?.scorer.clearCache(scorerId);
      return {
        success: true,
        message: `Version ${version.versionNumber} deleted successfully`
      };
    } catch (error) {
      return handleError(error, "Error deleting scorer version");
    }
  }
});
var COMPARE_SCORER_VERSIONS_ROUTE = createRoute({
  method: "GET",
  path: "/stored/scorers/:scorerId/versions/compare",
  requiresAuth: true,
  responseType: "json",
  pathParamSchema: scorerVersionPathParams,
  queryParamSchema: compareScorerVersionsQuerySchema,
  responseSchema: compareScorerVersionsResponseSchema,
  summary: "Compare scorer versions",
  description: "Compares two versions and returns the differences between them",
  tags: ["Scorer Versions"],
  handler: async ({ mastra, scorerId, from, to, requestContext }) => {
    try {
      const storage = mastra.getStorage();
      if (!storage) {
        throw new HTTPException(500, { message: "Storage is not configured" });
      }
      const scorerStore = await storage.getStore("scorerDefinitions");
      if (!scorerStore) {
        throw new HTTPException(500, { message: "Scorer definitions storage domain is not available" });
      }
      const scorer = await scorerStore.getById(scorerId);
      assertStoredResourceScope(scorer, await getStoredResourceScope(mastra, requestContext));
      const fromVersion = await scorerStore.getVersion(from);
      if (!fromVersion) {
        throw new HTTPException(404, { message: `Version with id ${from} not found` });
      }
      if (fromVersion.scorerDefinitionId !== scorerId) {
        throw new HTTPException(404, {
          message: `Version with id ${from} not found for scorer ${scorerId}`
        });
      }
      const toVersion = await scorerStore.getVersion(to);
      if (!toVersion) {
        throw new HTTPException(404, { message: `Version with id ${to} not found` });
      }
      if (toVersion.scorerDefinitionId !== scorerId) {
        throw new HTTPException(404, {
          message: `Version with id ${to} not found for scorer ${scorerId}`
        });
      }
      const fromConfig = extractConfigFromVersion(
        fromVersion,
        SNAPSHOT_CONFIG_FIELDS
      );
      const toConfig = extractConfigFromVersion(
        toVersion,
        SNAPSHOT_CONFIG_FIELDS
      );
      const diffs = computeVersionDiffs(fromConfig, toConfig);
      return {
        diffs,
        fromVersion,
        toVersion
      };
    } catch (error) {
      return handleError(error, "Error comparing scorer versions");
    }
  }
});

export { ACTIVATE_SCORER_VERSION_ROUTE, COMPARE_SCORER_VERSIONS_ROUTE, CREATE_SCORER_VERSION_ROUTE, DELETE_SCORER_VERSION_ROUTE, GET_SCORER_VERSION_ROUTE, LIST_SCORER_VERSIONS_ROUTE, RESTORE_SCORER_VERSION_ROUTE };
//# sourceMappingURL=chunk-BALLHDVH.js.map
//# sourceMappingURL=chunk-BALLHDVH.js.map