import { describe, it, expect, beforeAll } from '@jest/globals';
import { BillingService } from '@shieldai/shared-billing';
import { SubscriptionTier } from '@shieldai/shared-billing';

describe('Billing Performance Benchmarks', () => {
  let billingService: BillingService;
  const iterations = 1000;

  beforeAll(() => {
    billingService = BillingService.getInstance();
  });

  describe('Tier Limit Checks', () => {
    it('should check tier limits within 1ms', async () => {
      const startTime = performance.now();

      for (let i = 0; i < iterations; i++) {
        await billingService.getTierLimits('plus' as SubscriptionTier);
      }

      const endTime = performance.now();
      const avgTime = (endTime - startTime) / iterations;

      expect(avgTime).toBeLessThan(1);
    });

    it('should check usage against limit within 1ms', async () => {
      const startTime = performance.now();

      for (let i = 0; i < iterations; i++) {
        await billingService.checkUsageAgainstLimit(
          `user_${i}`,
          'plus' as SubscriptionTier,
          1000
        );
      }

      const endTime = performance.now();
      const avgTime = (endTime - startTime) / iterations;

      expect(avgTime).toBeLessThan(1);
    });
  });

  describe('Concurrency', () => {
    it('should handle 100 concurrent limit checks', async () => {
      const promises = Array.from({ length: 100 }, (_, i) =>
        billingService.checkUsageAgainstLimit(
          `user_${i}`,
          'plus' as SubscriptionTier,
          1000 + i
        )
      );

      const startTime = performance.now();
      const results = await Promise.all(promises);
      const endTime = performance.now();

      expect(results).toHaveLength(100);
      expect(endTime - startTime).toBeLessThan(100);
    });
  });
});
