import { normaliseBaseUrl } from './httpPaths';

const requestedMode = (import.meta.env.VITE_APP_MODE || 'STANDALONE').toUpperCase();
const moduleMode = requestedMode === 'SHIRE_MODULE';
const configuredRoute = import.meta.env.VITE_SCANFORGE_ROUTE || '/agents/scanforge';

export const appConfig = {
  mode: moduleMode ? 'SHIRE_MODULE' : 'STANDALONE',
  shireApiBaseUrl: normaliseBaseUrl(import.meta.env.VITE_SHIRE_API_BASE_URL || ''),
  shireModuleContextUrl: import.meta.env.VITE_SHIRE_MODULE_CONTEXT_URL || '/api/v1/modules/scanforge/context',
  scanBridgeBaseUrl: normaliseBaseUrl(import.meta.env.VITE_SCAN_BRIDGE_BASE_URL || ''),
  scanforgeRoute: `/${configuredRoute.replace(/^\/+|\/+$/g, '')}`,
  embedded: moduleMode,
  standaloneAuthEnabled: String(import.meta.env.VITE_STANDALONE_AUTH || 'true').toLowerCase() === 'true',
  shireSessionEnabled: moduleMode,
  statusPollMs: Math.max(2000, Number(import.meta.env.VITE_SCANFORGE_STATUS_POLL_MS || 5000))
};

export const isShireModule = () => appConfig.mode === 'SHIRE_MODULE';
export const isStandalone = () => appConfig.mode === 'STANDALONE';

export const scanPath = (sub = '') => {
  const base = appConfig.mode === 'SHIRE_MODULE' ? appConfig.scanforgeRoute : '';
  return sub === '' ? (base || '/') : `${base}/${String(sub).replace(/^\/+/, '')}`;
};

export const SHIREVAULT_BACKUP_PATH = '/SHiREVault/Backup/OSBackups';
