import * as acquirersService from './acquirers.service';
import * as paymentSchedulesService from './paymentSchedules.service';
import * as documentsService from './documents.service';
import * as notificationsService from './notifications.service';

export const getDashboardData = async (userId: number, userRole: string) => {
  // 1. Récupérer les profils acquéreurs de l'utilisateur (il peut avoir acheté plusieurs lots)
  const profiles = await acquirersService.getAcquirerProfilesForUser(userId);
  
  if (profiles.length === 0) {
    return {
      profiles: [],
      schedules: [],
      documents: [],
      notifications: [],
      unread_notifications_count: 0
    };
  }

  // 2. Pour chaque profil, récupérer son échéancier
  const schedules = await Promise.all(
    profiles.map(p => paymentSchedulesService.getScheduleByAcquirerId(p.id))
  );

  // 3. Récupérer les documents de l'utilisateur
  const documents = await documentsService.getDocumentsByOwner(userId, userId, userRole);

  // 4. Récupérer les notifications non lues ou récentes (ici on prend tout pour simplifier)
  const notifications = await notificationsService.getUserNotifications(userId);
  const unreadCount = await notificationsService.getUnreadCount(userId);

  return {
    profiles,
    schedules: schedules.filter(s => s !== null),
    documents,
    notifications,
    unread_notifications_count: unreadCount
  };
};
