"""
    Pygments TLDR formatter tests
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    :copyright: Copyright 2006-2025 by the Pygments team, see AUTHORS.
    :license: BSD, see LICENSE for details.
"""
import logging
logging.basicConfig(
    level=logging.DEBUG,
    format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
)


import os
from io import StringIO
from os import path

import pytest

from pygments_tldr.formatters.tldr import TLDRFormatter
from pygments_tldr.lexers.javascript import JavascriptLexer


class TestTLDRFormatter:
    """Test class for TLDR formatter with hardcoded file path."""
    
    # Hardcoded file path as requested
    HARDCODED_FILE = "/Users/csimoes/Projects/Python/tldr/tempReact/react/packages/react/src/jsx/ReactJSXElement.js"
    logging.info("Info CLASS TestTLDRFormatter STARTED")
    logging.warn("Warn CLASS TestTLDRFormatter STARTED")
    def test_tldr_formatter_with_hardcoded_file(self):
        """Test TLDR formatter against hardcoded React file."""
        # Check if the hardcoded file exists
        if not os.path.exists(self.HARDCODED_FILE):
            pytest.skip(f"Hardcoded file {self.HARDCODED_FILE} does not exist")
            
        # Read the file content
        with open(self.HARDCODED_FILE, 'r', encoding='utf-8') as f:
            code_content = f.read()
        
        # Create lexer and get tokens
        lexer = JavascriptLexer()
        tokens = list(lexer.get_tokens(code_content))
        
        # Test basic TLDR formatting (only extracts function signatures when full=False)
        formatter = TLDRFormatter()
        output = StringIO()
        formatter.format(tokens, output)
        result = output.getvalue()
        
        # Basic assertions - result might be empty if no functions detected
        assert result is not None
        
        # Check if we have JavaScript functions in the source
        has_functions = ('function' in code_content or 
                        'const ' in code_content or 
                        'export ' in code_content)
        
        if has_functions:
            # If functions exist and are detected, we should have output
            # Note: TLDR formatter only outputs function signatures by default (full=False)
            logging.debug(f"Result length: {len(result)}")
            logging.debug(f"Result content: '{result}'")
            # The formatter should process the file without errors
            assert isinstance(result, str)
    
    def test_tldr_formatter_with_function_highlighting(self):
        """Test TLDR formatter with function highlighting enabled."""
        if not os.path.exists(self.HARDCODED_FILE):
            pytest.skip(f"Hardcoded file {self.HARDCODED_FILE} does not exist")
            
        with open(self.HARDCODED_FILE, 'r', encoding='utf-8') as f:
            code_content = f.read()
        
        lexer = JavascriptLexer()
        tokens = list(lexer.get_tokens(code_content))
        
        # Test with function highlighting
        formatter = TLDRFormatter(highlight_functions=True)
        output = StringIO()
        formatter.format(tokens, output)
        result = output.getvalue()
        
        assert result is not None
        # The formatter should process without errors
        assert isinstance(result, str)
    
    def test_tldr_formatter_full_document(self):
        """Test TLDR formatter with full document output."""
        if not os.path.exists(self.HARDCODED_FILE):
            pytest.skip(f"Hardcoded file {self.HARDCODED_FILE} does not exist")
            
        with open(self.HARDCODED_FILE, 'r', encoding='utf-8') as f:
            code_content = f.read()
        
        lexer = JavascriptLexer()
        tokens = list(lexer.get_tokens(code_content))
        
        # Test with full document
        formatter = TLDRFormatter(full=True, title="React Act Test", lang="javascript")
        output = StringIO()
        formatter.format(tokens, output)
        result = output.getvalue()
        
        assert result is not None
        assert len(result) > 0
        # Should contain document header
        if formatter.options.get('full'):
            assert "React Act Test" in result or "Generated by" in result
        # Should contain code fences
        assert "```" in result
    
    def test_tldr_formatter_with_line_numbers(self):
        """Test TLDR formatter with line numbers."""
        if not os.path.exists(self.HARDCODED_FILE):
            pytest.skip(f"Hardcoded file {self.HARDCODED_FILE} does not exist")
            
        with open(self.HARDCODED_FILE, 'r', encoding='utf-8') as f:
            code_content = f.read()
        
        lexer = JavascriptLexer()
        tokens = list(lexer.get_tokens(code_content))
        
        # Test with line numbers
        formatter = TLDRFormatter(full=True, linenos=True, linenostart=1)
        output = StringIO()
        formatter.format(tokens, output)
        result = output.getvalue()
        
        assert result is not None
        assert len(result) > 0
        # Should contain line number indicators when full=True
        if formatter.options.get('full'):
            # Line numbers appear as "   1 |" format
            assert "|" in result