import dotenv from 'dotenv';
import { path } from './utils/pathUtils.js';

dotenv.config({ quiet: true });

const rootDir = process.cwd();
const defaultClientOrigins = [
  'http://localhost:3000',
  'http://127.0.0.1:3000',
  'http://localhost:5173',
  'http://127.0.0.1:5173'
];
const configuredClientOrigins = (process.env.CLIENT_ORIGINS || process.env.CLIENT_ORIGIN || '')
  .split(',')
  .map((origin) => origin.trim())
  .filter(Boolean);

export const config = {
  nodeEnv: process.env.NODE_ENV || 'development',
  isProduction: process.env.NODE_ENV === 'production',
  port: Number(process.env.PORT || 5174),
  serverFqdn: process.env.SERVER_FQDN || 'http://localhost:5174',
  clientOrigin: process.env.CLIENT_ORIGIN || 'http://localhost:5173',
  clientOrigins: [...new Set([...configuredClientOrigins, ...defaultClientOrigins])],
  databasePath: path.resolve(rootDir, process.env.DATABASE_PATH || './data/poshmanager.sqlite'),
  logDir: path.resolve(rootDir, process.env.LOG_DIR || './data/logs'),
  fileLockerDir: path.resolve(rootDir, process.env.FILE_LOCKER_DIR || process.env.ASSET_DIR || './FileLocker'),
  // Location for bundled tools such as the Intune Win32 Content Prep Tool.
  // Drop IntuneWinAppUtil.exe in ./tools (or point INTUNEWIN_UTIL_PATH elsewhere).
  toolsDir: path.resolve(rootDir, process.env.TOOLS_DIR || './tools'),
  intunewinUtilPath: process.env.INTUNEWIN_UTIL_PATH || '',
  // Optional cross-platform packager hook. Template with {source} {setup} {output}.
  intunewinBuildCommand: process.env.INTUNEWIN_BUILD_COMMAND || '',
  maxAssetBytes: Number(process.env.MAX_ASSET_BYTES || 52428800),
  jsonLimit: process.env.JSON_LIMIT || '60mb',
  jwtSecret: process.env.JWT_SECRET || 'dev-only-change-me',
  credentialStoreKey: process.env.CREDENTIAL_STORE_KEY || process.env.JWT_SECRET || 'dev-only-credential-key',
  defaultAdminEmail: process.env.DEFAULT_ADMIN_EMAIL || 'admin@posh.local',
  defaultAdminPassword: process.env.DEFAULT_ADMIN_PASSWORD || 'change-me-now',
  defaultAdminName: process.env.DEFAULT_ADMIN_NAME || 'POSH Admin',
  powershellBin: process.env.POWERSHELL_BIN || 'pwsh',
  allowScriptExecution: (process.env.ALLOW_SCRIPT_EXECUTION || 'true').toLowerCase() === 'true',
  vcenter: {
    enabled: (process.env.VCENTER_ENABLED || 'false').toLowerCase() === 'true',
    baseUrl: process.env.VCENTER_BASE_URL || '',
    username: process.env.VCENTER_USERNAME || '',
    password: process.env.VCENTER_PASSWORD || '',
    allowUntrustedTls: (process.env.VCENTER_ALLOW_UNTRUSTED_TLS || 'false').toLowerCase() === 'true'
  },
  entra: {
    enabled: (process.env.ENTRA_ENABLED || 'false').toLowerCase() === 'true',
    tenantId: process.env.ENTRA_TENANT_ID || '',
    clientId: process.env.ENTRA_CLIENT_ID || '',
    clientSecret: process.env.ENTRA_CLIENT_SECRET || '',
    callbackUrl: process.env.ENTRA_CALLBACK_URL || '',
    passwordChangeUrl: process.env.ENTRA_PASSWORD_CHANGE_URL || 'https://mysignins.microsoft.com/security-info/password/change'
  }
};
