"""
Configuration for gpathex
"""
import os
from pathlib import Path
from typing import Any, Dict


class Config:
    """Configuration manager."""
    
    _instance = None
    _config: Dict[str, Any] = {
        # URLs
        "KEGG_ORG_URL": "http://www.kegg.jp/kegg/catalog/org_list.html",
        "KEGG_BASE_URL": "https://www.kegg.jp",
        "KEGG_REST_URL": "https://rest.kegg.jp",
        
        # Directories
        "DEFAULT_OUTPUT_DIR": str(Path.cwd()),
        "DEFAULT_ORG_FILE": "kegg_organisms.tsv",
        "DEFAULT_RANKS_FILE": "kegg_ranks.tsv",
        
        # Download settings
        "MAX_RETRIES": 3,
        "RETRY_DELAY": 5,
        "DOWNLOAD_TIMEOUT": 30,
        "DEFAULT_CONCURRENT": 5,
        "RATE_LIMIT_DELAY": 1.0,
        
        # File formats
        "ALLOWED_FASTA_FORMATS": [".fa", ".fasta", ".fa.gz", ".fasta.gz", ".fa.bz2", ".fasta.bz2"],
        
        # Performance
        "USE_POLARS": True,
        "USE_BIOPYTHON": True,
        "CHUNK_SIZE": 10000,
        
        # Logging
        "LOG_FORMAT": "[%(levelname)s] %(asctime)s - %(name)s - %(message)s",
        "LOG_LEVEL": "INFO",
        "COLOR_OUTPUT": True,
    }
    
    def __new__(cls):
        if cls._instance is None:
            cls._instance = super().__new__(cls)
        return cls._instance
    
    def __init__(self):
        self._setup_directories()
    
    def _setup_directories(self):
        """Ensure necessary directories exist."""
        output_dir = Path(self.get("DEFAULT_OUTPUT_DIR"))
        output_dir.mkdir(parents=True, exist_ok=True)
    
    def get(self, key: str, default: Any = None) -> Any:
        """Get configuration value."""
        return self._config.get(key, default)
    
    def set(self, key: str, value: Any) -> None:
        """Set configuration value."""
        self._config[key] = value
    
    def update(self, config_dict: Dict[str, Any]) -> None:
        """Update multiple configuration values."""
        self._config.update(config_dict)
    
    @property
    def all(self) -> Dict[str, Any]:
        """Get all configuration values."""
        return self._config.copy()


# Global config instance
config = Config()
