import { z } from "zod";
const EmailSchema = z.string().email("Invalid email format").min(5, "Email too short").max(255, "Email too long").toLowerCase().transform((email) => email.trim()).refine((email) => !email.includes(".."), "Invalid email format").refine((email) => !email.startsWith("."), "Invalid email format").refine((email) => !email.endsWith("."), "Invalid email format");
const UserTierSchema = z.enum(["free", "pro", "enterprise"]);
const PermissionSchema = z.enum([
  "read",
  "write",
  "delete",
  "admin",
  "moderate"
]);
const OrganizationSchema = z.object({
  id: z.string().uuid("Invalid organization ID"),
  name: z.string().min(1).max(255),
  role: z.enum(["member", "admin", "owner"]).default("member")
});
const UserMetadataSchema = z.object({
  lastLoginIp: z.string().ip().optional(),
  userAgent: z.string().max(500).optional(),
  createdVia: z.string().max(50).optional(),
  signupSource: z.string().max(50).optional(),
  isDevelopmentUser: z.boolean().optional(),
  auth0: z.record(z.unknown()).optional(),
  customFields: z.record(z.string(), z.unknown()).optional()
}).strict();
const CreateUserSchema = z.object({
  sub: z.string().min(1).max(255),
  email: EmailSchema,
  name: z.string().min(1).max(255).optional(),
  avatar: z.string().url().optional(),
  tier: UserTierSchema.default("free"),
  permissions: z.array(PermissionSchema).default(["read", "write"]),
  organizations: z.array(OrganizationSchema).default([]),
  metadata: UserMetadataSchema.optional()
});
const UpdateUserSchema = CreateUserSchema.partial().extend({
  id: z.string().uuid("Invalid user ID")
});
const CreateApiKeySchema = z.object({
  userId: z.string().uuid("Invalid user ID"),
  name: z.string().min(1).max(100).default("API Key"),
  expiresIn: z.number().int().positive().optional()
  // seconds
});
const CreateSessionSchema = z.object({
  userId: z.string().uuid("Invalid user ID"),
  expiresIn: z.number().int().min(60).max(86400 * 30).default(86400),
  // 1 minute to 30 days
  metadata: z.record(z.string(), z.unknown()).optional()
});
const JWTPayloadSchema = z.object({
  sub: z.string().min(1),
  email: EmailSchema.optional(),
  name: z.string().optional(),
  picture: z.string().url().optional(),
  iat: z.number(),
  exp: z.number(),
  aud: z.string().or(z.array(z.string())).optional(),
  iss: z.string().optional(),
  permissions: z.array(z.string()).optional(),
  roles: z.array(z.string()).optional(),
  org_id: z.string().optional(),
  org_name: z.string().optional(),
  org_role: z.string().optional()
});
const ContextTypeSchema = z.enum([
  "code",
  "documentation",
  "conversation",
  "decision",
  "task",
  "error",
  "analysis"
]);
const CreateContextFrameSchema = z.object({
  projectId: z.string().uuid("Invalid project ID"),
  branch: z.string().max(255).optional(),
  content: z.string().min(1).max(1e6),
  // 1MB max
  summary: z.string().max(5e3).optional(),
  type: ContextTypeSchema,
  metadata: z.record(z.string(), z.unknown()).optional()
});
const CreateTraceSchema = z.object({
  sessionId: z.string().uuid("Invalid session ID"),
  type: z.string().min(1).max(100),
  data: z.record(z.string(), z.unknown()),
  metadata: z.record(z.string(), z.unknown()).optional()
});
const EmbeddingDimensionsSchema = z.number().int().positive().refine(
  (dim) => [384, 768, 1024, 1536, 3072].includes(dim),
  "Invalid embedding dimensions. Must be 384, 768, 1024, 1536, or 3072"
);
const SemanticSearchConfigSchema = z.object({
  tableName: z.string().regex(/^[a-zA-Z_][a-zA-Z0-9_]*$/, "Invalid table name").max(63),
  // PostgreSQL table name limit
  embeddingColumn: z.string().regex(/^[a-zA-Z_][a-zA-Z0-9_]*$/, "Invalid column name").max(63),
  contentColumn: z.string().regex(/^[a-zA-Z_][a-zA-Z0-9_]*$/, "Invalid column name").max(63),
  vectorDimensions: EmbeddingDimensionsSchema
});
const SearchQuerySchema = z.object({
  query: z.string().min(1).max(5e3),
  limit: z.number().int().min(1).max(100).default(10),
  threshold: z.number().min(0).max(1).default(0.7),
  filters: z.record(z.string(), z.unknown()).optional()
});
const PostgresConfigSchema = z.object({
  host: z.string().min(1),
  port: z.number().int().min(1).max(65535).default(5432),
  database: z.string().min(1).max(63),
  user: z.string().min(1).max(63),
  password: z.string().min(1),
  ssl: z.union([
    z.boolean(),
    z.object({
      rejectUnauthorized: z.boolean().optional(),
      ca: z.string().optional(),
      cert: z.string().optional(),
      key: z.string().optional()
    })
  ]).optional(),
  max: z.number().int().min(1).max(100).default(20),
  // Connection pool size
  idleTimeoutMillis: z.number().int().positive().default(3e4),
  connectionTimeoutMillis: z.number().int().positive().default(5e3),
  enableTimescale: z.boolean().default(false),
  enablePgvector: z.boolean().default(false),
  vectorDimensions: EmbeddingDimensionsSchema.optional()
});
const RedisConfigSchema = z.object({
  host: z.string().default("localhost"),
  port: z.number().int().min(1).max(65535).default(6379),
  password: z.string().optional(),
  db: z.number().int().min(0).max(15).default(0),
  keyPrefix: z.string().optional(),
  retryStrategy: z.function().optional(),
  maxRetriesPerRequest: z.number().int().positive().optional()
});
const MetricEntrySchema = z.object({
  metric: z.string().min(1).max(255),
  value: z.number(),
  type: z.enum(["counter", "gauge", "timing"]),
  tags: z.record(z.string(), z.string()).optional()
});
const LogLevelSchema = z.enum(["ERROR", "WARN", "INFO", "DEBUG"]);
const LinearWebhookPayloadSchema = z.object({
  action: z.enum(["create", "update", "delete"]),
  type: z.enum(["issue", "comment", "project", "cycle"]),
  data: z.object({
    id: z.string(),
    title: z.string().optional(),
    description: z.string().optional(),
    state: z.string().optional(),
    priority: z.number().optional(),
    assignee: z.object({
      id: z.string(),
      email: EmailSchema.optional(),
      name: z.string().optional()
    }).optional()
  }),
  createdAt: z.string().datetime(),
  organizationId: z.string()
});
const PaginationSchema = z.object({
  page: z.number().int().positive().default(1),
  limit: z.number().int().min(1).max(100).default(20),
  sortBy: z.string().optional(),
  sortOrder: z.enum(["asc", "desc"]).default("desc")
});
const DateRangeSchema = z.object({
  from: z.string().datetime().or(z.date()),
  to: z.string().datetime().or(z.date())
}).refine((data) => {
  const from = typeof data.from === "string" ? new Date(data.from) : data.from;
  const to = typeof data.to === "string" ? new Date(data.to) : data.to;
  return from <= to;
}, "From date must be before or equal to To date");
const FileUploadSchema = z.object({
  filename: z.string().min(1).max(255).regex(/^[a-zA-Z0-9._-]+$/, "Invalid filename"),
  mimetype: z.string().regex(/^[a-zA-Z]+\/[a-zA-Z0-9.+-]+$/, "Invalid MIME type"),
  size: z.number().int().positive().max(10 * 1024 * 1024),
  // 10MB max
  encoding: z.string().optional()
});
const sanitizeSQLIdentifier = (identifier) => {
  const sanitized = identifier.replace(/[^a-zA-Z0-9_]/g, "");
  if (!/^[a-zA-Z_]/.test(sanitized)) {
    throw new Error("Invalid SQL identifier");
  }
  if (sanitized.length > 63) {
    throw new Error("SQL identifier too long");
  }
  return sanitized;
};
const sanitizeHTML = (input) => {
  return input.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#x27;").replace(/\//g, "&#x2F;");
};
export {
  CreateApiKeySchema,
  CreateContextFrameSchema,
  CreateSessionSchema,
  CreateTraceSchema,
  CreateUserSchema,
  DateRangeSchema,
  FileUploadSchema,
  JWTPayloadSchema,
  LinearWebhookPayloadSchema,
  LogLevelSchema,
  MetricEntrySchema,
  PaginationSchema,
  PostgresConfigSchema,
  RedisConfigSchema,
  SearchQuerySchema,
  SemanticSearchConfigSchema,
  UpdateUserSchema,
  sanitizeHTML,
  sanitizeSQLIdentifier
};
//# sourceMappingURL=schemas.js.map
