"""LaTeX export service.

This module provides the LaTeXExporter class that wraps the existing
LaTeXExporter implementation from text_optimization.citation_formatter.
"""

from typing import Optional

from loguru import logger

from .base import BaseExporter, ExportOptions, ExportResult
from .registry import ExporterRegistry


@ExporterRegistry.register
class LaTeXExporter(BaseExporter):
    """LaTeX exporter using the existing LaTeXExporter implementation.

    This exporter converts markdown content to LaTeX format suitable for
    compilation with pdflatex or similar LaTeX processors.
    """

    def __init__(self):
        """Initialize LaTeX exporter."""
        # Import here to avoid circular imports
        from ..text_optimization.citation_formatter import (
            LaTeXExporter as LegacyLaTeXExporter,
        )

        self._legacy = LegacyLaTeXExporter()

    @property
    def format_name(self) -> str:
        return "latex"

    @property
    def file_extension(self) -> str:
        return ".tex"

    @property
    def mimetype(self) -> str:
        return "text/plain"

    def export(
        self,
        markdown_content: str,
        options: Optional[ExportOptions] = None,
    ) -> ExportResult:
        """Convert markdown content to LaTeX.

        Args:
            markdown_content: The markdown text to convert
            options: Optional export options (title, metadata)

        Returns:
            ExportResult with LaTeX content as UTF-8 bytes, filename, and mimetype

        Raises:
            ValueError: If content exceeds maximum size limit
        """
        try:
            # Check content size limit to prevent OOM errors
            self._validate_content_size(markdown_content)

            options = options or ExportOptions()

            content = self._legacy.export_to_latex(markdown_content)
            filename = self._generate_safe_filename(options.title)

            logger.info(
                f"Generated LaTeX in memory, size: {len(content)} bytes"
            )

            return ExportResult(
                content=content.encode("utf-8"),
                filename=filename,
                mimetype=self.mimetype,
            )

        except Exception:
            logger.exception("Error generating LaTeX")
            raise
