"""Exporter registry for document format discovery.

This module provides the ExporterRegistry class that manages exporter
registration and lookup. Exporters can be registered using the @register
decorator for automatic discovery.
"""

from typing import Dict, List, Optional, Type

from loguru import logger

from .base import BaseExporter


class ExporterRegistry:
    """Registry for document exporters.

    This class manages the registration and lookup of exporter classes.
    Exporters can be registered using the @register class method as a decorator.

    Example:
        @ExporterRegistry.register
        class PDFExporter(BaseExporter):
            ...

        # Later, to get the exporter:
        exporter = ExporterRegistry.get_exporter("pdf")
    """

    _exporters: Dict[str, Type[BaseExporter]] = {}
    _instances: Dict[str, BaseExporter] = {}

    @classmethod
    def register(cls, exporter_class: Type[BaseExporter]) -> Type[BaseExporter]:
        """Register an exporter class.

        Can be used as a decorator:
            @ExporterRegistry.register
            class MyExporter(BaseExporter):
                ...

        Args:
            exporter_class: The exporter class to register

        Returns:
            The exporter class (unchanged), allowing use as decorator
        """
        # Instantiate temporarily to get format_name
        instance = exporter_class()
        format_name = instance.format_name.lower()
        cls._exporters[format_name] = exporter_class
        logger.debug(f"Registered exporter for format: {format_name}")
        return exporter_class

    @classmethod
    def get_exporter(cls, format_name: str) -> Optional[BaseExporter]:
        """Get an exporter instance for the given format.

        Uses singleton pattern - returns cached instance if available.

        Args:
            format_name: The format identifier (e.g., 'pdf', 'odt')

        Returns:
            An exporter instance, or None if format not supported
        """
        format_name = format_name.lower()

        # Return cached instance if available
        if format_name in cls._instances:
            return cls._instances[format_name]

        # Create new instance
        exporter_class = cls._exporters.get(format_name)
        if exporter_class:
            instance = exporter_class()
            cls._instances[format_name] = instance
            return instance

        return None

    @classmethod
    def get_available_formats(cls) -> List[str]:
        """Get list of available export formats.

        Returns:
            List of format identifiers (e.g., ['pdf', 'odt', 'latex'])
        """
        return list(cls._exporters.keys())

    @classmethod
    def is_format_supported(cls, format_name: str) -> bool:
        """Check if a format is supported.

        Args:
            format_name: The format identifier to check

        Returns:
            True if the format is supported, False otherwise
        """
        return format_name.lower() in cls._exporters

    @classmethod
    def clear(cls) -> None:
        """Clear all registered exporters.

        Primarily useful for testing.
        """
        cls._exporters.clear()
        cls._instances.clear()
