"""Java syntax validation using tree-sitter.

Uses tree-sitter-java for accurate syntax validation.
"""

from __future__ import annotations

from functools import lru_cache

import tree_sitter_java
from tree_sitter import Language, Parser

java_parser = Parser(Language(tree_sitter_java.language()))


@lru_cache(maxsize=100)
def validate_java_syntax(code: str) -> tuple[bool, str | None]:
    """Validate Java syntax using tree-sitter.

    Args:
        code: The Java source code to validate

    Returns:
        Tuple of (is_valid, error_message)

    """
    if not code.strip():
        return False, "Empty code"

    tree = java_parser.parse(bytes(code, "utf8"))
    if tree.root_node.has_error:
        return False, "Invalid Java syntax"
    return True, None
