import { Account, IAccountService } from "@/contexts/accounts/domain";
import { ITransactionManager } from "@/core/common/infrastructure/database";
import { Collection, Result } from "@repo/rdx-utils";

export class ListAccountsUseCase {
  constructor(
    private readonly accountService: IAccountService,
    private readonly transactionManager: ITransactionManager
  ) {}

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