const path = require("path");

const root = path.resolve(__dirname, "..");

function bool(value, fallback = false) {
  if (value === undefined || value === null || value === "") return fallback;
  return ["1", "true", "yes", "on"].includes(String(value).toLowerCase());
}

function fromRoot(value, fallback) {
  const selected = value || fallback;
  return path.isAbsolute(selected) ? selected : path.join(root, selected);
}

module.exports = {
  root,
  env: process.env.NODE_ENV || "development",
  port: Number(process.env.PORT || 3000),
  baseUrl: process.env.APP_BASE_URL || "http://localhost:3000",
  primaryDomain: process.env.PRIMARY_DOMAIN || "debbiewindlerseamstress.com",
  secondaryDomain: process.env.SECONDARY_DOMAIN || "debbiewindler.com",
  sessionSecret: process.env.SESSION_SECRET || "development-only-change-me",
  databasePath: fromRoot(process.env.DATABASE_PATH, "./storage/site.sqlite"),
  uploadDir: fromRoot(process.env.UPLOAD_DIR, "./public/uploads"),
  backupDir: fromRoot(process.env.BACKUP_DIR, "./storage/backups"),
  smtp: {
    host: process.env.SMTP_HOST || "",
    port: Number(process.env.SMTP_PORT || 587),
    secure: bool(process.env.SMTP_SECURE, false),
    user: process.env.SMTP_USER || "",
    pass: process.env.SMTP_PASS || "",
    from: process.env.SMTP_FROM || "Debbie Windler Seamstress <website@debbiewindlerseamstress.com>",
    ownerEmail: process.env.OWNER_EMAIL || ""
  },
  captcha: {
    enabled: bool(process.env.CAPTCHA_ENABLED, false),
    siteKey: process.env.CAPTCHA_SITE_KEY || "",
    secretKey: process.env.CAPTCHA_SECRET_KEY || ""
  },
  production: process.env.NODE_ENV === "production"
};
