import { Response } from 'express';
import prisma from '../../config/database';
import { AuthRequest } from '../../shared/middleware/auth';
import { ResponseFormatter } from '../../shared/utils/responseFormatter';

class DashboardController {
  async getStats(req: AuthRequest, res: Response) {
    const userId = req.user!.id;

    const [contactsCount, activeTasksCount, unreadNotificationsCount] = await Promise.all([
      prisma.contact.count({
        where: {
          archivedAt: null,
        },
      }),
      prisma.task.count({
        where: {
          status: { notIn: ['COMPLETED', 'CANCELLED'] },
        },
      }),
      prisma.notification.count({
        where: {
          userId,
          isRead: false,
        },
      }),
    ]);

    res.json(
      ResponseFormatter.success({
        contacts: contactsCount,
        activeTasks: activeTasksCount,
        notifications: unreadNotificationsCount,
      })
    );
  }
}

export default new DashboardController();
