import { UniqueID } from "@/core/common/domain";
import { ITransactionManager } from "@/core/common/infrastructure/database";
import { logger } from "@/core/logger";
import { Result } from "@repo/rdx-utils";
import { IInvoiceService, Invoice } from "../domain";

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

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