import EleventyFetch from '@11ty/eleventy-fetch'; const { POSTGREST_URL, POSTGREST_API_KEY } = process.env; const fetchAllMovies = async () => { try { return await EleventyFetch(`${POSTGREST_URL}/optimized_movies?select=*`, { duration: '1h', type: 'json', fetchOptions: { method: 'GET', headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${POSTGREST_API_KEY}` } } }); } catch (error) { console.error('Error fetching movies:', error); return []; } }; export default async function () { try { const movies = await fetchAllMovies(); const favoriteMovies = movies.filter((movie) => movie.favorite); const now = new Date(); const recentlyWatchedMovies = movies.filter((movie) => { const lastWatched = movie.last_watched; if (!lastWatched) return false; const lastWatchedDate = new Date(lastWatched); if (isNaN(lastWatchedDate.getTime())) return false; const sixMonthsAgo = new Date(); sixMonthsAgo.setMonth(sixMonthsAgo.getMonth() - 6); return lastWatchedDate >= sixMonthsAgo; }); return { movies, watchHistory: movies.filter((movie) => movie.last_watched), recentlyWatched: recentlyWatchedMovies, favorites: favoriteMovies.sort((a, b) => a.title.localeCompare(b.title)), feed: movies.filter((movie) => movie.feed) }; } catch (error) { console.error('Error fetching and processing movies data:', error); return { movies: [], watchHistory: [], recentlyWatched: [], favorites: [], feed: [] }; } }