const FeeCalculator = require('../../../utils/feeCalculator');

describe('FeeCalculator', () => {
  describe('calculateRentalFees', () => {
    it('should calculate fees correctly for whole dollar amounts', () => {
      const result = FeeCalculator.calculateRentalFees(100);

      expect(result.totalAmount).toBe(100);
      expect(result.platformFee).toBe(10); // 10% platform fee
      expect(result.totalChargedAmount).toBe(100);
      expect(result.payoutAmount).toBe(90); // 100 - 10
    });

    it('should calculate fees correctly for decimal amounts', () => {
      const result = FeeCalculator.calculateRentalFees(99.99);

      expect(result.totalAmount).toBe(99.99);
      expect(result.platformFee).toBe(10); // 10% of 99.99, rounded to 2 decimals
      expect(result.totalChargedAmount).toBe(99.99);
      expect(result.payoutAmount).toBe(89.99);
    });

    it('should handle small amounts', () => {
      const result = FeeCalculator.calculateRentalFees(1);

      expect(result.totalAmount).toBe(1);
      expect(result.platformFee).toBe(0.1);
      expect(result.payoutAmount).toBe(0.9);
    });

    it('should handle large amounts', () => {
      const result = FeeCalculator.calculateRentalFees(10000);

      expect(result.totalAmount).toBe(10000);
      expect(result.platformFee).toBe(1000);
      expect(result.payoutAmount).toBe(9000);
    });

    it('should round to 2 decimal places', () => {
      const result = FeeCalculator.calculateRentalFees(33.33);

      expect(result.totalAmount).toBe(33.33);
      expect(result.platformFee).toBe(3.33); // 10% of 33.33
      expect(result.payoutAmount).toBe(30); // 33.33 - 3.33
    });

    it('should handle zero amount', () => {
      const result = FeeCalculator.calculateRentalFees(0);

      expect(result.totalAmount).toBe(0);
      expect(result.platformFee).toBe(0);
      expect(result.totalChargedAmount).toBe(0);
      expect(result.payoutAmount).toBe(0);
    });

    it('should calculate correct fee for amounts requiring rounding', () => {
      const result = FeeCalculator.calculateRentalFees(123.456);

      expect(result.totalAmount).toBe(123.46); // Rounded
      expect(result.platformFee).toBe(12.35); // 10% rounded
      expect(result.payoutAmount).toBe(111.11);
    });
  });

  describe('formatFeesForDisplay', () => {
    it('should format fees as currency strings', () => {
      const fees = {
        totalAmount: 100,
        platformFee: 10,
        totalChargedAmount: 100,
        payoutAmount: 90,
      };

      const result = FeeCalculator.formatFeesForDisplay(fees);

      expect(result.totalAmount).toBe('$100.00');
      expect(result.platformFee).toBe('$10.00 (10%)');
      expect(result.totalCharge).toBe('$100.00');
      expect(result.ownerPayout).toBe('$90.00');
    });

    it('should format decimal amounts correctly', () => {
      const fees = {
        totalAmount: 99.99,
        platformFee: 10,
        totalChargedAmount: 99.99,
        payoutAmount: 89.99,
      };

      const result = FeeCalculator.formatFeesForDisplay(fees);

      expect(result.totalAmount).toBe('$99.99');
      expect(result.platformFee).toBe('$10.00 (10%)');
      expect(result.totalCharge).toBe('$99.99');
      expect(result.ownerPayout).toBe('$89.99');
    });

    it('should handle zero values', () => {
      const fees = {
        totalAmount: 0,
        platformFee: 0,
        totalChargedAmount: 0,
        payoutAmount: 0,
      };

      const result = FeeCalculator.formatFeesForDisplay(fees);

      expect(result.totalAmount).toBe('$0.00');
      expect(result.platformFee).toBe('$0.00 (10%)');
      expect(result.totalCharge).toBe('$0.00');
      expect(result.ownerPayout).toBe('$0.00');
    });

    it('should handle large values', () => {
      const fees = {
        totalAmount: 10000,
        platformFee: 1000,
        totalChargedAmount: 10000,
        payoutAmount: 9000,
      };

      const result = FeeCalculator.formatFeesForDisplay(fees);

      expect(result.totalAmount).toBe('$10000.00');
      expect(result.platformFee).toBe('$1000.00 (10%)');
      expect(result.totalCharge).toBe('$10000.00');
      expect(result.ownerPayout).toBe('$9000.00');
    });
  });

  describe('Integration: calculateRentalFees + formatFeesForDisplay', () => {
    it('should work together correctly', () => {
      const fees = FeeCalculator.calculateRentalFees(250);
      const formatted = FeeCalculator.formatFeesForDisplay(fees);

      expect(formatted.totalAmount).toBe('$250.00');
      expect(formatted.platformFee).toBe('$25.00 (10%)');
      expect(formatted.totalCharge).toBe('$250.00');
      expect(formatted.ownerPayout).toBe('$225.00');
    });
  });
});
