/**
 * Validation Types
 *
 * Shared type definitions for validation results
 * Provides consistent validation return types across the application
 */

/**
 * Generic validation result
 *
 * @template T - Type of the validated data
 *
 * @example
 * ```typescript
 * function validateAge(age: number): ValidationResult<number> {
 *   if (age < 0) {
 *     return { valid: false, error: 'Age must be positive' };
 *   }
 *   return { valid: true, data: age };
 * }
 * ```
 */
export type ValidationResult<T> =
  | { valid: true; data: T }
  | { valid: false; error: string };

/**
 * Validation result with optional sanitized value
 * Used when validation also performs sanitization
 */
export type SanitizedValidationResult =
  | { valid: true; sanitized: string }
  | { valid: false; error: string };

/**
 * Multiple field validation result
 * Used for validating complex objects
 */
export type MultiFieldValidationResult =
  | { valid: true }
  | { valid: false; errors: Record<string, string> };

/**
 * Type guard to check if validation succeeded
 */
export function isValid<T>(result: ValidationResult<T>): result is { valid: true; data: T } {
  return result.valid === true;
}

/**
 * Type guard to check if validation failed
 */
export function isInvalid<T>(result: ValidationResult<T>): result is { valid: false; error: string } {
  return result.valid === false;
}

/**
 * Extract data from validation result or throw error
 *
 * @throws {Error} If validation failed
 */
export function unwrap<T>(result: ValidationResult<T>): T {
  if (result.valid) {
    return result.data;
  }
  throw new Error(result.error);
}

/**
 * Extract data from validation result or return default
 */
export function unwrapOr<T>(result: ValidationResult<T>, defaultValue: T): T {
  if (result.valid) {
    return result.data;
  }
  return defaultValue;
}
