"""Base processor class for HTML transformations."""

from abc import ABC, abstractmethod
from typing import Dict, Any, Optional
from pathlib import Path
from bs4 import BeautifulSoup


class Processor(ABC):
    """Base class for all HTML processors.
    
    Each processor applies specific transformations to the HTML content
    generated by Jupyter Book. Processors are executed in priority order.
    """
    
    def __init__(self, config: Optional[Dict[str, Any]] = None):
        """Initialize processor with configuration.
        
        Args:
            config: Processor-specific configuration dictionary
        """
        self.config = config or {}
        self.enabled = self.config.get("enabled", True)
    
    @property
    @abstractmethod
    def name(self) -> str:
        """Return the processor name."""
        pass
    
    @property
    def priority(self) -> int:
        """Return execution priority (lower numbers run first).
        
        Default priority levels:
            0-10: Pre-processing (structure modifications)
            10-50: Content transformations
            50-90: Styling and layout
            90-100: Post-processing (cleanup, validation)
        """
        return 50
    
    @abstractmethod
    def process(self, html_content: str, soup: BeautifulSoup, context: Dict[str, Any]) -> str:
        """Process the HTML content.
        
        Args:
            html_content: Raw HTML string
            soup: BeautifulSoup parsed HTML
            context: Shared context dictionary with metadata
            
        Returns:
            Processed HTML string
        """
        pass
    
    def process_file(self, file_path: Path, context: Dict[str, Any]) -> None:
        """Process a single HTML file.
        
        Args:
            file_path: Path to HTML file to process
            context: Shared context dictionary
        """
        if not self.enabled:
            return
            
        html_content = file_path.read_text(encoding="utf-8")
        soup = BeautifulSoup(html_content, "lxml")
        
        processed_html = self.process(html_content, soup, context)
        
        file_path.write_text(processed_html, encoding="utf-8")
    
    def __repr__(self) -> str:
        return f"{self.__class__.__name__}(priority={self.priority}, enabled={self.enabled})"
