// FILE: backend/src/config.js
// Centralized configuration (ESM)

const toInt = (v, d) => {
  const n = Number(v);
  return Number.isFinite(n) ? n : d;
};
const parseAllowlist = (s) =>
  String(s || "")
    .split(",")
    .map((x) => x.trim())
    .filter(Boolean);

export const config = {
  PORT: toInt(process.env.PORT ?? process.env.BACKEND_PORT, 3001),

  DB: {
    HOST: process.env.DB_HOST || "mariadb",
    PORT: toInt(process.env.DB_PORT, 3306),
    USER: process.env.DB_USER || "center",
    PASS: process.env.DB_PASSWORD || "Center#2025",
    NAME: process.env.DB_NAME || "dms",
  },

  JWT: {
    SECRET: process.env.JWT_SECRET || "dev-secret",
    EXPIRES_IN: process.env.JWT_EXPIRES_IN || "8h",
    REFRESH_SECRET: process.env.JWT_REFRESH_SECRET || "dev-refresh",
    REFRESH_EXPIRES_IN: process.env.JWT_REFRESH_EXPIRES_IN || "7d",
  },

  SECURITY: {
    RATE_LIMIT_WINDOW_MS: toInt(process.env.RATE_LIMIT_WINDOW_MS, 60_000),
    RATE_LIMIT_MAX: toInt(process.env.RATE_LIMIT_MAX, 100),
  },

  CORS_ORIGINS: parseAllowlist(
    process.env.CORS_ALLOWLIST ||
      // เผื่อ dev ทั่วไป
      "http://localhost:3000,http://127.0.0.1:3000"
  ),
};

// เผื่อไฟล์ไหน import แบบ default
export default config;
