"""
Configuration management for Link Checker package
"""

import os
import json

# Try relative import first, fall back to absolute
try:
    from .exceptions import ConfigurationError
except ImportError:
    from exceptions import ConfigurationError

# Default configuration
DEFAULT_CONFIG = {
    'database': {
        'host': '37.27.12.199',
        'user': 'axg_crm_new1',
        'password': 'Yyv1QT2JUMLyxt71cYLG',
        'dbname': 'axg_crm_new',
        'autocommit': True
    },
    'api': {
        'solr_api_key': 'aecaa0fe-4673-436d-a798-1601cf593f64',
        'proxy_api_token': 'c9f53cf2f4e28761654178dbe22fb9479b27f1c1'
    },
    'paths': {
        'scraper_path': '/home/moses/scraper',
        'user_agents_script': '/home/moses/scripts/user_agents.js',
        'chromedriver_path': '/opt/chromedriver'
    },
    'solr': {
        'url': 'http://127.0.0.1:8983/solr/check_status',
        'auth': ('axghouse', 'SolrRocks')
    }
}

# Server-specific configurations
SERVER_CONFIGS = {
    'autocheck': {
        'paths': {
            'scraper_path': '/home/moses/scraper',
            'user_agents_script': '/home/moses/scripts/user_agents.js',
            'chromedriver_path': '/opt/chromedriver'
        }
    },
    'linkverification': {
        'paths': {
            'scraper_path': '/root/scraper',
            'user_agents_script': '/root/user_agents.js',
            'chromedriver_path': '/opt/chromedriver'
        }
    },
    'manualcheckstatus': {
        'paths': {
            'scraper_path': '/home/moses/scraper',
            'user_agents_script': '/home/moses/scripts/user_agents.js',
            'chromedriver_path': '/opt/chromedriver'
        }
    },
    'testtool': {
        'paths': {
            'scraper_path': '/home/moses/scraper',
            'user_agents_script': '/home/moses/scripts/user_agents.js',
            'chromedriver_path': '/opt/chromedriver'
        }
    }
}

def get_config(server_type=None, config_file=None):
    """
    Get configuration for the specified server type
    
    Args:
        server_type (str): Type of server (autocheck, linkverification, etc.)
        config_file (str): Path to custom config file
    
    Returns:
        dict: Configuration dictionary
    """
    config = DEFAULT_CONFIG.copy()
    
    # Load custom config file if provided
    if config_file and os.path.exists(config_file):
        try:
            with open(config_file, 'r') as f:
                custom_config = json.load(f)
                config.update(custom_config)
        except Exception as e:
            raise ConfigurationError(f"Failed to load config file {config_file}: {e}")
    
    # Apply server-specific configuration
    if server_type and server_type in SERVER_CONFIGS:
        server_config = SERVER_CONFIGS[server_type]
        for key, value in server_config.items():
            if key in config:
                config[key].update(value)
            else:
                config[key] = value
    
    return config

def update_config(server_type, updates):
    """
    Update configuration for a specific server type
    
    Args:
        server_type (str): Type of server
        updates (dict): Configuration updates
    """
    if server_type not in SERVER_CONFIGS:
        SERVER_CONFIGS[server_type] = {}
    
    SERVER_CONFIGS[server_type].update(updates)
