// Vigthoria Configuration and Constants
// Extracted from monolithic extension.js for proper modularity

const SUBSCRIPTION_TIERS = {
    free: {
        name: 'Free',
        maxRequests: 20,
        maxFileSize: 1024 * 100, // 100KB
        allowedModes: ['ask'],
        features: ['basic_chat', 'question_answering'],
        analytics: false,
        priority: 'low'
    },
    developer: {
        name: 'Developer',
        maxRequests: 100,
        maxFileSize: 1024 * 1024, // 1MB
        allowedModes: ['ask', 'build', 'chat', 'assistant'],
        features: ['basic_chat', 'code_explanation', 'question_answering', 'code_generation'],
        analytics: false,
        priority: 'normal'
    },
    basic: {
        name: 'Developer',
        maxRequests: 100,
        maxFileSize: 1024 * 1024, // 1MB
        allowedModes: ['ask', 'build', 'chat', 'assistant'],
        features: ['basic_chat', 'code_explanation', 'question_answering', 'code_generation'],
        analytics: false,
        priority: 'normal'
    },
    pro: {
        name: 'Pro',
        maxRequests: 1000,
        maxFileSize: 10 * 1024 * 1024, // 10MB
        allowedModes: ['ask', 'build', 'agent', 'chat', 'assistant', 'developer'],
        features: ['basic_chat', 'code_explanation', 'advanced_completion', 'project_analysis', 'autonomous_coding'],
        analytics: true,
        priority: 'high'
    },
    professional: {
        name: 'Professional',
        maxRequests: 1000,
        maxFileSize: 10 * 1024 * 1024, // 10MB
        allowedModes: ['ask', 'build', 'agent', 'chat', 'assistant', 'developer'],
        features: ['basic_chat', 'code_explanation', 'advanced_completion', 'project_analysis', 'autonomous_coding'],
        analytics: true,
        priority: 'high'
    },
    premium: {
        name: 'Pro', 
        maxRequests: 1000,
        maxFileSize: 10 * 1024 * 1024, // 10MB
        allowedModes: ['ask', 'build', 'agent', 'chat', 'assistant', 'developer'],
        features: ['basic_chat', 'code_explanation', 'advanced_completion', 'project_analysis', 'autonomous_coding'],
        analytics: true,
        priority: 'high'
    },
    enterprise: {
        name: 'Enterprise',
        maxRequests: -1, // unlimited
        maxFileSize: 100 * 1024 * 1024, // 100MB
        allowedModes: ['ask', 'build', 'agent', 'operator', 'chat', 'assistant', 'developer', 'autonomous'],
        features: ['all'],
        analytics: true,
        priority: 'enterprise',
        customAI: true
    },
    admin: {
        name: 'Admin',
        maxRequests: -1,
        maxFileSize: 100 * 1024 * 1024,
        allowedModes: ['ask', 'build', 'agent', 'operator', 'chat', 'assistant', 'developer', 'autonomous'],
        features: ['all'],
        analytics: true,
        priority: 'enterprise',
        customAI: true
    },
    master_admin: {
        name: 'Master Admin',
        maxRequests: -1,
        maxFileSize: 100 * 1024 * 1024,
        allowedModes: ['ask', 'build', 'agent', 'operator', 'chat', 'assistant', 'developer', 'autonomous'],
        features: ['all'],
        analytics: true,
        priority: 'enterprise',
        customAI: true
    }
};

const SECURITY_CONFIG = {
    maxRequestSize: 10 * 1024 * 1024, // 10MB
    timeout: 30000, // 30 seconds
    allowedFileTypes: ['.js', '.ts', '.py', '.java', '.cpp', '.c', '.h', '.css', '.html', '.json', '.md', '.txt'],
    blacklistedExtensions: ['.exe', '.bat', '.cmd', '.ps1', '.sh'],
    admin: {
        allowedCommands: ['npm', 'node', 'git', 'code', 'mkdir', 'touch', 'ls', 'pwd', 'cat'],
        restrictedPaths: ['/', '/etc', '/usr', '/bin', '/sbin', '/var'],
        maxConcurrentTerminals: 3
    },
    user: {
        allowedCommands: ['npm install', 'npm run', 'git status', 'git add', 'git commit', 'ls', 'pwd', 'cat'],
        restrictedPaths: ['/', '/etc', '/usr', '/bin', '/sbin'],
        maxConcurrentTerminals: 1
    },
    rateLimiting: {
        requestsPerMinute: 60,
        requestsPerHour: 1000,
        requestsPerDay: 10000
    }
};

const DEFAULT_IDE_HOST = process.env.VIGTHORIA_IDE_HOST || 'https://coder.vigthoria.io';
const DEFAULT_API_HOST = process.env.VIGTHORIA_API_HOST || 'https://coder.vigthoria.io';
const DEFAULT_USER_PORT = process.env.VIGTHORIA_USER_PORT || '443';
const DEFAULT_ADMIN_PORT = process.env.VIGTHORIA_ADMIN_PORT || '443';
const DEFAULT_IDE_PORT = process.env.VIGTHORIA_IDE_PORT || '443';
const DEFAULT_API_PORT = process.env.VIGTHORIA_API_PORT || '443';
const DEFAULT_MODEL_ROUTER_PORT = process.env.VIGTHORIA_MODEL_ROUTER_PORT || '443';

const normalizeUrl = (explicitUrl, host, port) => {
    if (explicitUrl) {
        return explicitUrl.replace(/\/$/, '');
    }

    const base = host || 'http://localhost';

    try {
        const normalized = base.includes('://') ? base : `http://${base}`;
        const url = new URL(normalized);
        if (!url.port) {
            url.port = port;
        }
        return url.toString().replace(/\/$/, '');
    } catch (error) {
        return `${base.replace(/\/$/, '')}:${port}`;
    }
};

const CONFIG = {
    server: {
        ideUrl: normalizeUrl(process.env.VIGTHORIA_IDE_URL, DEFAULT_IDE_HOST, DEFAULT_IDE_PORT),
        ideFallbackUrl: normalizeUrl(process.env.VIGTHORIA_IDE_FALLBACK_URL, DEFAULT_IDE_HOST, DEFAULT_IDE_PORT),
        apiUrl: normalizeUrl(process.env.VIGTHORIA_API_URL, DEFAULT_API_HOST, DEFAULT_API_PORT),
        apiFallbackUrl: normalizeUrl(process.env.VIGTHORIA_API_FALLBACK_URL, DEFAULT_API_HOST, DEFAULT_API_PORT),
        userUrl: normalizeUrl(process.env.VIGTHORIA_USER_URL, DEFAULT_IDE_HOST, DEFAULT_USER_PORT),
        adminUrl: normalizeUrl(process.env.VIGTHORIA_ADMIN_URL, DEFAULT_IDE_HOST, DEFAULT_ADMIN_PORT),
        dashboardUrl: normalizeUrl(process.env.VIGTHORIA_DASHBOARD_URL, DEFAULT_API_HOST, DEFAULT_API_PORT),
        modelRouterUrl: process.env.VIGTHORIA_MODEL_ROUTER_URL || 'https://coder.vigthoria.io/api/model-router',
        modelRouterFallbackUrl: process.env.VIGTHORIA_MODEL_ROUTER_FALLBACK_URL || 'https://coder.vigthoria.io/api/model-router',
        healthPaths: ['/health', '/api/health', '/'],
        healthCheckInterval: 30000,
        reconnectAttempts: 3,
        reconnectDelay: 5000,
        services: {
            ollama: process.env.VIGTHORIA_OLLAMA_URL || 'https://coder.vigthoria.io/api/ollama',
            huggingface: process.env.VIGTHORIA_HF_URL || 'https://coder.vigthoria.io/api/huggingface',
            agents: process.env.VIGTHORIA_AGENT_URL || 'https://agent.vigthoria.io',
            agentCoder: process.env.VIGTHORIA_AGENT_CODER_URL || 'https://coder.vigthoria.io/api/agent',
            analytics: process.env.VIGTHORIA_ANALYTICS_URL || 'https://coder.vigthoria.io/api/analytics'
        }
    },
    ai: {
        model: 'vigthoria-v3-code-30b',
        temperature: 0.7,
        maxTokens: 4000,
        timeout: 30000,
        fallbackModel: 'vigthoria-fast-1.7b'
    },
    features: {
        autoSave: true,
        contextAwareness: true,
        predictiveCompletion: true,
        realTimeAnalysis: true,
        collaborativeEditing: false,
        advancedDebugging: true
    },
    ui: {
        theme: 'dark',
        animations: true,
        notifications: true,
        soundEffects: false
    },
    performance: {
        cacheSize: 100 * 1024 * 1024, // 100MB
        maxHistoryEntries: 1000,
        backgroundProcessing: true,
        optimizedRendering: true
    },
    analytics: {
        enabled: true,
        anonymizeData: true,
        reportErrors: true,
        performanceMetrics: true
    }
};

const DEFAULT_SETTINGS = {
    mode: 'assistant',
    subscription: 'developer',
    autoActivate: true,
    showWelcome: true,
    enableNotifications: true,
    maxChatHistory: 100
};

// AI Modes Configuration
const AI_MODES = {
    ask: {
        name: 'ASK Mode',
        description: 'Quick Q&A using Vigthoria AI models',
        icon: '❓',
        features: ['question_answering', 'explanations', 'quick_help'],
        subscription: 'developer',
        preferredModels: ['vigthoria-fast-1.7b', 'vigthoria-v3-code-30b'],
        endpoint: 'model-router'
    },
    build: {
        name: 'BUILD Mode',
        description: 'Code generation and development using Vigthoria models',
        icon: '🔨',
        features: ['code_generation', 'scaffolding', 'architecture', 'implementation'],
        subscription: 'developer',
        preferredModels: ['vigthoria-v3-code-30b', 'deepseek-coder-v2:latest'],
        endpoint: 'model-router'
    },
    agent: {
        name: 'AGENT Mode',
        description: 'Autonomous agent for complex development tasks',
        icon: '🤖',
        features: ['autonomous_coding', 'project_analysis', 'multi_step_tasks'],
        subscription: 'pro',
        preferredModels: ['vigthoria-v3-code-30b', 'deepseek-coder-v2:latest'],
        endpoint: 'agent-service',
        delegation: true
    },
    operator: {
        name: 'OPERATOR Mode',
        description: 'System operations, deployment, and infrastructure management',
        icon: '⚙️',
        features: ['system_management', 'deployment', 'infrastructure'],
        subscription: 'enterprise',
        preferredModels: ['vigthoria-v3-code-30b', 'deepseek-coder-v2:latest'],
        endpoint: 'operator-service',
        delegation: true
    },
    chat: {
        name: 'Chat Mode',
        description: 'Basic conversational AI assistance',
        icon: '💬',
        features: ['text_chat', 'basic_help'],
        subscription: 'developer',
        preferredModels: ['vigthoria-fast-1.7b', 'vigthoria-v3-code-30b']
    },
    assistant: {
        name: 'Assistant Mode', 
        description: 'Intelligent development assistant',
        icon: '🤖',
        features: ['code_explanation', 'suggestions', 'documentation'],
        subscription: 'developer',
        preferredModels: ['vigthoria-v3-code-30b', 'vigthoria-fast-1.7b']
    },
    developer: {
        name: 'Developer Mode',
        description: 'Advanced development tools and analysis',
        icon: '👨‍💻',
        features: ['code_generation', 'refactoring', 'testing', 'debugging'],
        subscription: 'pro',
        preferredModels: ['vigthoria-v3-code-30b', 'deepseek-coder-v2:latest']
    },
    autonomous: {
        name: 'Autonomous Mode',
        description: 'Fully autonomous AI development agent',
        icon: '🚀',
        features: ['full_autonomy', 'project_management', 'advanced_ai'],
        subscription: 'enterprise',
        preferredModels: ['vigthoria-v3-code-30b', 'deepseek-coder-v2:latest']
    }
};

// Error Messages
const ERROR_MESSAGES = {
    SUBSCRIPTION_REQUIRED: 'This feature requires an eligible Vigthoria subscription',
    MODE_NOT_AVAILABLE: 'This mode is not available for your subscription tier',
    SERVER_UNREACHABLE: 'Unable to connect to Vigthoria servers',
    INVALID_REQUEST: 'Invalid request format',
    RATE_LIMIT_EXCEEDED: 'Rate limit exceeded. Please try again later',
    FILE_TOO_LARGE: 'File size exceeds maximum limit',
    UNSUPPORTED_FILE_TYPE: 'Unsupported file type',
    SECURITY_VIOLATION: 'Security policy violation detected'
};

// Success Messages
const SUCCESS_MESSAGES = {
    MODE_SWITCHED: 'Successfully switched to {mode} mode',
    SUBSCRIPTION_UPGRADED: 'Subscription upgraded successfully',
    FEATURE_ENABLED: '{feature} has been enabled',
    CONNECTION_RESTORED: 'Connection to Vigthoria servers restored',
    SETTINGS_SAVED: 'Settings saved successfully'
};

module.exports = {
    SUBSCRIPTION_TIERS,
    SECURITY_CONFIG,
    CONFIG,
    DEFAULT_SETTINGS,
    AI_MODES,
    ERROR_MESSAGES,
    SUCCESS_MESSAGES
};
