import { Injectable, Inject } from '@nestjs/common';
import { MESSAGE_REPOSITORY, IMessageRepository } from '../../domain/repositories/message.repository.interface';

@Injectable()
export class MessageService {
  constructor(
    @Inject(MESSAGE_REPOSITORY) private readonly msgRepo: IMessageRepository,
  ) {}

  async getMessages(userId: string, page: number, limit: number) {
    const [items, total] = await this.msgRepo.findByUserId(userId, page, limit);
    return { items, total, page, limit };
  }

  async markAsRead(messageId: string, userId: string) {
    await this.msgRepo.markAsRead(messageId, userId);
  }

  async markAllAsRead(userId: string) {
    await this.msgRepo.markAllAsRead(userId);
  }

  async getUnreadCount(userId: string) {
    return this.msgRepo.countUnread(userId);
  }

  async createMessage(userId: string, type: string, title: string, content: string, metadata?: any) {
    return this.msgRepo.create({ userId, type, title, content, metadata });
  }
}
