import { describe, expect, it } from 'bun:test';
import {
  generateCachePrefix,
  getFullQueueName,
  normalizeServiceName,
  parseQueueName,
} from '../src/service-utils';

describe('Service Utils', () => {
  describe('normalizeServiceName', () => {
    it('should convert camelCase to kebab-case', () => {
      expect(normalizeServiceName('myService')).toBe('my-service');
      expect(normalizeServiceName('userAuthService')).toBe('user-auth-service');
      expect(normalizeServiceName('APIGateway')).toBe('apigateway');
    });

    it('should handle already kebab-case names', () => {
      expect(normalizeServiceName('my-service')).toBe('my-service');
      expect(normalizeServiceName('user-auth-service')).toBe('user-auth-service');
    });

    it('should handle single word names', () => {
      expect(normalizeServiceName('service')).toBe('service');
      expect(normalizeServiceName('API')).toBe('api');
    });

    it('should handle empty string', () => {
      expect(normalizeServiceName('')).toBe('');
    });
  });

  describe('getFullQueueName', () => {
    it('should generate queue name with service and handler', () => {
      const name = getFullQueueName('userService', 'processUser');
      expect(name).toBe('{user-service_processUser}');
    });

    it('should normalize service name but keep handler as-is', () => {
      const name = getFullQueueName('APIGateway', 'handleRequest');
      expect(name).toBe('{apigateway_handleRequest}');
    });

    it('should handle already normalized service name', () => {
      const name = getFullQueueName('user-service', 'process-user');
      expect(name).toBe('{user-service_process-user}');
    });
  });

  describe('generateCachePrefix', () => {
    it('should generate cache prefix for service', () => {
      const prefix = generateCachePrefix('userService');
      expect(prefix).toBe('cache:user-service');
    });

    it('should generate cache prefix for service only', () => {
      const prefix = generateCachePrefix('userService');
      expect(prefix).toBe('cache:user-service');
    });

    it('should normalize service names', () => {
      const prefix = generateCachePrefix('APIGateway');
      expect(prefix).toBe('cache:apigateway');
    });
  });

  describe('parseQueueName', () => {
    it('should parse valid queue name', () => {
      const result = parseQueueName('{user-service_process-user}');
      expect(result).toEqual({
        service: 'user-service',
        handler: 'process-user',
      });
    });

    it('should return null for invalid format', () => {
      expect(parseQueueName('invalid-queue-name')).toBeNull();
      expect(parseQueueName('user-service_process-user')).toBeNull(); // Missing braces
      expect(parseQueueName('{user-service-process-user}')).toBeNull(); // Missing underscore
      expect(parseQueueName('{user-service_}')).toBeNull(); // Missing handler
      expect(parseQueueName('{_process-user}')).toBeNull(); // Missing service
    });

    it('should handle queue names with multiple underscores', () => {
      const result = parseQueueName('{user_service_process_user}');
      expect(result).toEqual({
        service: 'user',
        handler: 'service_process_user',
      });
    });
  });

  describe('complete service utils workflow', () => {
    it('should handle full queue name generation and parsing', () => {
      // Generate a queue name
      const serviceName = 'userService';
      const handlerName = 'processOrder';
      const queueName = getFullQueueName(serviceName, handlerName);

      expect(queueName).toBe('{user-service_processOrder}');

      // Parse it back
      const parsed = parseQueueName(queueName);
      expect(parsed).toEqual({
        service: 'user-service',
        handler: 'processOrder',
      });
    });

    it('should handle cache prefix generation', () => {
      const serviceName = 'orderService';
      const cachePrefix = generateCachePrefix(serviceName);

      expect(cachePrefix).toBe('cache:order-service');

      // Use it for cache keys
      const cacheKey = `${cachePrefix}:user:123`;
      expect(cacheKey).toBe('cache:order-service:user:123');
    });
  });
});
