import { describe, expect, it } from 'bun:test';
import { unifiedAppSchema, toUnifiedConfig, getStandardServiceName } from '../unified-app.schema';

describe('UnifiedAppConfig', () => {
  describe('getStandardServiceName', () => {
    it('should convert camelCase to kebab-case', () => {
      expect(getStandardServiceName('dataIngestion')).toBe('data-ingestion');
      expect(getStandardServiceName('dataPipeline')).toBe('data-pipeline');
      expect(getStandardServiceName('webApi')).toBe('web-api');
    });

    it('should handle already kebab-case names', () => {
      expect(getStandardServiceName('data-ingestion')).toBe('data-ingestion');
      expect(getStandardServiceName('web-api')).toBe('web-api');
    });

    it('should handle single word names', () => {
      expect(getStandardServiceName('api')).toBe('api');
      expect(getStandardServiceName('worker')).toBe('worker');
    });
  });

  describe('unifiedAppSchema transform', () => {
    it('should set serviceName from name if not provided', () => {
      const config = {
        name: 'test-app',
        version: '1.0.0',
        service: {
          name: 'webApi',
          port: 3000,
        },
        log: { level: 'info' },
      };

      const result = unifiedAppSchema.parse(config);
      expect(result.service.serviceName).toBe('web-api');
    });

    it('should keep existing serviceName if provided', () => {
      const config = {
        name: 'test-app',
        version: '1.0.0',
        service: {
          name: 'webApi',
          serviceName: 'custom-name',
          port: 3000,
        },
        log: { level: 'info' },
      };

      const result = unifiedAppSchema.parse(config);
      expect(result.service.serviceName).toBe('custom-name');
    });

    it('should sync nested and flat database configs', () => {
      const config = {
        name: 'test-app',
        version: '1.0.0',
        service: { name: 'test', port: 3000 },
        log: { level: 'info' },
        database: {
          postgres: {
            host: 'localhost',
            port: 5432,
            database: 'test',
            user: 'user',
            password: 'pass',
          },
          mongodb: {
            uri: 'mongodb://localhost:27017',
            database: 'test',
          },
        },
      };

      const result = unifiedAppSchema.parse(config);
      
      // Should have both nested and flat structure
      expect(result.postgres).toBeDefined();
      expect(result.mongodb).toBeDefined();
      expect(result.database?.postgres).toBeDefined();
      expect(result.database?.mongodb).toBeDefined();
      
      // Values should match
      expect(result.postgres?.host).toBe('localhost');
      expect(result.postgres?.port).toBe(5432);
      expect(result.mongodb?.uri).toBe('mongodb://localhost:27017');
    });

    it('should handle questdb ilpPort to influxPort mapping', () => {
      const config = {
        name: 'test-app',
        version: '1.0.0',
        service: { name: 'test', port: 3000 },
        log: { level: 'info' },
        database: {
          questdb: {
            host: 'localhost',
            ilpPort: 9009,
            httpPort: 9000,
            pgPort: 8812,
            database: 'questdb',
          },
        },
      };

      const result = unifiedAppSchema.parse(config);
      expect(result.questdb).toBeDefined();
      expect((result.questdb as any).influxPort).toBe(9009);
    });
  });

  describe('toUnifiedConfig', () => {
    it('should convert StockBotAppConfig to UnifiedAppConfig', () => {
      const stockBotConfig = {
        name: 'stock-bot',
        version: '1.0.0',
        environment: 'development',
        service: {
          name: 'dataIngestion',
          port: 3001,
          host: '0.0.0.0',
        },
        log: {
          level: 'info',
          format: 'json',
        },
        database: {
          postgres: {
            enabled: true,
            host: 'localhost',
            port: 5432,
            database: 'stock',
            user: 'user',
            password: 'pass',
          },
          dragonfly: {
            enabled: true,
            host: 'localhost',
            port: 6379,
            db: 0,
          },
        },
      };

      const unified = toUnifiedConfig(stockBotConfig);
      
      expect(unified.service.serviceName).toBe('data-ingestion');
      expect(unified.redis).toBeDefined();
      expect(unified.redis?.host).toBe('localhost');
      expect(unified.postgres).toBeDefined();
      expect(unified.postgres?.host).toBe('localhost');
    });
  });
});