"use server";

import { getCurrentUser } from "@/lib/auth/user";
import {
  getNotificationsForUser,
  DashboardNotification,
} from "@/lib/services/notification.service";

export async function getNotificationsAction(): Promise<{
  success: boolean;
  notifications: DashboardNotification[];
  unreadCount: number;
}> {
  try {
    const actor = await getCurrentUser().catch(() => null);
    const notifications = await getNotificationsForUser(actor);
    const unreadCount = notifications.filter((n) => n.unread).length;

    return {
      success: true,
      notifications,
      unreadCount,
    };
  } catch (err: any) {
    console.error("[Get Notifications Action Error]:", err);
    return {
      success: false,
      notifications: [],
      unreadCount: 0,
    };
  }
}
