import { ExchangeRateProvider } from '../../src/store/models/exchange-rate-provider';

describe('ExchangeRateProvider Model', () => {
  describe('recordSuccess logic', () => {
    it('should reset failure count and update status to ok', () => {
      // Test the logic for recordSuccess
      const updates = {
        last_success_at: expect.any(Date),
        failure_count: 0,
        status: 'ok',
      };

      expect(updates.failure_count).toBe(0);
      expect(updates.status).toBe('ok');
    });
  });

  describe('recordFailure logic', () => {
    it('should increment failure count and set status to degraded', () => {
      const currentFailureCount = 0;
      const newFailureCount = currentFailureCount + 1;

      const updates: any = {
        last_failure_at: new Date(),
        failure_count: newFailureCount,
      };

      // Auto-pause after 5 consecutive failures
      if (newFailureCount >= 5) {
        updates.status = 'paused';
        updates.paused_reason = 'Test error';
      } else {
        updates.status = 'degraded';
      }

      expect(updates.failure_count).toBe(1);
      expect(updates.status).toBe('degraded');
    });

    it('should set status to paused after 5 consecutive failures', () => {
      const currentFailureCount = 4;
      const newFailureCount = currentFailureCount + 1;
      const reason = 'Test error';

      const updates: any = {
        last_failure_at: new Date(),
        failure_count: newFailureCount,
      };

      if (newFailureCount >= 5) {
        updates.status = 'paused';
        updates.paused_reason = reason || 'Too many consecutive failures';
      } else {
        updates.status = 'degraded';
      }

      expect(updates.failure_count).toBe(5);
      expect(updates.status).toBe('paused');
      expect(updates.paused_reason).toBe('Test error');
    });

    it('should use default reason if none provided', () => {
      const currentFailureCount = 4;
      const newFailureCount = currentFailureCount + 1;
      const reason = undefined;

      const updates: any = {
        last_failure_at: new Date(),
        failure_count: newFailureCount,
      };

      if (newFailureCount >= 5) {
        updates.status = 'paused';
        updates.paused_reason = reason || 'Too many consecutive failures';
      } else {
        updates.status = 'degraded';
      }

      expect(updates.paused_reason).toBe('Too many consecutive failures');
    });

    it('should not pause before reaching 5 failures', () => {
      const currentFailureCount = 3;
      const newFailureCount = currentFailureCount + 1;

      const updates: any = {
        last_failure_at: new Date(),
        failure_count: newFailureCount,
      };

      if (newFailureCount >= 5) {
        updates.status = 'paused';
        updates.paused_reason = 'Test error';
      } else {
        updates.status = 'degraded';
      }

      expect(updates.failure_count).toBe(4);
      expect(updates.status).toBe('degraded');
    });
  });

  describe('Status values', () => {
    it('should have valid status enum values', () => {
      const validStatuses = ['ok', 'degraded', 'paused', 'down'];

      validStatuses.forEach((status) => {
        expect(validStatuses).toContain(status);
      });
    });
  });

  describe('Provider priority', () => {
    it('should support priority ordering', () => {
      const provider1 = { priority: 1 } as ExchangeRateProvider;
      const provider2 = { priority: 2 } as ExchangeRateProvider;

      expect(provider1.priority).toBeLessThan(provider2.priority);
    });
  });
});
