"""RIS export service.

This module provides the RISExporter class that wraps the existing
RISExporter 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 RISExporter(BaseExporter):
    """RIS exporter using the existing RISExporter implementation.

    This exporter extracts references from markdown content and converts
    them to RIS (Research Information Systems) format, which can be
    imported into reference managers like Zotero, Mendeley, or EndNote.
    """

    def __init__(self):
        """Initialize RIS exporter."""
        # Import here to avoid circular imports
        from ..text_optimization.citation_formatter import (
            RISExporter as LegacyRISExporter,
        )

        self._legacy = LegacyRISExporter()

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

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

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

    def export(
        self,
        markdown_content: str,
        options: Optional[ExportOptions] = None,
    ) -> ExportResult:
        """Extract references from markdown and convert to RIS format.

        Args:
            markdown_content: The markdown text containing sources/references
            options: Optional export options (title, metadata)

        Returns:
            ExportResult with RIS 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_ris(markdown_content)
            filename = self._generate_safe_filename(options.title)

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

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

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