// Environment configuration with validation
export const env = {
  // Database
  DATABASE_PATH: process.env.DATABASE_PATH || './data/gallus_cms.db',

  // Gitea OAuth
  GITEA_URL: process.env.GITEA_URL || 'https://git.bookageek.ch',
  GITEA_CLIENT_ID: process.env.GITEA_CLIENT_ID || '',
  GITEA_CLIENT_SECRET: process.env.GITEA_CLIENT_SECRET || '',
  GITEA_REDIRECT_URI: process.env.GITEA_REDIRECT_URI || 'http://localhost:3000/api/auth/callback',
  GITEA_ALLOWED_USERS: process.env.GITEA_ALLOWED_USERS || '',

  // Git Configuration
  GIT_REPO_URL: process.env.GIT_REPO_URL || '',
  GIT_TOKEN: process.env.GIT_TOKEN || '',
  GIT_USER_NAME: process.env.GIT_USER_NAME || 'Gallus CMS',
  GIT_USER_EMAIL: process.env.GIT_USER_EMAIL || 'cms@galluspub.ch',
  GIT_WORKSPACE_DIR: process.env.GIT_WORKSPACE_DIR || '/tmp/gallus-repo',

  // JWT & Session
  JWT_SECRET: process.env.JWT_SECRET || '',
  SESSION_SECRET: process.env.SESSION_SECRET || '',

  // Server
  PORT: parseInt(process.env.PORT || '3000', 10),
  NODE_ENV: process.env.NODE_ENV || 'development',
  CORS_ORIGIN: process.env.CORS_ORIGIN || 'http://localhost:5173',
  FRONTEND_URL: process.env.FRONTEND_URL || 'http://localhost:5173',

  // Upload
  MAX_FILE_SIZE: parseInt(process.env.MAX_FILE_SIZE || '5242880', 10),
};

// Validate required environment variables
export function validateEnv() {
  const required = [
    'DATABASE_PATH',
    'GITEA_CLIENT_ID',
    'GITEA_CLIENT_SECRET',
    'GIT_REPO_URL',
    'GIT_TOKEN',
    'JWT_SECRET',
    'SESSION_SECRET',
  ];

  const missing = required.filter(key => !env[key as keyof typeof env]);

  if (missing.length > 0) {
    throw new Error(`Missing required environment variables: ${missing.join(', ')}`);
  }
}
