/**
 * Input validation utilities for API routes
 */

export function sanitizeString(input: string, maxLength: number = 1000): string {
  if (typeof input !== 'string') return ''
  return input.trim().slice(0, maxLength)
}

export function isValidId(id: string): boolean {
  // CUID format used by Prisma
  return typeof id === 'string' && /^[a-z0-9]{20,36}$/i.test(id)
}

export function validatePagination(page: string | null, limit: string | null): { page: number; limit: number } {
  const p = Math.max(1, Math.min(1000, parseInt(page || '1', 10) || 1))
  const l = Math.max(1, Math.min(100, parseInt(limit || '20', 10) || 20))
  return { page: p, limit: l }
}

export const MAX_LENGTHS = {
  name: 200,
  email: 254,
  password: 128,
  title: 500,
  description: 5000,
  content: 100000,
  message: 10000,
  query: 2000,
  phone: 20,
  oabNumber: 10,
  oabState: 2,
  processNumber: 30,
  court: 200,
} as const
