# Copyright (c) 2026 MergeGuide, Inc. All rights reserved.
# SPDX-License-Identifier: Proprietary
"""Canonical language registry for MergeGuide.

Single source of truth for file extension → language mapping.
All consumers (engine, CLI, MCP server) should import from here.
"""

from __future__ import annotations

import os

# Canonical mapping of file extensions to language identifiers.
# Normalized: shell variants → "bash", unknown → "unknown".
LANGUAGE_REGISTRY: dict[str, str] = {
    # Python
    ".py": "python",
    ".pyw": "python",
    ".pyi": "python",
    # JavaScript
    ".js": "javascript",
    ".jsx": "javascript",
    ".mjs": "javascript",
    ".cjs": "javascript",
    # TypeScript
    ".ts": "typescript",
    ".tsx": "typescript",
    # Java
    ".java": "java",
    # Go
    ".go": "go",
    # Rust
    ".rs": "rust",
    # Ruby
    ".rb": "ruby",
    ".rake": "ruby",
    ".gemspec": "ruby",
    # PHP
    ".php": "php",
    # C#
    ".cs": "csharp",
    # C/C++
    ".c": "c",
    ".h": "c",
    ".cpp": "cpp",
    ".hpp": "cpp",
    ".cc": "cpp",
    ".cxx": "cpp",
    # Swift
    ".swift": "swift",
    # Kotlin
    ".kt": "kotlin",
    ".kts": "kotlin",
    # Scala
    ".scala": "scala",
    # Shell / Bash
    ".sh": "bash",
    ".bash": "bash",
    ".zsh": "bash",
    # PowerShell
    ".ps1": "powershell",
    ".psm1": "powershell",
    # Dart
    ".dart": "dart",
    # R
    ".r": "r",
    ".R": "r",
    # Elixir
    ".ex": "elixir",
    ".exs": "elixir",
    # Terraform / HCL
    ".tf": "terraform",
    ".tfvars": "terraform",
    ".hcl": "terraform",
    # Dockerfile
    ".dockerfile": "dockerfile",
    # Markup / Data
    ".yaml": "yaml",
    ".yml": "yaml",
    ".json": "json",
    ".xml": "xml",
    ".html": "html",
    ".htm": "html",
    ".css": "css",
    ".scss": "scss",
    ".less": "less",
    ".md": "markdown",
    ".markdown": "markdown",
    # SQL
    ".sql": "sql",
    # Groovy (Jenkinsfile)
    ".groovy": "groovy",
    ".gradle": "groovy",
}

# Special filename → language mappings (no extension)
FILENAME_REGISTRY: dict[str, str] = {
    "Dockerfile": "dockerfile",
    "Jenkinsfile": "groovy",
    "Makefile": "make",
    "Rakefile": "ruby",
    "Gemfile": "ruby",
}


def detect_language(path: str) -> str:
    """Detect programming language from a file path.

    Checks extension first, then falls back to filename matching.

    Args:
        path: File path to analyze.

    Returns:
        Language identifier string, or "unknown" if not recognized.
    """
    _, ext = os.path.splitext(path)
    if ext:
        lang = LANGUAGE_REGISTRY.get(ext)
        if lang:
            return lang

    basename = os.path.basename(path)
    return FILENAME_REGISTRY.get(basename, "unknown")


def get_source_extensions() -> list[str]:
    """Get glob patterns for all known source file extensions and special filenames.

    Returns:
        List of glob patterns like ["*.py", "*.js", ...] plus special
        filenames like "Dockerfile", "Jenkinsfile", etc.
    """
    patterns = [f"*{ext}" for ext in LANGUAGE_REGISTRY]
    # Include special filenames that have no extension (Dockerfile, Jenkinsfile, etc.)
    patterns.extend(FILENAME_REGISTRY.keys())
    return patterns
