import { Injectable } from '@nestjs/common';
import { EntityManager, EntityRepository } from '@mikro-orm/postgresql';
import { WalletTransaction } from '../entities/wallet-transaction.entity';

@Injectable()
export class WalletTransactionRepository extends EntityRepository<WalletTransaction> {
  constructor(readonly em: EntityManager) {
    super(em, WalletTransaction);
  }
  async getCurrentWalletBalance(userId: string, restaurantId: string): Promise<number> {
    const walletTransaction = await this.em.findOne(
      WalletTransaction,
      {
        user: { id: userId },
        restaurant: { id: restaurantId },
      },
      { orderBy: { createdAt: 'desc' } },
    );
    return walletTransaction?.balance || 0;
  }
}
