'use strict'
const env = (k, fallback = '') => process.env[k] || fallback
const int = (k, fallback) => parseInt(env(k, String(fallback)), 10)

module.exports = {
  PORT: int('PORT', 3300),
  INTERNAL_TOKEN: env('HUB_INTERNAL_TOKEN'),
  ERP_URL: env('ERP_URL', 'http://erpnext-backend:8000'),
  ERP_TOKEN: env('ERP_TOKEN'),
  ERP_SITE: env('ERP_SITE', 'erp.gigafibre.ca'),
  HUB_PUBLIC_URL: env('HUB_PUBLIC_URL', 'https://msg.gigafibre.ca'),
  GOOGLE_REVIEW_URL: env('GOOGLE_REVIEW_URL'), // page d'avis Google (clic 5★) — définir dans .env (https://search.google.com/local/writereview?placeid=…)
  TWILIO_ACCOUNT_SID: env('TWILIO_ACCOUNT_SID'),
  TWILIO_AUTH_TOKEN: env('TWILIO_AUTH_TOKEN'),
  TWILIO_FROM: env('TWILIO_FROM'),
  TWILIO_API_KEY: env('TWILIO_API_KEY'),
  TWILIO_API_SECRET: env('TWILIO_API_SECRET'),
  TWILIO_TWIML_APP_SID: env('TWILIO_TWIML_APP_SID'),
  PBX_ENABLED: env('PBX_ENABLED') === '1',
  PBX_URL: env('PBX_URL', 'https://targopbx.3cx.ca'),
  PBX_USER: env('PBX_USER'),
  PBX_PASS: env('PBX_PASS'),
  PBX_POLL_INTERVAL: int('PBX_POLL_INTERVAL', 30000),
  ROUTR_DB_URL: env('ROUTR_DB_URL'),
  FNIDENTITY_DB_URL: env('FNIDENTITY_DB_URL'),
  CACHE_DB_URL: env('CACHE_DB_URL', env('ROUTR_DB_URL')),
  SIP_WSS_URL: env('SIP_WSS_URL', 'wss://voice.gigafibre.ca:5063'),
  SIP_DOMAIN: env('SIP_DOMAIN', 'voice.gigafibre.ca'),
  SIP_EXTENSION: env('SIP_EXTENSION', '1001'),
  SIP_AUTH_ID: env('SIP_AUTH_ID', '1001'),
  SIP_AUTH_PASSWORD: env('SIP_AUTH_PASSWORD'),
  SIP_DISPLAY_NAME: env('SIP_DISPLAY_NAME', 'Targo Ops'),
  GENIEACS_NBI_URL: env('GENIEACS_NBI_URL', 'http://10.5.2.115:7557'),
  AUTHENTIK_URL: env('AUTHENTIK_URL', 'https://auth.targo.ca'),
  AUTHENTIK_TOKEN: env('AUTHENTIK_TOKEN'),
  VAPID_PUBLIC_KEY: env('VAPID_PUBLIC_KEY'),
  VAPID_PRIVATE_KEY: env('VAPID_PRIVATE_KEY'),
  CLIENT_PUBLIC_URL: env('CLIENT_PUBLIC_URL', 'https://msg.gigafibre.ca'),
  AI_API_KEY: env('AI_API_KEY'),
  AI_MODEL: env('AI_MODEL', 'gemini-2.5-flash'),
  AI_FALLBACK_MODEL: env('AI_FALLBACK_MODEL', 'gemini-2.5-flash-lite-preview'),
  AI_BASE_URL: env('AI_BASE_URL', 'https://generativelanguage.googleapis.com/v1beta/openai/'),
  // Modèle LOCAL (vLLM/Ollama → QWEN/Hermes), OpenAI-compatible. Vide = profil « débranché » (repli auto sur Gemini).
  AI_LOCAL_BASE_URL: env('AI_LOCAL_BASE_URL', ''), // ex. http://gpu-host:8000/v1/  (slash final requis)
  AI_LOCAL_MODEL: env('AI_LOCAL_MODEL', ''),       // ex. Qwen/Qwen2.5-14B-Instruct
  AI_LOCAL_API_KEY: env('AI_LOCAL_API_KEY', ''), // VIDE par défaut = profil local débranché. Définir (même factice pour vLLM sans auth) = activation explicite.
  // Routage tâche→profil (JSON). Ex. {"triage":"local","sms":"local"} pour basculer ces tâches vers le local. Vide = défauts (tout Gemini).
  AI_ROUTES: (() => { try { return JSON.parse(env('AI_ROUTES', '') || '{}') } catch (e) { return {} } })(),
  VOICE_MODEL: env('VOICE_MODEL', 'models/gemini-2.5-flash-live-preview'),
  OLT_HOST: env('OLT_HOST'),
  OLT_COMMUNITY: env('OLT_COMMUNITY', 'public'),
  OLT_NAME: env('OLT_NAME'),
  OLT_POLL_INTERVAL: int('OLT_POLL_INTERVAL', 300000),
  // OLT_LIST='[{"host":"172.17.192.6","community":"targosnmp","name":"olt3.Remi"}]'
  OLT_LIST: (() => { try { return JSON.parse(env('OLT_LIST', '[]')) } catch { return [] } })(),
  LEGACY_DB_HOST: env('LEGACY_DB_HOST', 'legacy-db'),
  LEGACY_DB_USER: env('LEGACY_DB_USER', 'facturation'),
  LEGACY_DB_PASS: env('LEGACY_DB_PASS'),
  LEGACY_DB_NAME: env('LEGACY_DB_NAME', 'gestionclient'),
  OMLX_URL: env('OMLX_URL', 'http://127.0.0.1:8000'),
  TRACCAR_URL: env('TRACCAR_URL', 'http://tracker.targointernet.com:8082'),
  ROSTER_SOLVER_URL: env('ROSTER_SOLVER_URL', 'http://roster-solver:8090'),
  OSRM_URL: env('OSRM_URL', 'http://osrm:5000'), // moteur de routes OSM (matrice temps de route réels) pour l'optimiseur VRP
  JWT_SECRET: env('JWT_SECRET'),
  FIELD_APP_URL: env('FIELD_APP_URL', 'https://msg.gigafibre.ca'),
  EXTERNAL_URL: env('EXTERNAL_URL', 'https://msg.gigafibre.ca'),
  TRACCAR_USER: env('TRACCAR_USER', 'louis@targo.ca'),
  TRACCAR_PASS: env('TRACCAR_PASS'),
  TRACCAR_TOKEN: env('TRACCAR_TOKEN'),
  DOCUSEAL_URL: env('DOCUSEAL_URL', 'https://sign.gigafibre.ca'),
  DOCUSEAL_API_KEY: env('DOCUSEAL_API_KEY'),
  DOCUSEAL_DEFAULT_TEMPLATE_ID: int('DOCUSEAL_DEFAULT_TEMPLATE_ID', 1),
  // Stripe
  STRIPE_SECRET_KEY: env('STRIPE_SECRET_KEY'),
  STRIPE_WEBHOOK_SECRET: env('STRIPE_WEBHOOK_SECRET'),
  CLIENT_PORTAL_URL: env('CLIENT_PORTAL_URL', 'https://portal.gigafibre.ca'),
  // Oktopus TR-369 controller
  OKTOPUS_URL: env('OKTOPUS_URL', 'https://oss.gigafibre.ca'),
  OKTOPUS_USER: env('OKTOPUS_USER', ''),
  OKTOPUS_PASS: env('OKTOPUS_PASS', ''),
  OKTOPUS_MONGO_URL: env('OKTOPUS_MONGO_URL', 'mongodb://oktopus-mongo-1:27017'),
  OKTOPUS_MQTT_URL: env('OKTOPUS_MQTT_URL', 'mqtt://oktopus-mqtt-1:1883'),
  // InfluxDB (Grafana metrics — interface stats, SNMP, OLT logs)
  KUMA_METRICS_URL: env('KUMA_METRICS_URL', 'https://kuma.targo.ca/metrics'),
  INFLUXDB_URL: env('INFLUXDB_URL', 'http://10.5.2.65:8086'),
  INFLUXDB_DB: env('INFLUXDB_DB', 'telegraf'),
  GRAFANA_URL: env('GRAFANA_URL', 'http://10.5.2.65:3000'),
  GRAFANA_API_KEY: env('GRAFANA_API_KEY'),
  // Email (nodemailer via Mailjet SMTP)
  SMTP_HOST: env('SMTP_HOST', 'in-v3.mailjet.com'),
  SMTP_PORT: int('SMTP_PORT', 587),
  SMTP_SECURE: env('SMTP_SECURE') === '1',
  SMTP_USER: env('SMTP_USER'),
  SMTP_PASS: env('SMTP_PASS'),
  MAIL_FROM: env('MAIL_FROM', 'Gigafibre <noreply@targo.ca>'),
  // Modem-Bridge (Playwright headless browser for ONU web GUI)
  BRIDGE_HOST: env('BRIDGE_HOST', 'modem-bridge'),
  BRIDGE_PORT: int('BRIDGE_PORT', 3301),
  BRIDGE_TOKEN: env('BRIDGE_TOKEN'),
}
