import { registerAs } from '@nestjs/config';

export default registerAs('environment', () => ({
  // Environment
  nodeEnv: process.env.NODE_ENV || 'development',
  port: parseInt(process.env.API_PORT, 10) || 3020,
  apiUrl: process.env.API_URL || 'http://localhost:3020',
  appVersion: process.env.APP_VERSION || '1.0.0',

  // Environment checks
  isProduction: process.env.NODE_ENV === 'production',
  isStaging: process.env.NODE_ENV === 'staging',
  isDevelopment: process.env.NODE_ENV === 'development' || !process.env.NODE_ENV,

  // Database
  database: {
    host: process.env.DATABASE_HOST || 'localhost',
    port: parseInt(process.env.DATABASE_PORT, 10) || 5432,
    name: process.env.DATABASE_NAME || 'maternal_app',
    user: process.env.DATABASE_USER || 'maternal_user',
    password: process.env.DATABASE_PASSWORD,
    ssl: process.env.DATABASE_SSL === 'true',
    sslRejectUnauthorized: process.env.DATABASE_SSL_REJECT_UNAUTHORIZED !== 'false',
    connectionPoolMin: parseInt(process.env.DATABASE_CONNECTION_POOL_MIN, 10) || 2,
    connectionPoolMax: parseInt(process.env.DATABASE_CONNECTION_POOL_MAX, 10) || 10,
  },

  // Redis
  redis: {
    host: process.env.REDIS_HOST || 'localhost',
    port: parseInt(process.env.REDIS_PORT, 10) || 6379,
    password: process.env.REDIS_PASSWORD || undefined,
    url: process.env.REDIS_URL,
    ttl: parseInt(process.env.REDIS_TTL, 10) || 3600,
    tls: process.env.REDIS_TLS === 'true',
  },

  // MongoDB
  mongodb: {
    uri: process.env.MONGODB_URI || 'mongodb://localhost:27017/maternal_ai_chat',
  },

  // MinIO / S3
  minio: {
    endpoint: process.env.MINIO_ENDPOINT || 'localhost',
    port: parseInt(process.env.MINIO_PORT, 10) || 9000,
    useSSL: process.env.MINIO_USE_SSL === 'true',
    accessKey: process.env.MINIO_ACCESS_KEY,
    secretKey: process.env.MINIO_SECRET_KEY,
    bucket: process.env.MINIO_BUCKET || 'maternal-files',
    region: process.env.MINIO_REGION || 'us-east-1',
  },

  // JWT
  jwt: {
    secret: process.env.JWT_SECRET,
    expiration: process.env.JWT_EXPIRATION || '1h',
    refreshSecret: process.env.JWT_REFRESH_SECRET,
    refreshExpiration: process.env.JWT_REFRESH_EXPIRATION || '7d',
  },

  // AI Services
  ai: {
    provider: process.env.AI_PROVIDER || 'azure',
    openai: {
      apiKey: process.env.OPENAI_API_KEY,
      model: process.env.OPENAI_MODEL || 'gpt-4o-mini',
      embeddingModel: process.env.OPENAI_EMBEDDING_MODEL || 'text-embedding-3-small',
      maxTokens: parseInt(process.env.OPENAI_MAX_TOKENS, 10) || 1000,
    },
    azure: {
      enabled: process.env.AZURE_OPENAI_ENABLED === 'true',
      chat: {
        endpoint: process.env.AZURE_OPENAI_CHAT_ENDPOINT,
        deployment: process.env.AZURE_OPENAI_CHAT_DEPLOYMENT,
        apiVersion: process.env.AZURE_OPENAI_CHAT_API_VERSION || '2025-04-01-preview',
        apiKey: process.env.AZURE_OPENAI_CHAT_API_KEY,
        maxTokens: parseInt(process.env.AZURE_OPENAI_CHAT_MAX_TOKENS, 10) || 1000,
        reasoningEffort: process.env.AZURE_OPENAI_REASONING_EFFORT || 'medium',
      },
      whisper: {
        endpoint: process.env.AZURE_OPENAI_WHISPER_ENDPOINT,
        deployment: process.env.AZURE_OPENAI_WHISPER_DEPLOYMENT || 'whisper',
        apiVersion: process.env.AZURE_OPENAI_WHISPER_API_VERSION || '2024-06-01',
        apiKey: process.env.AZURE_OPENAI_WHISPER_API_KEY,
      },
      embeddings: {
        endpoint: process.env.AZURE_OPENAI_EMBEDDINGS_ENDPOINT,
        deployment: process.env.AZURE_OPENAI_EMBEDDINGS_DEPLOYMENT || 'text-embedding-ada-002',
        apiVersion: process.env.AZURE_OPENAI_EMBEDDINGS_API_VERSION || '2023-05-15',
        apiKey: process.env.AZURE_OPENAI_EMBEDDINGS_API_KEY,
      },
    },
  },

  // Security
  security: {
    corsOrigins: process.env.CORS_ORIGIN?.split(',') || ['http://localhost:3030'],
    rateLimitTTL: parseInt(process.env.RATE_LIMIT_TTL, 10) || 60,
    rateLimitMax: parseInt(process.env.RATE_LIMIT_MAX, 10) || 100,
  },

  // Logging
  logging: {
    level: process.env.LOG_LEVEL || 'info',
    dir: process.env.LOG_DIR || 'logs',
  },

  // Error Tracking
  sentry: {
    enabled: process.env.SENTRY_ENABLED === 'true',
    dsn: process.env.SENTRY_DSN,
    environment: process.env.SENTRY_ENVIRONMENT || process.env.NODE_ENV || 'development',
    sampleRate: parseFloat(process.env.SENTRY_SAMPLE_RATE) || 1.0,
    tracesSampleRate: parseFloat(process.env.SENTRY_TRACES_SAMPLE_RATE) || 0.1,
    profilesSampleRate: parseFloat(process.env.SENTRY_PROFILES_SAMPLE_RATE) || 0.1,
  },

  // Analytics
  analytics: {
    enabled: process.env.ANALYTICS_ENABLED === 'true',
    provider: process.env.ANALYTICS_PROVIDER || 'posthog',
    posthog: {
      apiKey: process.env.POSTHOG_API_KEY,
      host: process.env.POSTHOG_HOST || 'https://app.posthog.com',
    },
  },

  // Email
  email: {
    mailgun: {
      apiKey: process.env.MAILGUN_API_KEY,
      domain: process.env.MAILGUN_DOMAIN,
      region: process.env.MAILGUN_REGION || 'eu',
    },
    from: process.env.EMAIL_FROM || 'noreply@maternal-app.com',
    fromName: process.env.EMAIL_FROM_NAME || 'Maternal App',
    appUrl: process.env.APP_URL || 'http://localhost:3030',
  },

  // Backups
  backups: {
    enabled: process.env.BACKUP_ENABLED === 'true',
    schedule: process.env.BACKUP_SCHEDULE || '0 2 * * *',
    retentionDays: parseInt(process.env.BACKUP_RETENTION_DAYS, 10) || 30,
    s3: {
      bucket: process.env.BACKUP_S3_BUCKET,
      region: process.env.BACKUP_S3_REGION || 'us-east-1',
      accessKey: process.env.BACKUP_S3_ACCESS_KEY,
      secretKey: process.env.BACKUP_S3_SECRET_KEY,
    },
  },

  // Health Checks
  healthCheck: {
    enabled: process.env.HEALTH_CHECK_ENABLED !== 'false',
    dbTimeout: parseInt(process.env.HEALTH_CHECK_DB_TIMEOUT, 10) || 5000,
    redisTimeout: parseInt(process.env.HEALTH_CHECK_REDIS_TIMEOUT, 10) || 3000,
    mongodbTimeout: parseInt(process.env.HEALTH_CHECK_MONGODB_TIMEOUT, 10) || 5000,
  },

  // Performance
  performance: {
    clusterMode: process.env.CLUSTER_MODE === 'true',
    clusterWorkers: parseInt(process.env.CLUSTER_WORKERS, 10) || 4,
  },
}));
