'use strict';

var chunkOWWI7PFV_cjs = require('./chunk-OWWI7PFV.cjs');
var chunk2XZ2466F_cjs = require('./chunk-2XZ2466F.cjs');
var chunkDRDVJ35H_cjs = require('./chunk-DRDVJ35H.cjs');
var chunkZ7LCIYK7_cjs = require('./chunk-Z7LCIYK7.cjs');
var chunkN54Q3AQR_cjs = require('./chunk-N54Q3AQR.cjs');
var chunk64ITUOXI_cjs = require('./chunk-64ITUOXI.cjs');

// src/server/handlers/scorer-versions.ts
var SNAPSHOT_CONFIG_FIELDS = [
  "name",
  "description",
  "type",
  "model",
  "instructions",
  "scoreRange",
  "presetConfig",
  "defaultSampling"
];
var LIST_SCORER_VERSIONS_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "GET",
  path: "/stored/scorers/:scorerId/versions",
  requiresAuth: true,
  responseType: "json",
  pathParamSchema: chunkOWWI7PFV_cjs.scorerVersionPathParams,
  queryParamSchema: chunkOWWI7PFV_cjs.listScorerVersionsQuerySchema,
  responseSchema: chunkOWWI7PFV_cjs.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 chunk64ITUOXI_cjs.HTTPException(500, { message: "Storage is not configured" });
      }
      const scorerStore = await storage.getStore("scorerDefinitions");
      if (!scorerStore) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Scorer definitions storage domain is not available" });
      }
      const scorer = await scorerStore.getById(scorerId);
      if (!scorer) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `Scorer with id ${scorerId} not found` });
      }
      chunkDRDVJ35H_cjs.assertStoredResourceScope(scorer, await chunkDRDVJ35H_cjs.getStoredResourceScope(mastra, requestContext));
      const result = await scorerStore.listVersions({
        scorerDefinitionId: scorerId,
        page,
        perPage,
        orderBy
      });
      return result;
    } catch (error) {
      return chunkZ7LCIYK7_cjs.handleError(error, "Error listing scorer versions");
    }
  }
});
var CREATE_SCORER_VERSION_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "POST",
  path: "/stored/scorers/:scorerId/versions",
  requiresAuth: true,
  responseType: "json",
  pathParamSchema: chunkOWWI7PFV_cjs.scorerVersionPathParams,
  bodySchema: chunkOWWI7PFV_cjs.createScorerVersionBodySchema,
  responseSchema: chunkOWWI7PFV_cjs.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 chunk64ITUOXI_cjs.HTTPException(500, { message: "Storage is not configured" });
      }
      const scorerStore = await storage.getStore("scorerDefinitions");
      if (!scorerStore) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Scorer definitions storage domain is not available" });
      }
      const scorer = await scorerStore.getById(scorerId);
      if (!scorer) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `Scorer with id ${scorerId} not found` });
      }
      chunkDRDVJ35H_cjs.assertStoredResourceScope(scorer, await chunkDRDVJ35H_cjs.getStoredResourceScope(mastra, requestContext));
      let currentConfig = {};
      if (scorer.activeVersionId) {
        const activeVersion = await scorerStore.getVersion(scorer.activeVersionId);
        if (activeVersion) {
          currentConfig = chunk2XZ2466F_cjs.extractConfigFromVersion(
            activeVersion,
            SNAPSHOT_CONFIG_FIELDS
          );
        }
      }
      const latestVersion = await scorerStore.getLatestVersion(scorerId);
      if (!scorer.activeVersionId && latestVersion) {
        currentConfig = chunk2XZ2466F_cjs.extractConfigFromVersion(
          latestVersion,
          SNAPSHOT_CONFIG_FIELDS
        );
      }
      const previousConfig = latestVersion ? chunk2XZ2466F_cjs.extractConfigFromVersion(latestVersion, SNAPSHOT_CONFIG_FIELDS) : null;
      const changedFields = chunk2XZ2466F_cjs.calculateChangedFields(previousConfig, currentConfig);
      const { versionId } = await chunk2XZ2466F_cjs.createVersionWithRetry(
        scorerStore,
        scorerId,
        "scorerDefinitionId",
        currentConfig,
        changedFields,
        { changeMessage }
      );
      const version = await scorerStore.getVersion(versionId);
      if (!version) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Failed to retrieve created version" });
      }
      await chunk2XZ2466F_cjs.enforceRetentionLimit(
        scorerStore,
        scorerId,
        "scorerDefinitionId",
        scorer.activeVersionId
      );
      return version;
    } catch (error) {
      return chunkZ7LCIYK7_cjs.handleError(error, "Error creating scorer version");
    }
  }
});
var GET_SCORER_VERSION_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "GET",
  path: "/stored/scorers/:scorerId/versions/:versionId",
  requiresAuth: true,
  responseType: "json",
  pathParamSchema: chunkOWWI7PFV_cjs.scorerVersionIdPathParams,
  responseSchema: chunkOWWI7PFV_cjs.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 chunk64ITUOXI_cjs.HTTPException(500, { message: "Storage is not configured" });
      }
      const scorerStore = await storage.getStore("scorerDefinitions");
      if (!scorerStore) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Scorer definitions storage domain is not available" });
      }
      const version = await scorerStore.getVersion(versionId);
      if (!version) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `Version with id ${versionId} not found` });
      }
      if (version.scorerDefinitionId !== scorerId) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, {
          message: `Version with id ${versionId} not found for scorer ${scorerId}`
        });
      }
      const scorer = await scorerStore.getById(scorerId);
      chunkDRDVJ35H_cjs.assertStoredResourceScope(scorer, await chunkDRDVJ35H_cjs.getStoredResourceScope(mastra, requestContext));
      return version;
    } catch (error) {
      return chunkZ7LCIYK7_cjs.handleError(error, "Error getting scorer version");
    }
  }
});
var ACTIVATE_SCORER_VERSION_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "POST",
  path: "/stored/scorers/:scorerId/versions/:versionId/activate",
  requiresAuth: true,
  responseType: "json",
  pathParamSchema: chunkOWWI7PFV_cjs.scorerVersionIdPathParams,
  responseSchema: chunkOWWI7PFV_cjs.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 chunk64ITUOXI_cjs.HTTPException(500, { message: "Storage is not configured" });
      }
      const scorerStore = await storage.getStore("scorerDefinitions");
      if (!scorerStore) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Scorer definitions storage domain is not available" });
      }
      const scorer = await scorerStore.getById(scorerId);
      if (!scorer) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `Scorer with id ${scorerId} not found` });
      }
      chunkDRDVJ35H_cjs.assertStoredResourceScope(scorer, await chunkDRDVJ35H_cjs.getStoredResourceScope(mastra, requestContext));
      const version = await scorerStore.getVersion(versionId);
      if (!version) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `Version with id ${versionId} not found` });
      }
      if (version.scorerDefinitionId !== scorerId) {
        throw new chunk64ITUOXI_cjs.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 chunkZ7LCIYK7_cjs.handleError(error, "Error activating scorer version");
    }
  }
});
var RESTORE_SCORER_VERSION_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "POST",
  path: "/stored/scorers/:scorerId/versions/:versionId/restore",
  requiresAuth: true,
  responseType: "json",
  pathParamSchema: chunkOWWI7PFV_cjs.scorerVersionIdPathParams,
  responseSchema: chunkOWWI7PFV_cjs.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 chunk64ITUOXI_cjs.HTTPException(500, { message: "Storage is not configured" });
      }
      const scorerStore = await storage.getStore("scorerDefinitions");
      if (!scorerStore) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Scorer definitions storage domain is not available" });
      }
      const scorer = await scorerStore.getById(scorerId);
      if (!scorer) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `Scorer with id ${scorerId} not found` });
      }
      chunkDRDVJ35H_cjs.assertStoredResourceScope(scorer, await chunkDRDVJ35H_cjs.getStoredResourceScope(mastra, requestContext));
      const versionToRestore = await scorerStore.getVersion(versionId);
      if (!versionToRestore) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `Version with id ${versionId} not found` });
      }
      if (versionToRestore.scorerDefinitionId !== scorerId) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, {
          message: `Version with id ${versionId} not found for scorer ${scorerId}`
        });
      }
      const restoredConfig = chunk2XZ2466F_cjs.extractConfigFromVersion(
        versionToRestore,
        SNAPSHOT_CONFIG_FIELDS
      );
      await scorerStore.update({
        id: scorerId,
        ...restoredConfig
      });
      const latestVersion = await scorerStore.getLatestVersion(scorerId);
      const previousConfig = latestVersion ? chunk2XZ2466F_cjs.extractConfigFromVersion(latestVersion, SNAPSHOT_CONFIG_FIELDS) : null;
      const changedFields = chunk2XZ2466F_cjs.calculateChangedFields(previousConfig, restoredConfig);
      const { versionId: newVersionId } = await chunk2XZ2466F_cjs.createVersionWithRetry(
        scorerStore,
        scorerId,
        "scorerDefinitionId",
        restoredConfig,
        changedFields,
        {
          changeMessage: `Restored from version ${versionToRestore.versionNumber}`
        }
      );
      const newVersion = await scorerStore.getVersion(newVersionId);
      if (!newVersion) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Failed to retrieve created version" });
      }
      await chunk2XZ2466F_cjs.enforceRetentionLimit(
        scorerStore,
        scorerId,
        "scorerDefinitionId",
        scorer.activeVersionId
      );
      mastra.getEditor()?.scorer.clearCache(scorerId);
      return newVersion;
    } catch (error) {
      return chunkZ7LCIYK7_cjs.handleError(error, "Error restoring scorer version");
    }
  }
});
var DELETE_SCORER_VERSION_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "DELETE",
  path: "/stored/scorers/:scorerId/versions/:versionId",
  requiresAuth: true,
  responseType: "json",
  pathParamSchema: chunkOWWI7PFV_cjs.scorerVersionIdPathParams,
  responseSchema: chunkOWWI7PFV_cjs.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 chunk64ITUOXI_cjs.HTTPException(500, { message: "Storage is not configured" });
      }
      const scorerStore = await storage.getStore("scorerDefinitions");
      if (!scorerStore) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Scorer definitions storage domain is not available" });
      }
      const scorer = await scorerStore.getById(scorerId);
      if (!scorer) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `Scorer with id ${scorerId} not found` });
      }
      chunkDRDVJ35H_cjs.assertStoredResourceScope(scorer, await chunkDRDVJ35H_cjs.getStoredResourceScope(mastra, requestContext));
      const version = await scorerStore.getVersion(versionId);
      if (!version) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `Version with id ${versionId} not found` });
      }
      if (version.scorerDefinitionId !== scorerId) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, {
          message: `Version with id ${versionId} not found for scorer ${scorerId}`
        });
      }
      if (scorer.activeVersionId === versionId) {
        throw new chunk64ITUOXI_cjs.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 chunkZ7LCIYK7_cjs.handleError(error, "Error deleting scorer version");
    }
  }
});
var COMPARE_SCORER_VERSIONS_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "GET",
  path: "/stored/scorers/:scorerId/versions/compare",
  requiresAuth: true,
  responseType: "json",
  pathParamSchema: chunkOWWI7PFV_cjs.scorerVersionPathParams,
  queryParamSchema: chunkOWWI7PFV_cjs.compareScorerVersionsQuerySchema,
  responseSchema: chunkOWWI7PFV_cjs.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 chunk64ITUOXI_cjs.HTTPException(500, { message: "Storage is not configured" });
      }
      const scorerStore = await storage.getStore("scorerDefinitions");
      if (!scorerStore) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Scorer definitions storage domain is not available" });
      }
      const scorer = await scorerStore.getById(scorerId);
      chunkDRDVJ35H_cjs.assertStoredResourceScope(scorer, await chunkDRDVJ35H_cjs.getStoredResourceScope(mastra, requestContext));
      const fromVersion = await scorerStore.getVersion(from);
      if (!fromVersion) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `Version with id ${from} not found` });
      }
      if (fromVersion.scorerDefinitionId !== scorerId) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, {
          message: `Version with id ${from} not found for scorer ${scorerId}`
        });
      }
      const toVersion = await scorerStore.getVersion(to);
      if (!toVersion) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `Version with id ${to} not found` });
      }
      if (toVersion.scorerDefinitionId !== scorerId) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, {
          message: `Version with id ${to} not found for scorer ${scorerId}`
        });
      }
      const fromConfig = chunk2XZ2466F_cjs.extractConfigFromVersion(
        fromVersion,
        SNAPSHOT_CONFIG_FIELDS
      );
      const toConfig = chunk2XZ2466F_cjs.extractConfigFromVersion(
        toVersion,
        SNAPSHOT_CONFIG_FIELDS
      );
      const diffs = chunk2XZ2466F_cjs.computeVersionDiffs(fromConfig, toConfig);
      return {
        diffs,
        fromVersion,
        toVersion
      };
    } catch (error) {
      return chunkZ7LCIYK7_cjs.handleError(error, "Error comparing scorer versions");
    }
  }
});

exports.ACTIVATE_SCORER_VERSION_ROUTE = ACTIVATE_SCORER_VERSION_ROUTE;
exports.COMPARE_SCORER_VERSIONS_ROUTE = COMPARE_SCORER_VERSIONS_ROUTE;
exports.CREATE_SCORER_VERSION_ROUTE = CREATE_SCORER_VERSION_ROUTE;
exports.DELETE_SCORER_VERSION_ROUTE = DELETE_SCORER_VERSION_ROUTE;
exports.GET_SCORER_VERSION_ROUTE = GET_SCORER_VERSION_ROUTE;
exports.LIST_SCORER_VERSIONS_ROUTE = LIST_SCORER_VERSIONS_ROUTE;
exports.RESTORE_SCORER_VERSION_ROUTE = RESTORE_SCORER_VERSION_ROUTE;
//# sourceMappingURL=chunk-CU3VFK4E.cjs.map
//# sourceMappingURL=chunk-CU3VFK4E.cjs.map