import type { Actions, PageServerLoad } from './$types';
import { fail } from '@sveltejs/kit';
import {
  deleteNotification,
  listNotifications,
  markAllRead,
  markRead
} from '$lib/server/notifications';

export const load: PageServerLoad = () => {
  return { notifications: listNotifications() };
};

export const actions: Actions = {
  markRead: async ({ request }) => {
    const form = await request.formData();
    const id = String(form.get('id') ?? '');
    if (!id) return fail(400, { error: 'ID mancante.' });
    markRead(id);
    return { success: true };
  },
  markAllRead: async () => {
    markAllRead();
    return { success: true, allRead: true };
  },
  delete: async ({ request }) => {
    const form = await request.formData();
    const id = String(form.get('id') ?? '');
    if (!id) return fail(400, { error: 'ID mancante.' });
    deleteNotification(id);
    return { success: true };
  }
};
