import { injectable, unmanaged } from 'inversify';
import { PaymentsApiClient } from '@/lib/api/payments/PaymentsApiClient';
import { WalletViewModel } from '@/lib/view-models/WalletViewModel';
import { getWebsiteApiBaseUrl } from '@/lib/config/apiBaseUrl';
import { ConsoleLogger } from '@/lib/infrastructure/logging/ConsoleLogger';
import { EnhancedErrorReporter } from '@/lib/infrastructure/EnhancedErrorReporter';
import { Service } from '@/lib/contracts/services/Service';

@injectable()
export class WalletService implements Service {
  private readonly apiClient: PaymentsApiClient;

  constructor(@unmanaged() apiClient?: PaymentsApiClient) {
    if (apiClient) {
      this.apiClient = apiClient;
    } else {
      const baseUrl = getWebsiteApiBaseUrl();
      const logger = new ConsoleLogger();
      const errorReporter = new EnhancedErrorReporter(logger);
      this.apiClient = new PaymentsApiClient(baseUrl, errorReporter, logger);
    }
  }

  async getWallet(leagueId: string): Promise<WalletViewModel> {
    const data = await this.apiClient.getWallet({ leagueId });
    return new WalletViewModel({ ...data.wallet, transactions: data.transactions });
  }
}
