import { format } from 'date-fns';

export const formatARCSummary = (app) => {
  if (!app) return null;

  // Owner information is removed as per request
  const clientName = app.clients?.name || 'Unknown Association';
  
  let formattedDate = 'Date not available';
  if (app.created_at) {
    try {
        formattedDate = format(new Date(app.created_at), 'MMMM do, yyyy');
    } catch (e) {
        console.error("Invalid date", e);
    }
  }
  
  // Find a thumbnail
  // First check arc_files
  let thumbnail = null;
  if (app.arc_files && Array.isArray(app.arc_files) && app.arc_files.length > 0) {
      // Look for image extensions
      const imageFile = app.arc_files.find(f => 
          f.file_name && f.file_name.match(/\.(jpeg|jpg|png|gif|webp)$/i)
      );
      if (imageFile) thumbnail = imageFile.file_url;
  }
  
  // Fallback to legacy file_url if it looks like an image
  if (!thumbnail && app.file_url && app.file_url.match(/\.(jpeg|jpg|png|gif|webp)$/i)) {
      thumbnail = app.file_url;
  }

  return {
    ...app,
    displayClient: clientName,
    displayDate: formattedDate,
    thumbnailUrl: thumbnail
  };
};