import dotenv from 'dotenv';
import chalk from 'chalk';

const envPath = process.env.MEET_CONFIG_DIR
	? process.env.MEET_CONFIG_DIR
	: process.env.NODE_ENV === 'development'
		? '.env.development'
		: undefined;

dotenv.config(envPath ? { path: envPath } : {});

export const {
	SERVER_PORT = 6080,
	SERVER_CORS_ORIGIN = '*',
	MEET_NAME_ID = 'openviduMeet',
	MEET_API_KEY = 'meet-api-key',
	MEET_USER = 'user',
	MEET_SECRET = 'user',
	MEET_ADMIN_USER = 'admin',
	MEET_ADMIN_SECRET = 'admin',
	MEET_PARTICIPANT_TOKEN_EXPIRATION = '6h',
	MEET_ACCESS_TOKEN_EXPIRATION = '2h',
	MEET_REFRESH_TOKEN_EXPIRATION = '1d',
	MEET_PREFERENCES_STORAGE_MODE = 's3',
	MEET_WEBHOOK_ENABLED = 'false',
	MEET_WEBHOOK_URL = 'http://localhost:5080/webhook',
	MEET_LOG_LEVEL = 'info',

	// LiveKit configuration
	LIVEKIT_URL = 'ws://localhost:7880',
	LIVEKIT_URL_PRIVATE = LIVEKIT_URL, // Uses LIVEKIT_URL if not explicitly set
	LIVEKIT_API_KEY = 'devkey',
	LIVEKIT_API_SECRET = 'secret',

	// S3 configuration
	MEET_S3_BUCKET = 'openvidu',
	MEET_S3_SUBBUCKET = 'openvidu-meet',
	MEET_S3_SERVICE_ENDPOINT = 'http://localhost:9000',
	MEET_S3_ACCESS_KEY = 'minioadmin',
	MEET_S3_SECRET_KEY = 'minioadmin',
	MEET_AWS_REGION = 'us-east-1',
	MEET_S3_WITH_PATH_STYLE_ACCESS = 'true',
	MEET_S3_MAX_RETRIES_ATTEMPTS_ON_SAVE_ERROR = '5',
	MEET_S3_INITIAL_RETRY_DELAY_MS = '100',

	// Redis configuration
	MEET_REDIS_HOST: REDIS_HOST = 'localhost',
	MEET_REDIS_PORT: REDIS_PORT = 6379,
	MEET_REDIS_USERNAME: REDIS_USERNAME = '',
	MEET_REDIS_PASSWORD: REDIS_PASSWORD = 'redispassword',
	MEET_REDIS_DB: REDIS_DB = '0',

	// Redis Sentinel configuration
	MEET_REDIS_SENTINEL_HOST_LIST: REDIS_SENTINEL_HOST_LIST = '',
	MEET_REDIS_SENTINEL_PASSWORD: REDIS_SENTINEL_PASSWORD = '',
	MEET_REDIS_SENTINEL_MASTER_NAME: REDIS_SENTINEL_MASTER_NAME = 'openvidu',

	// Deployment configuration
	MODULES_FILE = undefined,
	MODULE_NAME = 'openviduMeet',
	ENABLED_MODULES = ''
} = process.env;

// Base paths for the API
export const MEET_API_BASE_PATH = '/meet/api';
export const MEET_INTERNAL_API_BASE_PATH_V1 = '/meet/internal-api/v1';
export const MEET_API_BASE_PATH_V1 = MEET_API_BASE_PATH + '/v1';

// Cookie names
export const PARTICIPANT_TOKEN_COOKIE_NAME = 'OvMeetParticipantToken';
export const ACCESS_TOKEN_COOKIE_NAME = 'OvMeetAccessToken';
export const REFRESH_TOKEN_COOKIE_NAME = 'OvMeetRefreshToken';

// Fixed usernames
export const MEET_ANONYMOUS_USER = 'anonymous';
export const MEET_API_USER = 'api-user';

// S3 prefixes
export const MEET_S3_ROOMS_PREFIX = 'rooms';
export const MEET_S3_RECORDINGS_PREFIX = 'recordings';

// Time to live for the active recording lock in Redis
export const MEET_RECORDING_LOCK_TTL = '6h';

export function checkModuleEnabled() {
	if (MODULES_FILE) {
		const moduleName = MODULE_NAME;
		const enabledModules = ENABLED_MODULES.split(',').map((module) => module.trim());

		if (!enabledModules.includes(moduleName)) {
			console.error(`Module ${moduleName} is not enabled`);
			process.exit(0);
		}
	}
}

export const logEnvVars = () => {
	const credential = chalk.yellow;
	const text = chalk.cyanBright;

	console.log(' ');
	console.log('---------------------------------------------------------');
	console.log('OpenVidu Meet Server Configuration');
	console.log('---------------------------------------------------------');
	console.log('SERVICE NAME ID: ', text(MEET_NAME_ID));
	console.log('CORS ORIGIN:', text(SERVER_CORS_ORIGIN));
	console.log('MEET LOG LEVEL: ', text(MEET_LOG_LEVEL));
	console.log('MEET API KEY: ', credential('****' + MEET_API_KEY.slice(-3)));
	console.log('MEET ADMIN USER: ', credential('****' + MEET_ADMIN_USER.slice(-3)));
	console.log('MEET ADMIN PASSWORD: ', credential('****' + MEET_ADMIN_SECRET.slice(-3)));
	console.log('MEET ACCESS TOKEN EXPIRATION: ', text(MEET_ACCESS_TOKEN_EXPIRATION));
	console.log('MEET REFRESH TOKEN EXPIRATION: ', text(MEET_REFRESH_TOKEN_EXPIRATION));
	console.log('MEET PREFERENCES STORAGE:', text(MEET_PREFERENCES_STORAGE_MODE));

	console.log('---------------------------------------------------------');
	console.log('LIVEKIT Configuration');
	console.log('---------------------------------------------------------');
	console.log('LIVEKIT URL: ', text(LIVEKIT_URL));
	console.log('LIVEKIT URL PRIVATE: ', text(LIVEKIT_URL_PRIVATE));
	console.log('LIVEKIT API SECRET: ', credential('****' + LIVEKIT_API_SECRET.slice(-3)));
	console.log('LIVEKIT API KEY: ', credential('****' + LIVEKIT_API_KEY.slice(-3)));
	console.log('---------------------------------------------------------');
	console.log('S3 Configuration');
	console.log('---------------------------------------------------------');
	console.log('MEET S3 BUCKET:', text(MEET_S3_BUCKET));
	console.log('MEET S3 SERVICE ENDPOINT:', text(MEET_S3_SERVICE_ENDPOINT));
	console.log('MEET S3 ACCESS KEY:', credential('****' + MEET_S3_ACCESS_KEY.slice(-3)));
	console.log('MEET S3 SECRET KEY:', credential('****' + MEET_S3_SECRET_KEY.slice(-3)));
	console.log('MEET AWS REGION:', text(MEET_AWS_REGION));
	console.log('MEET S3 WITH PATH STYLE ACCESS:', text(MEET_S3_WITH_PATH_STYLE_ACCESS));
	console.log('MEET S3 MAX RETRIES ATTEMPTS ON SAVE ERROR:', text(MEET_S3_MAX_RETRIES_ATTEMPTS_ON_SAVE_ERROR));
	console.log('MEET S3 INITIAL RETRY DELAY MS:', text(MEET_S3_INITIAL_RETRY_DELAY_MS));
	console.log('---------------------------------------------------------');
	console.log('Redis Configuration');
	console.log('---------------------------------------------------------');
	console.log('REDIS HOST:', text(REDIS_HOST));
	console.log('REDIS PORT:', text(REDIS_PORT));
	console.log('REDIS USERNAME:', credential('****' + REDIS_USERNAME.slice(-3)));
	console.log('REDIS PASSWORD:', credential('****' + REDIS_PASSWORD.slice(-3)));

	if (REDIS_SENTINEL_HOST_LIST !== '') {
		console.log('REDIS SENTINEL IS ENABLED');
		console.log('REDIS SENTINEL HOST LIST:', text(REDIS_SENTINEL_HOST_LIST));
	}

	console.log('---------------------------------------------------------');
	console.log(' ');
};
