# Path: core/lsp/scaffold_features/symbols/providers/variables.py
# ---------------------------------------------------------------

import re
from typing import Optional
from ....base.types import DocumentSymbol, SymbolKind
from .base import BaseSymbolProvider


class VariableProvider(BaseSymbolProvider):
    """
    [THE KEEPER OF NAMES]
    Maps Gnostic Variable definitions.
    """

    # Matches: $$ name: type = value
    # Matches: let name = value
    PATTERN = re.compile(r'^\s*(\$\$|let|def|const)\s+([a-zA-Z_]\w*)(?:\s*:\s*([a-zA-Z_]\w*))?')

    def extract(self, line_num: int, raw_line: str, stripped: str) -> Optional[DocumentSymbol]:
        match = self.PATTERN.match(raw_line)
        if match:
            sigil, name, type_hint = match.groups()

            # [ASCENSION]: Enriched Detail
            detail = f"Variable{f': {type_hint}' if type_hint else ''}"
            if sigil == 'const': detail = "Constant (Immutable)"

            return self.forge_vessel(
                name=name,
                detail=detail,
                kind=SymbolKind.Variable,
                line=line_num,
                start_col=raw_line.find(name),
                length=len(name)
            )
        return None