/**
 * 环境变量统一配置管理
 * 集中管理所有环境变量，避免重复声明
 */

/**
 * 环境变量配置接口
 */
export interface EnvConfig {
    // 基础配置
    nodeEnv: string;
    isDevelopment: boolean;
    isProduction: boolean;
    
    // API 基础 URL 配置
    baseUrl: string;
    javaUrl: string;
    cutUrl: string;
    cutUrlTo: string;
    
    // Google OAuth 配置
    googleClientId: string;
    googleRedirectUri: string;
    
    // Google Analytics 配置
    gaEnabled: boolean;
    gaMeasurementId: string;
    
    // 视频编辑配置
    videoEditUseMock: boolean;
    videoEditUseRemote: boolean;
    videoEditRemoteBase: string;
    
    // 其他配置
    errorConfig: number;
}

/**
 * 获取环境变量配置
 */
export const getEnvConfig = (): EnvConfig => {
    const nodeEnv = process.env.NODE_ENV || 'development';
    
    return {
        // 基础配置
        nodeEnv,
        isDevelopment: nodeEnv === 'development',
        isProduction: nodeEnv === 'production',
        
        // API 基础 URL 配置
        baseUrl: process.env.NEXT_PUBLIC_BASE_URL || 'https://77.smartvideo.py.qikongjian.com',
        javaUrl: process.env.NEXT_PUBLIC_JAVA_URL || 'https://77.app.java.auth.qikongjian.com',
        cutUrl: process.env.NEXT_PUBLIC_CUT_URL || 'https://smartcut.api.movieflow.ai',
        cutUrlTo: process.env.NEXT_PUBLIC_CUT_URL_TO || 'https://smartcut.api.movieflow.ai',
        
        // Google OAuth 配置
        googleClientId: process.env.NEXT_PUBLIC_GOOGLE_CLIENT_ID || '847079918888-o1nne8d3ij80dn20qurivo987pv07225.apps.googleusercontent.com',
        googleRedirectUri: process.env.NEXT_PUBLIC_GOOGLE_REDIRECT_URI || '',
        
        // Google Analytics 配置
        gaEnabled: process.env.NEXT_PUBLIC_GA_ENABLED === 'true',
        gaMeasurementId: process.env.NEXT_PUBLIC_GA_MEASUREMENT_ID || 'G-4BDXV6TWF4',
        
        // 视频编辑配置
        videoEditUseMock: process.env.NEXT_PUBLIC_VIDEO_EDIT_USE_MOCK === 'true',
        videoEditUseRemote: process.env.NEXT_PUBLIC_VIDEO_EDIT_USE_REMOTE === 'true',
        videoEditRemoteBase: process.env.NEXT_PUBLIC_VIDEO_EDIT_REMOTE_BASE || '/video-edit',
        
        // 其他配置
        errorConfig: Number(process.env.NEXT_PUBLIC_ERROR_CONFIG) || 0,
    };
};

/**
 * 环境变量配置实例
 */
export const env = getEnvConfig();

/**
 * 导出常用的环境变量配置
 */
export const {
    // 基础配置
    nodeEnv,
    isDevelopment,
    isProduction,
    
    // API 基础 URL 配置
    baseUrl,
    javaUrl,
    cutUrl,
    cutUrlTo,
    
    // Google OAuth 配置
    googleClientId,
    googleRedirectUri,
    
    // Google Analytics 配置
    gaEnabled,
    gaMeasurementId,
    
    // 视频编辑配置
    videoEditUseMock,
    videoEditUseRemote,
    videoEditRemoteBase,
    
    // 其他配置
    errorConfig,
} = env;

/**
 * 获取完整的 Google OAuth 重定向 URI
 */
export const getGoogleRedirectUri = (): string => {
    if (googleRedirectUri) {
        return googleRedirectUri;
    }
    return `${javaUrl}/api/auth/google/callback`;
};

/**
 * 检查是否启用 Google Analytics
 */
export const isGAAvailable = (): boolean => {
    return typeof window !== 'undefined' && 
           typeof window.gtag === 'function' && 
           gaEnabled;
};

/**
 * 获取视频编辑 API 配置
 */
export const getVideoEditApiConfig = () => {
    return {
        useMockApi: videoEditUseMock,
        useRemoteApi: videoEditUseRemote,
        remoteApiBase: videoEditRemoteBase,
        localApiBase: '/api/video-edit',
        enableDebugLog: isDevelopment,
    };
};

/**
 * 验证环境变量配置
 */
export const validateEnvConfig = (): { isValid: boolean; errors: string[] } => {
    const errors: string[] = [];
    
    // 验证必需的配置
    if (!baseUrl) {
        errors.push('NEXT_PUBLIC_BASE_URL is required');
    }
    
    if (!javaUrl) {
        errors.push('NEXT_PUBLIC_JAVA_URL is required');
    }
    
    if (!googleClientId) {
        errors.push('NEXT_PUBLIC_GOOGLE_CLIENT_ID is required');
    }
    
    // 验证 URL 格式
    try {
        new URL(baseUrl);
    } catch {
        errors.push('NEXT_PUBLIC_BASE_URL must be a valid URL');
    }
    
    try {
        new URL(javaUrl);
    } catch {
        errors.push('NEXT_PUBLIC_JAVA_URL must be a valid URL');
    }
    
    return {
        isValid: errors.length === 0,
        errors,
    };
};

/**
 * 开发环境下的配置调试信息
 */
export const logEnvConfig = (): void => {
    if (isDevelopment) {
        console.log('🔧 环境变量配置:', {
            nodeEnv,
            baseUrl,
            javaUrl,
            cutUrl,
            cutUrlTo,
            googleClientId,
            googleRedirectUri: getGoogleRedirectUri(),
            gaEnabled,
            gaMeasurementId,
            videoEditUseMock,
            videoEditUseRemote,
            videoEditRemoteBase,
            errorConfig,
        });
    }
};
