import { injectable, unmanaged } from 'inversify';
import { PaymentsApiClient } from '@/lib/api/payments/PaymentsApiClient';
import { MembershipFeeViewModel } from '@/lib/view-models/MembershipFeeViewModel';
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 MembershipFeeService 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 getMembershipFee(leagueId: string): Promise<MembershipFeeViewModel | null> {
    const data = await this.apiClient.getMembershipFees({ leagueId });
    if (!data.fee) return null;
    return new MembershipFeeViewModel(data.fee);
  }
}
