import { Inject } from '@nestjs/common';
import { type IQueryHandler, QueryHandler } from '@nestjs/cqrs';
import { type ReportEntity } from '../../../domain/entities/report.entity';
import { REPORT_REPOSITORY, type IReportRepository } from '../../../domain/repositories/report.repository';
import { ListReportsQuery } from './list-reports.query';

export interface ListReportsResult {
  reports: ReportEntity[];
  total: number;
}

@QueryHandler(ListReportsQuery)
export class ListReportsHandler implements IQueryHandler<ListReportsQuery, ListReportsResult> {
  constructor(
    @Inject(REPORT_REPOSITORY) private readonly reportRepo: IReportRepository,
  ) {}

  async execute(query: ListReportsQuery): Promise<ListReportsResult> {
    return this.reportRepo.findByUserId({
      userId: query.userId,
      type: query.type,
      limit: query.limit,
      offset: query.offset,
    });
  }
}
