import { UniqueID } from "core/common/domain";
import { 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 DeleteInvoiceUseCase {
  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.deleteInvoiceById(invoiceID, transaction);
      } catch (error: unknown) {
        logger.error(error as Error);
        return Result.fail(error as Error);
      }
    });
  }
}
