import { f as defineAdminEventHandler, h as requireAdminRole, i as requireApiTokenScopes, j as getQuery, k as getActivityFeed, c as createError } from '../../../nitro/nitro.mjs';
import 'node:crypto';
import 'jsonwebtoken';
import 'node:fs';
import 'node:module';
import 'node:path';
import 'node:url';
import 'node:http';
import 'node:https';
import 'node:events';
import 'node:buffer';
import 'pino';
import 'drizzle-orm/better-sqlite3';
import 'drizzle-orm/node-postgres';
import 'drizzle-orm/mysql2';
import 'better-sqlite3';
import 'pg';
import 'mysql2/promise';
import 'drizzle-orm';
import 'fs';
import 'path';
import 'prom-client';
import 'bcryptjs';
import '@node-rs/argon2';
import 'node:async_hooks';
import 'crypto';
import 'node:dns';
import 'node:util';
import 'node:net';
import 'fs/promises';
import 'url';
import 'semver';

const VALID_CATEGORIES = /* @__PURE__ */ new Set([
  "content",
  "schema",
  "media",
  "settings",
  "auth",
  "collaboration"
]);
const VALID_ACTOR_TYPES = /* @__PURE__ */ new Set(["admin", "user", "api_token", "system"]);
function getQueryValue(value) {
  if (Array.isArray(value)) {
    const first = value[0];
    if (first === null || first === void 0) {
      return null;
    }
    const normalized2 = String(first).trim();
    return normalized2.length > 0 ? normalized2 : null;
  }
  if (value === null || value === void 0) {
    return null;
  }
  const normalized = String(value).trim();
  return normalized.length > 0 ? normalized : null;
}
function createBadRequest(message) {
  return createError({
    statusCode: 400,
    statusMessage: "Bad Request",
    message
  });
}
function parseCategories(value) {
  const rawValue = getQueryValue(value);
  if (rawValue === null) {
    return null;
  }
  const categories = rawValue.split(",").map((category) => category.trim()).filter(Boolean);
  if (categories.length === 0) {
    throw createBadRequest("category must contain at least one valid value");
  }
  const invalidCategories = categories.filter(
    (category) => !VALID_CATEGORIES.has(category)
  );
  if (invalidCategories.length > 0) {
    throw createBadRequest(
      `Invalid activity categories: ${invalidCategories.join(", ")}. Allowed values: ${Array.from(
        VALID_CATEGORIES
      ).join(", ")}`
    );
  }
  return [...new Set(categories)];
}
function parseActorType(value) {
  const rawValue = getQueryValue(value);
  if (rawValue === null) {
    return void 0;
  }
  if (!VALID_ACTOR_TYPES.has(rawValue)) {
    throw createBadRequest(
      `Invalid actorType: ${rawValue}. Allowed values: ${Array.from(VALID_ACTOR_TYPES).join(", ")}`
    );
  }
  return rawValue;
}
function parseIntegerParam(value, fieldName, options = {}) {
  const rawValue = getQueryValue(value);
  if (rawValue === null) {
    return options.defaultValue;
  }
  if (!/^-?\d+$/.test(rawValue)) {
    throw createBadRequest(`${fieldName} must be an integer`);
  }
  const parsed = Number(rawValue);
  if (!Number.isSafeInteger(parsed)) {
    throw createBadRequest(`${fieldName} must be a safe integer`);
  }
  if (options.min !== void 0 && parsed < options.min) {
    throw createBadRequest(`${fieldName} must be greater than or equal to ${options.min}`);
  }
  return parsed;
}
function parseDateParam(value, fieldName) {
  const rawValue = getQueryValue(value);
  if (rawValue === null) {
    return void 0;
  }
  const timestamp = Date.parse(rawValue);
  if (Number.isNaN(timestamp)) {
    throw createBadRequest(`${fieldName} must be a valid ISO date string`);
  }
  return rawValue;
}
const index_get = defineAdminEventHandler("admin:activity:list", async (event) => {
  var _a, _b;
  try {
    await requireAdminRole(event, ["super-admin", "admin", "editor"]);
    requireApiTokenScopes(event.context.apiToken, { scopes: ["activity:read"] });
    const query = getQuery(event);
    const filters = {};
    const categories = parseCategories(query.category);
    if (categories) {
      filters.category = categories.length === 1 ? categories[0] : categories;
    }
    const actorType = parseActorType(query.actorType);
    if (actorType) {
      filters.actorType = actorType;
    }
    const actorId = parseIntegerParam(query.actorId, "actorId", { min: 1 });
    if (actorId !== void 0) {
      filters.actorId = actorId;
    }
    const entityType = getQueryValue(query.entityType);
    if (entityType) {
      filters.entityType = entityType;
    }
    const entityId = getQueryValue(query.entityId);
    if (entityId) {
      filters.entityId = entityId;
    }
    const dateFrom = parseDateParam(query.dateFrom, "dateFrom");
    if (dateFrom) {
      filters.dateFrom = dateFrom;
    }
    const dateTo = parseDateParam(query.dateTo, "dateTo");
    if (dateTo) {
      filters.dateTo = dateTo;
    }
    const limit = Math.min(
      (_a = parseIntegerParam(query.limit, "limit", { min: 1, defaultValue: 50 })) != null ? _a : 50,
      200
    );
    const offset = (_b = parseIntegerParam(query.offset, "offset", { min: 0, defaultValue: 0 })) != null ? _b : 0;
    const result = await getActivityFeed(filters, limit, offset);
    return {
      success: true,
      data: result.data,
      meta: {
        total: result.total,
        limit,
        offset
      }
    };
  } catch (error) {
    if (error && typeof error === "object" && "statusCode" in error) {
      throw error;
    }
    console.error("[GET /api/admin/activity] Error:", error);
    throw createError({
      statusCode: 500,
      statusMessage: "Failed to fetch activity feed",
      message: error instanceof Error ? error.message : "Unknown error"
    });
  }
});

export { index_get as default };
//# sourceMappingURL=index.get.mjs.map
