// packages/functions/src/shared/utils/internal/validation.ts

/**
 * @fileoverview Validation utility functions
 * @description Functions for validating environment and data
 *
 * @version 0.0.1
 * @since 0.0.1
 * @author AMBROISE PARK Consulting
 */

/**
 * Validates environment variables required for Stripe
 *
 * @version 0.0.1
 * @since 0.0.1
 * @author AMBROISE PARK Consulting
 */
export function validateStripeEnvironment(): void {
  const required = ['STRIPE_SECRET_KEY', 'STRIPE_WEBHOOK_SECRET'];
  const missing = required.filter((key) => !process.env[key]);

  if (missing.length > 0) {
    throw new Error(
      `Missing required environment variables: ${missing.join(', ')}`
    );
  }
}

/**
 * Safely parses JSON with error handling
 *
 * @version 0.0.1
 * @since 0.0.1
 * @author AMBROISE PARK Consulting
 */
export function safeJsonParse<T = any>(json: string): T | null {
  try {
    return JSON.parse(json);
  } catch {
    return null;
  }
}

/**
 * Validates document data against schema
 *
 * @version 0.0.1
 * @since 0.0.1
 * @author AMBROISE PARK Consulting
 */
export function validateDocument(data: any, schema?: any): void {
  if (!data || typeof data !== 'object') {
    throw new Error('Invalid document data');
  }

  // Basic validation - can be extended with schema validation
  if (Array.isArray(data)) {
    throw new Error('Document data cannot be an array');
  }
}
