import fs from 'fs';
import path from 'path';

export interface CarouselImage {
  src: string;
  alt: string;
}

/**
 * Reads all images from the carrousel folder at build/request time.
 * Supports jpg, jpeg, png, webp, and gif formats.
 */
export function getCarouselImages(): CarouselImage[] {
  const carrouselDir = path.join(process.cwd(), 'public', 'images', 'carrousel');
  
  try {
    const files = fs.readdirSync(carrouselDir);
    
    // Filter for supported image formats
    const supportedExtensions = ['.jpg', '.jpeg', '.png', '.webp', '.gif'];
    const imageFiles = files.filter((file) => {
      const ext = path.extname(file).toLowerCase();
      return supportedExtensions.includes(ext);
    });
    
    // Sort by filename (which includes date) for consistent ordering
    imageFiles.sort();
    
    return imageFiles.map((file) => ({
      src: `/images/carrousel/${file}`,
      alt: 'Spanglish language exchange event moment',
    }));
  } catch (error) {
    console.error('Error reading carrousel directory:', error);
    return [];
  }
}
