import type { Metadata } from 'next';
import type { PhotoGallery } from '@/lib/api';
import PhotosIndexClient from './PhotosIndexClient';

// Server-side calls go straight to the photo-api service; the browser uses
// the same-origin /api/photos path via the Next rewrite / nginx.
const photoApiUrl = process.env.PHOTO_API_URL || 'http://localhost:3003';

export const metadata: Metadata = {
  title: 'Event Photo Galleries',
  description: 'Photos from past Spanglish language exchange events in Asunción.',
};

async function getGalleries(): Promise<PhotoGallery[]> {
  try {
    const res = await fetch(`${photoApiUrl}/api/photos/public/galleries`, {
      next: { revalidate: 300 },
    });
    if (!res.ok) return [];
    const data = await res.json();
    return data.galleries || [];
  } catch {
    return [];
  }
}

export default async function PhotosPage() {
  const galleries = await getGalleries();
  return <PhotosIndexClient galleries={galleries} />;
}
