export const dynamic = 'force-dynamic'; import { createClient } from '@/lib/supabase/server'; import { redirect } from 'next/navigation'; import { checkIsAdmin } from '@/services/track-api-usage'; import Link from 'next/link'; import { ArrowLeft, Wine, User, Calendar, Star, Search, Filter } from 'lucide-react'; import AdminBottlesList from './AdminBottlesList'; export default async function AdminBottlesPage() { const supabase = await createClient(); const { data: { user } } = await supabase.auth.getUser(); if (!user) { redirect('/'); } const isAdmin = await checkIsAdmin(user.id); if (!isAdmin) { redirect('/'); } // Fetch all bottles from all users with user info const { data: bottlesRaw, error } = await supabase .from('bottles') .select(` id, name, distillery, image_url, abv, age, category, status, created_at, user_id, tastings ( id, rating ) `) .order('created_at', { ascending: false }) .limit(500); // Get unique user IDs const userIds = Array.from(new Set(bottlesRaw?.map(b => b.user_id) || [])); // Fetch profiles for these users const { data: profiles } = userIds.length > 0 ? await supabase.from('profiles').select('id, username, display_name').in('id', userIds) : { data: [] }; // Combine bottles with user info const bottles = bottlesRaw?.map(bottle => ({ ...bottle, user: profiles?.find(p => p.id === bottle.user_id) || { username: 'Unknown', display_name: null } })) || []; // Calculate stats const stats = { totalBottles: bottles.length, totalUsers: userIds.length, avgRating: bottles.reduce((sum, b) => { const ratings = b.tastings?.map((t: any) => t.rating).filter((r: number) => r > 0) || []; const avg = ratings.length > 0 ? ratings.reduce((a: number, b: number) => a + b, 0) / ratings.length : 0; return sum + avg; }, 0) / bottles.filter(b => b.tastings && b.tastings.length > 0).length || 0, topDistilleries: Object.entries( bottles.reduce((acc: Record, b) => { const d = b.distillery || 'Unknown'; acc[d] = (acc[d] || 0) + 1; return acc; }, {}) ).sort((a, b) => b[1] - a[1]).slice(0, 5), }; return (
{/* Header */}

All Bottles

View all scanned bottles from all users

{error && (
Error loading bottles: {error.message}
)} {/* Stats Cards */}
Total Bottles
{stats.totalBottles}
Total Users
{stats.totalUsers}
Avg Rating
{stats.avgRating > 0 ? stats.avgRating.toFixed(1) : 'N/A'}
Top Distillery
{stats.topDistilleries[0]?.[0] || 'N/A'}
{stats.topDistilleries[0] && (
{stats.topDistilleries[0][1]} bottles
)}
{/* Top Distilleries */}

Top 5 Distilleries

{stats.topDistilleries.map(([name, count]) => ( {name} ({count}) ))}
{/* Bottles List - Client Component for search/filter */}
); }