// server/api/village/events.get.ts
import { getAuthenticatedUserId } from '../../utils/auth';

export default defineEventHandler(async (event) => {
  const userId = getAuthenticatedUserId(event);

  const village = await prisma.village.findUnique({
    where: { userId },
  });

  if (!village) {
    // Or return empty array, depending on desired behavior for users without villages yet
    return []; 
  }

  const events = await prisma.villageEvent.findMany({
    where: {
      villageId: village.id,
    },
    orderBy: {
      createdAt: 'desc',
    },
    take: 50, // Limit to the last 50 events to avoid overloading the client
  });

  return events;
});
