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

describe('Billing Integration Tests', () => {
  let billingService: BillingService;
  let testCustomerId: string;

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

  describe('Tier Configuration', () => {
    it('should load tier configurations correctly', () => {
      const config = loadBillingConfig();
      
      expect(config.tiers.free.callMinutesLimit).toBe(100);
      expect(config.tiers.basic.callMinutesLimit).toBe(500);
      expect(config.tiers.plus.callMinutesLimit).toBe(2000);
      expect(config.tiers.premium.callMinutesLimit).toBe(10000);
    });

    it('should have increasing limits across tiers', () => {
      const config = loadBillingConfig();
      
      expect(config.tiers.free.callMinutesLimit).toBeLessThan(
        config.tiers.basic.callMinutesLimit
      );
      expect(config.tiers.basic.callMinutesLimit).toBeLessThan(
        config.tiers.plus.callMinutesLimit
      );
      expect(config.tiers.plus.callMinutesLimit).toBeLessThan(
        config.tiers.premium.callMinutesLimit
      );
    });
  });

  describe('Usage Limits', () => {
    it('should check usage within limit', async () => {
      const result = await billingService.checkUsageAgainstLimit(
        'user_test',
        'plus' as SubscriptionTier,
        1000
      );

      expect(result.withinLimit).toBe(true);
      expect(result.limit).toBe(2000);
      expect(result.remaining).toBe(1000);
    });

    it('should detect usage exceeding limit', async () => {
      const result = await billingService.checkUsageAgainstLimit(
        'user_test',
        'basic' as SubscriptionTier,
        600
      );

      expect(result.withinLimit).toBe(false);
      expect(result.remaining).toBe(0);
      expect(result.limit).toBe(500);
    });

    it('should return correct remaining minutes', async () => {
      const result = await billingService.checkUsageAgainstLimit(
        'user_test',
        'plus' as SubscriptionTier,
        1500
      );

      expect(result.remaining).toBe(500);
    });
  });

  describe('Tier Limits', () => {
    it('should return correct limits for each tier', async () => {
      const free = await billingService.getTierLimits('free' as SubscriptionTier);
      const basic = await billingService.getTierLimits('basic' as SubscriptionTier);
      const plus = await billingService.getTierLimits('plus' as SubscriptionTier);
      const premium = await billingService.getTierLimits('premium' as SubscriptionTier);

      expect(free.callMinutesLimit).toBe(100);
      expect(basic.callMinutesLimit).toBe(500);
      expect(plus.callMinutesLimit).toBe(2000);
      expect(premium.callMinutesLimit).toBe(10000);

      expect(free.smsCountLimit).toBe(500);
      expect(basic.smsCountLimit).toBe(2000);
      expect(plus.smsCountLimit).toBe(10000);
      expect(premium.smsCountLimit).toBe(50000);
    });
  });
});
