# xgen_doc2chunk/core/processor/text_handler.py
"""
Text Handler - Text File Processor

Class-based handler for text files inheriting from BaseHandler.
"""
import logging
from typing import List, Optional, TYPE_CHECKING

from xgen_doc2chunk.core.processor.base_handler import BaseHandler
from xgen_doc2chunk.core.functions.utils import clean_text, clean_code_text
from xgen_doc2chunk.core.functions.chart_extractor import BaseChartExtractor, NullChartExtractor
from xgen_doc2chunk.core.processor.text_helper.text_image_processor import TextImageProcessor
from xgen_doc2chunk.core.functions.img_processor import ImageProcessor

if TYPE_CHECKING:
    from xgen_doc2chunk.core.document_processor import CurrentFile

logger = logging.getLogger("document-processor")


DEFAULT_ENCODINGS = ['utf-8', 'utf-8-sig', 'cp949', 'euc-kr', 'latin-1', 'ascii']


class TextHandler(BaseHandler):
    """Text File Processing Handler Class"""

    def _create_file_converter(self):
        """Create text-specific file converter."""
        from xgen_doc2chunk.core.processor.text_helper.text_file_converter import TextFileConverter
        return TextFileConverter()

    def _create_preprocessor(self):
        """Create text-specific preprocessor."""
        from xgen_doc2chunk.core.processor.text_helper.text_preprocessor import TextPreprocessor
        return TextPreprocessor()

    def _create_chart_extractor(self) -> BaseChartExtractor:
        """Text files do not contain charts. Return NullChartExtractor."""
        return NullChartExtractor(self._chart_processor)

    def _create_metadata_extractor(self):
        """Text files do not have embedded metadata. Return None (uses NullMetadataExtractor)."""
        return None

    def _create_format_image_processor(self) -> ImageProcessor:
        """Create text-specific image processor."""
        return TextImageProcessor()

    def extract_text(
        self,
        current_file: "CurrentFile",
        extract_metadata: bool = True,
        file_type: Optional[str] = None,
        encodings: Optional[List[str]] = None,
        is_code: bool = False,
        **kwargs
    ) -> str:
        """
        Extract text from text file.

        Args:
            current_file: CurrentFile dict containing file info and binary data
            extract_metadata: Whether to extract metadata (ignored for text files)
            file_type: File type (extension)
            encodings: List of encodings to try
            is_code: Whether this is a code file
            **kwargs: Additional options

        Returns:
            Extracted text
        """
        file_path = current_file.get("file_path", "unknown")
        file_data = current_file.get("file_data", b"")
        enc = encodings or DEFAULT_ENCODINGS

        # Step 1: No file_converter for text files (direct decode)
        # Step 2: Preprocess - clean_content is the TRUE SOURCE
        preprocessed = self.preprocess(file_data)
        file_data = preprocessed.clean_content  # TRUE SOURCE

        for e in enc:
            try:
                text = file_data.decode(e)
                self.logger.info(f"Successfully decoded {file_path} with {e} encoding")
                return clean_code_text(text) if is_code else clean_text(text)
            except UnicodeDecodeError:
                self.logger.debug(f"Failed to decode {file_path} with {e}, trying next...")
                continue
            except Exception as ex:
                self.logger.error(f"Error decoding file {file_path} with {e}: {ex}")
                continue

        raise Exception(f"Could not decode file {file_path} with any supported encoding")

