"""
Configuration management for project status documentation.
"""

import os
import json
from typing import Dict, Any, List, Optional


class Config:
    """Configuration manager for project status documentation."""

    def __init__(self, config_file: str = "scripts/progress_config.json"):
        """Initialize with configuration file."""
        self.config_file = config_file
        self.config = self._load_config()

    def _load_config(self) -> Dict[str, Any]:
        """Load configuration from file."""
        default_config = {
            "project_name": "Project",
            "output_dir": "docs/status",
            "status_doc": "status.md",
            "features_file": "docs/features.md",
            "changelog_file": "CHANGELOG.md",
            "git_log_limit": 100,
            "untagged_commit_limit": 50,
            "top_files_limit": 20,
            "exclude_dirs": [
                "node_modules",
                "venv",
                ".git",
                ".vs",
                "bin",
                "obj",
            ],
            "source_extensions": [".py", ".cs", ".js", ".ts", ".html", ".css"],
            "templates": {
                "status": "status_template.md",
                "features": "feature_template.md",
                "changelog": "changelog_template.md",
            },
        }

        if not os.path.exists(self.config_file):
            return default_config

        try:
            with open(self.config_file, "r", encoding="utf-8") as f:
                config = json.load(f)

            # Merge with default config to ensure all keys exist
            merged_config = default_config.copy()
            merged_config.update(config)

            return merged_config
        except Exception:
            return default_config

    def get(self, key: str, default: Any = None) -> Any:
        """Get a configuration value."""
        if "." in key:
            # Handle nested keys like "templates.status"
            parts = key.split(".")
            value = self.config
            for part in parts:
                if isinstance(value, dict) and part in value:
                    value = value[part]
                else:
                    return default
            return value

        return self.config.get(key, default)

    def set(self, key: str, value: Any) -> None:
        """Set a configuration value."""
        if "." in key:
            # Handle nested keys
            parts = key.split(".")
            config = self.config
            for part in parts[:-1]:
                if part not in config:
                    config[part] = {}
                config = config[part]
            config[parts[-1]] = value
        else:
            self.config[key] = value
