import { Price } from '../../src/store/models/price';

describe('Price Model - Dynamic Pricing', () => {
  describe('formatBeforeSave - Dynamic Pricing Validation', () => {
    it('should validate dynamic pricing requires base_currency and base_amount', () => {
      const price = {
        pricing_type: 'dynamic' as const,
        // Missing base_currency and base_amount
      };

      expect(() => Price.formatBeforeSave(price)).toThrow(
        'base_currency and base_amount are required for dynamic pricing'
      );
    });

    it('should validate only USD is supported as base_currency', () => {
      const price = {
        pricing_type: 'dynamic' as const,
        base_currency: 'EUR',
        base_amount: '100',
      };

      expect(() => Price.formatBeforeSave(price)).toThrow('Only USD is supported as base_currency');
    });

    it('should accept valid dynamic pricing with USD', () => {
      const price = {
        pricing_type: 'dynamic' as const,
        base_currency: 'USD',
        base_amount: '100',
      };

      const result = Price.formatBeforeSave(price);

      expect(result.pricing_type).toBe('dynamic');
      expect(result.base_currency).toBe('USD');
      expect(result.base_amount).toBe('100');
    });

    it('should set default lock_duration if not provided', () => {
      const price = {
        pricing_type: 'dynamic' as const,
        base_currency: 'USD',
        base_amount: '100',
      };

      const result = Price.formatBeforeSave(price);

      expect(result.dynamic_pricing_config).toEqual({ lock_duration: 30 });
    });

    it('should preserve custom lock_duration if provided', () => {
      const price = {
        pricing_type: 'dynamic' as const,
        base_currency: 'USD',
        base_amount: '100',
        dynamic_pricing_config: { lock_duration: 600 },
      };

      const result = Price.formatBeforeSave(price);

      expect(result.dynamic_pricing_config).toEqual({ lock_duration: 600 });
    });

    it('should clear dynamic pricing fields for fixed prices', () => {
      const price = {
        pricing_type: 'fixed' as const,
        base_currency: 'USD',
        base_amount: '100',
        dynamic_pricing_config: { lock_duration: 300 },
      };

      const result = Price.formatBeforeSave(price);

      expect(result.pricing_type).toBe('fixed');
      expect(result.base_currency).toBeNull();
      expect(result.base_amount).toBeNull();
      expect(result.dynamic_pricing_config).toBeNull();
    });

    it('should not modify other price properties', () => {
      const price = {
        pricing_type: 'dynamic' as const,
        base_currency: 'USD',
        base_amount: '100',
        type: 'one_time' as const,
        billing_scheme: 'per_unit' as const,
        unit_amount: '500',
        metadata: { key: 'value' },
      };

      const result = Price.formatBeforeSave(price);

      expect(result.type).toBe('one_time');
      expect(result.billing_scheme).toBe('per_unit');
      expect(result.unit_amount).toBe('500');
      expect(result.metadata).toEqual({ key: 'value' });
    });
  });
});
