import "dotenv/config";

export const env = {
  PORT: Number(process.env.PORT || 3000),
  TRUST_PROXY: process.env.TRUST_PROXY || "false",
  JSON_BODY_LIMIT: process.env.JSON_BODY_LIMIT || "1mb",
  LOG_FORMAT: process.env.LOG_FORMAT || "dev",
  LOG_LEVEL: process.env.LOG_LEVEL || "info",
  ADMIN_TOKEN: process.env.ADMIN_TOKEN || "admin-dev-token",
  ADMIN_AUTH_ALLOW_LEGACY_TOKEN: process.env.ADMIN_AUTH_ALLOW_LEGACY_TOKEN || "true",
  ADMIN_DEV_LOGIN_ENABLED: process.env.ADMIN_DEV_LOGIN_ENABLED || "true",
  ADMIN_SESSION_SECRET: process.env.ADMIN_SESSION_SECRET || process.env.ADMIN_TOKEN || "admin-dev-token",
  ADMIN_SESSION_TTL_SECONDS: Number(process.env.ADMIN_SESSION_TTL_SECONDS || 28800),
  MERCHANT_TOKEN: process.env.MERCHANT_TOKEN || "merchant-dev-token",
  MERCHANT_PORTAL_PASSWORD: process.env.MERCHANT_PORTAL_PASSWORD || "merchant",
  MERCHANT_AUTH_ALLOW_LEGACY_TOKEN: process.env.MERCHANT_AUTH_ALLOW_LEGACY_TOKEN || "true",
  MERCHANT_DEV_LOGIN_ENABLED: process.env.MERCHANT_DEV_LOGIN_ENABLED || "true",
  MERCHANT_SESSION_SECRET: process.env.MERCHANT_SESSION_SECRET || process.env.MERCHANT_TOKEN || "merchant-dev-token",
  MERCHANT_SESSION_TTL_SECONDS: Number(process.env.MERCHANT_SESSION_TTL_SECONDS || 28800),
  DEVICE_TOKEN: process.env.DEVICE_TOKEN || "device-dev-token",
  DEVICE_AUTH_ALLOW_LEGACY_TOKEN: process.env.DEVICE_AUTH_ALLOW_LEGACY_TOKEN || "true",
  TRACE_HEADER: process.env.TRACE_HEADER || "x-request-id",
  IDEMPOTENCY_TTL_MS: Number(process.env.IDEMPOTENCY_TTL_MS || 300000),
  INTEGRATION_WEBHOOK_SECRET: process.env.INTEGRATION_WEBHOOK_SECRET || "dev-callback-secret",
  MQTT_PUBLISH_MODE: process.env.MQTT_PUBLISH_MODE || "simulator",
  MQTT_BROKER_URL: process.env.MQTT_BROKER_URL || "",
  MQTT_USERNAME: process.env.MQTT_USERNAME || "",
  MQTT_PASSWORD: process.env.MQTT_PASSWORD || "",
  MQTT_CLIENT_ID: process.env.MQTT_CLIENT_ID || "qris-platform-backend",
  MQTT_CONNECT_TIMEOUT_MS: Number(process.env.MQTT_CONNECT_TIMEOUT_MS || 5000),
  MQTT_SUBSCRIBE_ENABLED: process.env.MQTT_SUBSCRIBE_ENABLED || "false",
  MQTT_SUBSCRIBE_TOPICS: process.env.MQTT_SUBSCRIBE_TOPICS || "devices/+/uplink/#,soundbox/+/up,soundbox/+/down/heartbeat",
  MQTT_PUBLISH_FORCE_FAIL_ALL: process.env.MQTT_PUBLISH_FORCE_FAIL_ALL || "false",
  MQTT_PUBLISH_FORCE_FAIL_DEVICE_IDS: process.env.MQTT_PUBLISH_FORCE_FAIL_DEVICE_IDS || "",
  MQTT_PUBLISH_DEFAULT_RETRY_INTERVAL_MS: Number(
    process.env.MQTT_PUBLISH_DEFAULT_RETRY_INTERVAL_MS || 15000
  ),
  QF100_MQTT_BROKER_HOST: process.env.QF100_MQTT_BROKER_HOST || "",
  QF100_MQTT_BROKER_PORT: Number(process.env.QF100_MQTT_BROKER_PORT || 0),
  QF100_MQTT_USERNAME: process.env.QF100_MQTT_USERNAME || "",
  QF100_MQTT_PASSWORD: process.env.QF100_MQTT_PASSWORD || "",
  QF100_MQTT_KEEP_ALIVE_SECONDS: Number(process.env.QF100_MQTT_KEEP_ALIVE_SECONDS || 60),
  QF100_APP_CONFIG_VERSION: Number(process.env.QF100_APP_CONFIG_VERSION || 0),
  QF100_DYNAMIC_QR_URL: process.env.QF100_DYNAMIC_QR_URL || "",
  QF100_DYNAMIC_QR_GAP_SECONDS: Number(process.env.QF100_DYNAMIC_QR_GAP_SECONDS || 0),
  DYNAMIC_QR_EXPIRY_SCHEDULER_ENABLED: process.env.DYNAMIC_QR_EXPIRY_SCHEDULER_ENABLED || "true",
  DYNAMIC_QR_EXPIRY_SWEEP_INTERVAL_MS: Number(process.env.DYNAMIC_QR_EXPIRY_SWEEP_INTERVAL_MS || 60000),
  DYNAMIC_QR_EXPIRY_SWEEP_LIMIT: Number(process.env.DYNAMIC_QR_EXPIRY_SWEEP_LIMIT || 100),
  EXPORT_WORKER_ENABLED: process.env.EXPORT_WORKER_ENABLED || "true",
  EXPORT_WORKER_INTERVAL_MS: Number(process.env.EXPORT_WORKER_INTERVAL_MS || 2000),
  EXPORT_WORKER_BATCH_SIZE: Number(process.env.EXPORT_WORKER_BATCH_SIZE || 2),
  EXPORT_JOB_STALE_RUNNING_MS: Number(process.env.EXPORT_JOB_STALE_RUNNING_MS || 900000),
  EXPORT_SETTLEMENT_ADJUSTMENT_MAX_ROWS: Number(process.env.EXPORT_SETTLEMENT_ADJUSTMENT_MAX_ROWS || 5000),
  EXPORT_STORAGE_DIR: process.env.EXPORT_STORAGE_DIR || "./storage/exports",
  EXPORT_RETENTION_DAYS: Number(process.env.EXPORT_RETENTION_DAYS || 7),
  RATE_LIMIT_ENABLED: process.env.RATE_LIMIT_ENABLED || "true",
  RATE_LIMIT_LOGIN_WINDOW_MS: Number(process.env.RATE_LIMIT_LOGIN_WINDOW_MS || 60000),
  RATE_LIMIT_LOGIN_MAX: Number(process.env.RATE_LIMIT_LOGIN_MAX || 20),
  RATE_LIMIT_DEVICE_WINDOW_MS: Number(process.env.RATE_LIMIT_DEVICE_WINDOW_MS || 60000),
  RATE_LIMIT_DEVICE_MAX: Number(process.env.RATE_LIMIT_DEVICE_MAX || 600),
  RATE_LIMIT_ADMIN_WRITE_WINDOW_MS: Number(process.env.RATE_LIMIT_ADMIN_WRITE_WINDOW_MS || 60000),
  RATE_LIMIT_ADMIN_WRITE_MAX: Number(process.env.RATE_LIMIT_ADMIN_WRITE_MAX || 300),
  FINANCE_PLATFORM_FEE_BPS: Number(process.env.FINANCE_PLATFORM_FEE_BPS || 70),
  SETTLEMENT_ADJUSTMENT_REQUIRE_APPROVAL: process.env.SETTLEMENT_ADJUSTMENT_REQUIRE_APPROVAL || "false",
  DATABASE_URL: process.env.DATABASE_URL || "",
  PGHOST: process.env.PGHOST || "127.0.0.1",
  PGPORT: Number(process.env.PGPORT || 5432),
  PGUSER: process.env.PGUSER || "postgres",
  PGPASSWORD: process.env.PGPASSWORD || "postgres",
  PGDATABASE: process.env.PGDATABASE || "qris_soundbox_platform"
};
