import dotenv from 'dotenv';

dotenv.config();

const parseNumber = (value, defaultValue) => {
  const parsed = Number(value);
  return Number.isFinite(parsed) ? parsed : defaultValue;
};

const parseList = (value) =>
  (value || '')
    .split(',')
    .map((item) => item.trim())
    .filter(Boolean);

export const config = {
  env: process.env.NODE_ENV || 'development',
  port: parseNumber(process.env.PORT, 3000),
  kafka: {
    brokers: parseList(process.env.KAFKA_BROKERS),
    topic: process.env.KAFKA_TOPIC || process.env.KAFKA_TOPICS || 'blwlog4Nodejs-rcu-action-topic',
    groupId: process.env.KAFKA_GROUP_ID || 'bls-rcu-action-group',
    clientId: process.env.KAFKA_CLIENT_ID || 'bls-rcu-action-client',
    consumerInstances: parseNumber(process.env.KAFKA_CONSUMER_INSTANCES, 1),
    maxInFlight: parseNumber(process.env.KAFKA_MAX_IN_FLIGHT, 500),
    fetchMaxBytes: parseNumber(process.env.KAFKA_FETCH_MAX_BYTES, 10 * 1024 * 1024),
    fetchMinBytes: parseNumber(process.env.KAFKA_FETCH_MIN_BYTES, 1),
    fetchMaxWaitMs: parseNumber(process.env.KAFKA_FETCH_MAX_WAIT_MS, 100),
    autoCommitIntervalMs: parseNumber(process.env.KAFKA_AUTO_COMMIT_INTERVAL_MS, 5000),
    logMessages: process.env.KAFKA_LOG_MESSAGES === 'true',
    sasl: process.env.KAFKA_SASL_USERNAME && process.env.KAFKA_SASL_PASSWORD ? {
      mechanism: process.env.KAFKA_SASL_MECHANISM || 'plain',
      username: process.env.KAFKA_SASL_USERNAME,
      password: process.env.KAFKA_SASL_PASSWORD
    } : undefined
  },
  db: {
    host: process.env.DB_HOST || process.env.POSTGRES_HOST || 'localhost',
    port: parseNumber(process.env.DB_PORT || process.env.POSTGRES_PORT, 5432),
    user: process.env.DB_USER || process.env.POSTGRES_USER || 'postgres',
    password: process.env.DB_PASSWORD || process.env.POSTGRES_PASSWORD || '',
    database: process.env.DB_DATABASE || process.env.POSTGRES_DATABASE || 'bls_rcu_action',
    max: parseNumber(process.env.DB_MAX_CONNECTIONS || process.env.POSTGRES_MAX_CONNECTIONS, 10),
    ssl: process.env.DB_SSL === 'true' ? { rejectUnauthorized: false } : undefined,
    schema: process.env.DB_SCHEMA || 'rcu_action',
    table: process.env.DB_TABLE || 'rcu_action_events'
  },
  redis: {
    host: process.env.REDIS_HOST || 'localhost',
    port: parseNumber(process.env.REDIS_PORT, 6379),
    password: process.env.REDIS_PASSWORD || undefined,
    db: parseNumber(process.env.REDIS_DB, 0),
    projectName: process.env.REDIS_PROJECT_NAME || 'bls-rcu-action',
    apiBaseUrl: process.env.REDIS_API_BASE_URL || `http://localhost:${parseNumber(process.env.PORT, 3000)}`
  },
  roomStatusDb: {
    host: process.env.ROOM_STATUS_DB_HOST || process.env.DB_HOST || process.env.POSTGRES_HOST || 'localhost',
    port: parseNumber(process.env.ROOM_STATUS_DB_PORT || process.env.DB_PORT || process.env.POSTGRES_PORT, 5432),
    user: process.env.ROOM_STATUS_DB_USER || process.env.DB_USER || process.env.POSTGRES_USER || 'postgres',
    password: process.env.ROOM_STATUS_DB_PASSWORD || process.env.DB_PASSWORD || process.env.POSTGRES_PASSWORD || '',
    database: process.env.ROOM_STATUS_DB_DATABASE || process.env.DB_DATABASE || process.env.POSTGRES_DATABASE || 'bls_rcu_action',
    max: parseNumber(process.env.ROOM_STATUS_DB_MAX_CONNECTIONS, 5),
    ssl: process.env.ROOM_STATUS_DB_SSL === 'true' ? { rejectUnauthorized: false } : undefined,
    schema: process.env.ROOM_STATUS_DB_SCHEMA || 'room_status',
    table: process.env.ROOM_STATUS_DB_TABLE || 'room_status_moment'
  },
  enableLoopNameAutoGeneration: process.env.ENABLE_LOOP_NAME_AUTO_GENERATION === 'true'
};
