import { config } from './config.js';

// Single source of truth for which app settings are surfaced in the Config
// screen, what their boot-time value is, and whether they are pinned by an
// environment variable.
//
// Pinned settings are authoritative from the deployment environment (Docker /
// reverse proxy) and are locked in the UI; `seed()` refreshes them on every
// boot. Unpinned settings are seeded once as editable defaults and then owned
// by the admin via the Config screen, so their edits persist across restarts.

function envProvided(...names) {
  return names.some((name) => process.env[name] !== undefined && process.env[name] !== '');
}

export function settingDefinitions() {
  return [
    { key: 'server_fqdn', value: config.serverFqdn, pinned: envProvided('SERVER_FQDN') },
    { key: 'trusted_origins', value: config.clientOrigins.join(','), pinned: envProvided('CLIENT_ORIGINS', 'CLIENT_ORIGIN') },
    { key: 'client_origin', value: config.clientOrigin, pinned: envProvided('CLIENT_ORIGIN') },
    { key: 'api_port', value: String(config.port), pinned: envProvided('PORT') },
    // Filesystem and process-structural settings are always env/locked; changing
    // them at runtime would point the live process at the wrong storage.
    { key: 'database_path', value: config.databasePath, pinned: true },
    { key: 'log_dir', value: config.logDir, pinned: true },
    { key: 'file_locker_dir', value: config.fileLockerDir, pinned: true },
    { key: 'asset_dir', value: config.fileLockerDir, pinned: true },
    { key: 'node_env', value: config.nodeEnv, pinned: true },
    { key: 'max_asset_bytes', value: String(config.maxAssetBytes), pinned: envProvided('MAX_ASSET_BYTES') },
    { key: 'json_limit', value: config.jsonLimit, pinned: envProvided('JSON_LIMIT') },
    { key: 'entra_enabled', value: String(config.entra.enabled), pinned: envProvided('ENTRA_ENABLED') },
    { key: 'entra_tenant_id', value: config.entra.tenantId, pinned: envProvided('ENTRA_TENANT_ID') },
    { key: 'entra_client_id', value: config.entra.clientId, pinned: envProvided('ENTRA_CLIENT_ID') },
    { key: 'entra_callback_url', value: config.entra.callbackUrl, pinned: envProvided('ENTRA_CALLBACK_URL') },
    { key: 'entra_password_change_url', value: config.entra.passwordChangeUrl, pinned: envProvided('ENTRA_PASSWORD_CHANGE_URL') },
    { key: 'powershell_bin', value: config.powershellBin, pinned: envProvided('POWERSHELL_BIN') },
    { key: 'allow_script_execution', value: String(config.allowScriptExecution), pinned: envProvided('ALLOW_SCRIPT_EXECUTION') },
    { key: 'vcenter_enabled', value: String(config.vcenter.enabled), pinned: envProvided('VCENTER_ENABLED') },
    { key: 'vcenter_base_url', value: config.vcenter.baseUrl, pinned: envProvided('VCENTER_BASE_URL') },
    { key: 'vcenter_username', value: config.vcenter.username, pinned: envProvided('VCENTER_USERNAME') },
    { key: 'vcenter_password', value: config.vcenter.password, pinned: envProvided('VCENTER_PASSWORD') },
    { key: 'vcenter_allow_untrusted_tls', value: String(config.vcenter.allowUntrustedTls), pinned: envProvided('VCENTER_ALLOW_UNTRUSTED_TLS') }
  ];
}
