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

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

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