from typing import Dict, Optional, List


class LanguageRegistry:
    """Registry mapping programming languages to execution, linting, and extension settings."""

    def __init__(self) -> None:
        self._languages: Dict[str, Dict[str, str]] = {
            "python": {
                "name": "Python",
                "ext": ".py",
                "exec": "python3",
                "lint": "flake8",
            },
            "csharp": {
                "name": "C#",
                "ext": ".cs",
                "exec": "dotnet run",
                "lint": "dotnet build",
            },
            "cpp": {
                "name": "C++",
                "ext": ".cpp",
                "exec": "g++ -O2 -o main main.cpp && ./main",
                "lint": "g++ -fsyntax-only main.cpp",
            },
            "java": {
                "name": "Java",
                "ext": ".java",
                "exec": "javac Main.java && java Main",
                "lint": "javac Main.java",
            },
            "javascript": {
                "name": "JavaScript",
                "ext": ".js",
                "exec": "node",
                "lint": "node --check",
            },
            "typescript": {
                "name": "TypeScript",
                "ext": ".ts",
                "exec": "npx ts-node",
                "lint": "npx tsc --noEmit",
            },
            "rust": {
                "name": "Rust",
                "ext": ".rs",
                "exec": "rustc main.rs -o main && ./main",
                "lint": "rustc --emit=metadata main.rs",
            },
            "lua": {
                "name": "Lua",
                "ext": ".lua",
                "exec": "lua",
                "lint": "luac -p",
            },
            "html": {
                "name": "HTML",
                "ext": ".html",
                "exec": "cat",
                "lint": "tidy -e -q",
            },
            "go": {
                "name": "Go",
                "ext": ".go",
                "exec": "go run",
                "lint": "go vet",
            },
        }

        # Language aliases mapping
        self._aliases: Dict[str, str] = {
            "py": "python",
            "c#": "csharp",
            "cs": "csharp",
            "c++": "cpp",
            "cxx": "cpp",
            "js": "javascript",
            "node": "javascript",
            "ts": "typescript",
            "rs": "rust",
            "golang": "go",
        }

    def canonical_name(self, language: str) -> str:
        lang_lower = language.lower().strip()
        return self._aliases.get(lang_lower, lang_lower)

    def get_config(self, language: str) -> Optional[Dict[str, str]]:
        key = self.canonical_name(language)
        return self._languages.get(key)

    def list_languages(self) -> List[str]:
        return list(self._languages.keys())


registry = LanguageRegistry()
