import type { Config } from './schema';
import { productionConfig } from './production';

// Staging-specific configuration
// Inherits production security with testing-friendly adjustments
export const stagingConfig: Partial<Config> = {
	server: {
		port: 3001,
		backendUrl: 'https://staging-api.sso.vietprodev.com',
		frontendUrl: 'https://staging.app.vietprodev.com,https://staging-admin.vietprodev.com',
		projectName: productionConfig.server?.projectName || 'SSO VietProDev',
		projectVersion: productionConfig.server?.projectVersion || '1.0.0',
	},
	database: {
		host: 'staging-pgbouncer.vietprodev.com',
		port: productionConfig.database?.port || 5432,
		user: productionConfig.database?.user || 'postgres',
		password: productionConfig.database?.password || '',
		name: 'sso_staging',
		connectionString: '',
		readHost: 'staging-haproxy.vietprodev.com',
		readPort: 5433,
		slowQueryThresholdMs: productionConfig.database?.slowQueryThresholdMs || 500,
		pool: {
			max: productionConfig.database?.pool?.max || 50,
			min: productionConfig.database?.pool?.min || 5,
			acquire: productionConfig.database?.pool?.acquire || 30000,
			idle: productionConfig.database?.pool?.idle || 10000,
			evict: productionConfig.database?.pool?.evict || 60000,
		},
	},
	redis: {
		enabled: true,
		host: 'staging-redis.bekind.com',
		port: productionConfig.redis?.port || 6379,
		password: productionConfig.redis?.password || '',
		db: productionConfig.redis?.db || 0,
		connectTimeout: productionConfig.redis?.connectTimeout || 10000,
		commandTimeout: productionConfig.redis?.commandTimeout || 5000,
		maxRetries: productionConfig.redis?.maxRetries || 3,
		ipFamily: productionConfig.redis?.ipFamily || 4,
	},
	redisA: {
		enabled: true,
		host: 'staging-redis-a.bekind.com',
		port: productionConfig.redisA?.port || 6379,
		password: productionConfig.redisA?.password || '',
		cluster: productionConfig.redisA?.cluster || false,
		clusterNodes: productionConfig.redisA?.clusterNodes || '',
	},
	redisB: {
		host: 'staging-redis-b.bekind.com',
		port: productionConfig.redisB?.port || 6380,
		password: productionConfig.redisB?.password || '',
		db: productionConfig.redisB?.db || 0,
	},
	auth: {
		jwtSecret: productionConfig.auth?.jwtSecret || '',
		jwtSecretPrevious: productionConfig.auth?.jwtSecretPrevious || [],
		jwtRefreshSecret: productionConfig.auth?.jwtRefreshSecret || '',
		jwtRefreshSecretPrevious: productionConfig.auth?.jwtRefreshSecretPrevious || [],
		jwtExpiresIn: productionConfig.auth?.jwtExpiresIn || '1h',
		jwtRefreshExpiresIn: productionConfig.auth?.jwtRefreshExpiresIn || '7d',
		tokenEncryptionKey: productionConfig.auth?.tokenEncryptionKey || '',
		tokenEncryptionKeyPrevious: productionConfig.auth?.tokenEncryptionKeyPrevious || [],
		bcryptRounds: 10,
		defaultPassword: productionConfig.auth?.defaultPassword || 'Vietpro@123',
		passwordMinLength: productionConfig.auth?.passwordMinLength || 8,
		passwordMaxLength: productionConfig.auth?.passwordMaxLength || 128,
		includeTokensInResponse: true,
		skipSensitiveConfirm: false,
		enableRegister: productionConfig.auth?.enableRegister ?? true,
		passwordHistoryLimit: productionConfig.auth?.passwordHistoryLimit || 5,
	},
	cookie: {
		domain: '.vietprodev.com',
		crossSite: productionConfig.cookie?.crossSite ?? false,
		forceSecure: productionConfig.cookie?.forceSecure ?? true,
		devSameSiteLax: productionConfig.cookie?.devSameSiteLax ?? false,
		basePath: '',
	},
	storage: {
		provider: 'minio',
		publicBaseUrl: 'https://staging-cdn.bekind.com',
		localStorageRoot: productionConfig.storage?.localStorageRoot || './storage/uploads',
		minio: {
			endpoint: 'staging-minio.bekind.com',
			port: productionConfig.storage?.minio?.port || 9000,
			useSSL: productionConfig.storage?.minio?.useSSL ?? true,
			accessKey: productionConfig.storage?.minio?.accessKey || '',
			secretKey: productionConfig.storage?.minio?.secretKey || '',
			bucket: 'bekind-staging',
			region: productionConfig.storage?.minio?.region || 'us-east-1',
			s3Bucket: productionConfig.storage?.minio?.s3Bucket || '',
		},
		quotaPerUser: productionConfig.storage?.quotaPerUser || 0,
	},
	notifications: {
		rateLimiting: {
			maxLoginAttempts: 20, // Higher for testing
			lockoutDurationMinutes: 5, // Shorter lockout
			rateLimitWindowMs: productionConfig.notifications?.rateLimiting?.rateLimitWindowMs || 60000,
			rateLimitOnesignalMax: 200, // More permissive
			rateLimitZaloMax: 100,
			rateLimitEmailMax: 500,
		},
		maxRetries: productionConfig.notifications?.maxRetries || 3,
	},
	logging: {
		level: 'info', // More verbose than production
		mode: 'both', // Log to both file and console
	},
	featureFlags: {
		enableRedis: true,
		enableVirusScan: true,
		enableCSRF: true,
		enableRateLimit: false, // Disable rate limiting in staging
		showStackInErrors: true, // Show stack traces for debugging
		fileListUrlConcurrency: productionConfig.featureFlags?.fileListUrlConcurrency || 5,
		maxSimpleFallbackSize: productionConfig.featureFlags?.maxSimpleFallbackSize || 5000,
		maxFallbackMapSize: productionConfig.featureFlags?.maxFallbackMapSize || 10000,
	},
};
