/**
 * Settings validation schema for Images
 * Auto-generated from baseline.json
 * 
 * @module Performance/Images
 * @since 0.1.0
 */

import { z } from 'zod';

/**
 * Settings schema for Images
 */
export const ImagesSchema = z.object({
  enable_image_optimization: z.boolean().default(true),  // Enable image optimization
  enable_lazy_loading: z.boolean().default(true),  // Enable lazy loading for images
  lazy_load_threshold: z.number().int().min(0).max(1000).default(300),  // Pixels before viewport to start loading
  convert_to_webp: z.boolean().default(false),  // Convert images to WebP format
  convert_to_avif: z.boolean().default(false),  // Convert images to AVIF format
  serve_scaled_images: z.boolean().default(true),  // Serve appropriately sized images
  compression_level: z.number().int().min(0).max(100).default(85),  // JPEG compression quality
  enable_responsive_images: z.boolean().default(true),  // Add srcset for responsive images
  exclude_classes: z.array(z.string()).default(["no-lazy","skip-lazy"]),  // CSS classes to exclude from lazy loading
  add_dimensions: z.boolean().default(true),  // Add width/height attributes to prevent CLS
  optimize_on_upload: z.boolean().default(false),  // Optimize images on upload
  background_mode: z.boolean().default(false),  // Process images in background using Action Scheduler
  backup_originals: z.boolean().default(true),  // Keep backup of original images
  custom_folders: z.array(z.string()).default([]),  // Additional folders to scan for images
  smart_exclusions: z.object({
    enabled: z.boolean(),
    min_size: z.number(),
    max_size: z.number(),
    exclude_patterns: z.unknown()
  }).default({"enabled":true,"min_size":10,"max_size":5000,"exclude_patterns":["logo","icon","favicon","badge","banner"]}),  // Smart exclusion settings
  compression_type: z.enum(['lossy', 'glossy', 'lossless']).default('glossy'),  // Compression type for optimization
  smart_compression: z.boolean().default(true),  // Automatically adjust quality based on image content
  pdf_optimization: z.boolean().default(false),  // Optimize PDF files
  retina_images: z.boolean().default(true),  // Generate @2x images for retina displays
  png_to_jpeg: z.boolean().default(false),  // Convert PNG images to JPEG when beneficial
  cmyk_to_rgb: z.boolean().default(true),  // Convert CMYK images to RGB
  exif_removal: z.enum(['keep_all', 'remove_all', 'keep_copyright']).default('keep_copyright'),  // EXIF data handling
  optimization_method: z.enum(['local', 'prorank']).default('local'),  // Optimization method to use
  api_key: z.string().default(''),  // API key for cloud optimization
  max_image_width: z.number().int().min(800).max(4096).default(2560),  // Maximum image width in pixels
  delivery_method: z.enum(['none', 'picture', 'rewrite', 'both']).default('picture'),  // WebP/AVIF delivery method
  exclude_sizes: z.array(z.string()).default([]),  // Image sizes to exclude from optimization
});

/**
 * TypeScript type for Images settings
 */
export type ImagesSettings = z.infer<typeof ImagesSchema>;

/**
 * Default values for Images settings
 */
export const ImagesDefaults: ImagesSettings = ImagesSchema.parse({});

/**
 * Validate Images settings
 */
export function validateImagesSettings(data: unknown): ImagesSettings {
  return ImagesSchema.parse(data);
}

/**
 * Safely validate Images settings (returns validation result)
 */
export function safeValidateImagesSettings(data: unknown) {
  return ImagesSchema.safeParse(data);
}
