class ChatRepository {
  constructor(prisma) { this.prisma = prisma; }
  findByParticipants(senderId, receiverId, page = 1, limit = 50) {
    return this.prisma.chatMessage.findMany({
      where: { OR: [{ AND: [{ senderId }, { receiverId }] }, { AND: [{ senderId: receiverId }, { receiverId: senderId }] }] },
      include: { sender: { include: { profile: true } }, receiver: { include: { profile: true } } },
      skip: (page - 1) * limit,
      take: limit,
      orderBy: { createdAt: 'asc' },
    });
  }
  findAll(page = 1, limit = 50) {
    return this.prisma.chatMessage.findMany({
      include: { sender: { include: { profile: true } }, receiver: { include: { profile: true } } },
      skip: (page - 1) * limit,
      take: limit,
      orderBy: { createdAt: 'desc' },
    });
  }
  create(data) { return this.prisma.chatMessage.create({ data }); }
  markRead(id) { return this.prisma.chatMessage.update({ where: { id }, data: { isRead: true } }); }
  countUnread(userId) { return this.prisma.chatMessage.count({ where: { receiverId: userId, isRead: false } }); }
}

export default ChatRepository;
