import { Collection, Result } from "core/common/helpers";
import { ITransactionManager } from "core/common/infrastructure/database";
import { logger } from "core/common/infrastructure/logger";
import { IInvoiceService, Invoice } from "../domain";

export class ListInvoicesUseCase {
  constructor(
    private readonly invoiceService: IInvoiceService,
    private readonly transactionManager: ITransactionManager
  ) {}

  public execute(): Promise<Result<Collection<Invoice>, Error>> {
    return this.transactionManager.complete(async (transaction) => {
      try {
        return await this.invoiceService.findInvoices(transaction);
      } catch (error: unknown) {
        logger.error(error as Error);
        return Result.fail(error as Error);
      }
    });
  }
}
