import { Inject, Injectable } from '@nestjs/common';
import { PlayerWalletPayload } from './player.types';

interface IUserWalletsService {
  getBalance(userId: bigint): Promise<PlayerWalletPayload>;
  spend(userId: bigint, amount: number, referenceType: string, referenceId: string, note?: string): Promise<{ wallet: { balance: number } }>;
  earn(userId: bigint, amount: number, referenceType: string, referenceId: string, note?: string): Promise<{ wallet: { balance: number } }>;
}

@Injectable()
export class EconomyService {
  constructor(
    @Inject('IUserWalletsService') private readonly userWalletsService: IUserWalletsService,
  ) {}

  async getWallet(userId: bigint): Promise<PlayerWalletPayload> {
    return await this.userWalletsService.getBalance(userId);
  }

  async spend(userId: bigint, amount: number, referenceType: string, referenceId: string, note?: string): Promise<PlayerWalletPayload> {
    const result = await this.userWalletsService.spend(userId, amount, referenceType, referenceId, note);
    return {
      user_id: userId.toString(),
      balance: result.wallet.balance,
      currency: 'whale_coin',
    };
  }

  async earn(userId: bigint, amount: number, referenceType: string, referenceId: string, note?: string): Promise<PlayerWalletPayload> {
    const result = await this.userWalletsService.earn(userId, amount, referenceType, referenceId, note);
    return {
      user_id: userId.toString(),
      balance: result.wallet.balance,
      currency: 'whale_coin',
    };
  }
}
