#!/usr/bin/env python3
"""
Enhanced Base Scraper with Cumulative Markdown Support
Extension of base_scraper.py that adds cumulative markdown functionality
"""

from src.base_scraper import BaseScraper
from src.cumulative_markdown_manager import CumulativeMarkdownManager
from pathlib import Path
from typing import List, Dict, Any, Optional


class BaseScraperCumulative(BaseScraper):
    """Base scraper with cumulative markdown support."""
    
    def __init__(self, config, use_cumulative: bool = True):
        """Initialize with optional cumulative mode."""
        super().__init__(config)
        self.use_cumulative = use_cumulative
        
        if self.use_cumulative:
            self.cumulative_manager = CumulativeMarkdownManager(config, self.logger)
            self.logger.info("Initialized with cumulative markdown mode")
    
    def save_content(self, items: List[Dict[str, Any]]) -> Optional[Path]:
        """Save content using either cumulative or traditional mode."""
        if not items:
            self.logger.warning("No items to save")
            return None
        
        if self.use_cumulative:
            # Use cumulative manager
            return self.cumulative_manager.save_cumulative(
                items, 
                self.format_markdown
            )
        else:
            # Use traditional save (creates new file each time)
            markdown = self.format_markdown(items)
            return self.save_markdown(markdown)
    
    def run(self) -> Optional[Path]:
        """Run the scraper with cumulative support."""
        try:
            self.logger.info(f"Starting {self.config.source_name} scraper "
                           f"(cumulative={self.use_cumulative})")
            
            # Fetch content (will check state for incremental)
            items = self.fetch_content()
            
            if not items:
                self.logger.info("No new content found")
                return None
            
            self.logger.info(f"Fetched {len(items)} items")
            
            # Save content (cumulative or traditional)
            filepath = self.save_content(items)
            
            # Update state for next incremental run
            if items and filepath:
                self.update_state(items)
                
                # Log statistics if cumulative
                if self.use_cumulative:
                    stats = self.cumulative_manager.get_statistics(filepath)
                    self.logger.info(f"Cumulative stats: {stats}")
            
            return filepath
            
        except Exception as e:
            self.logger.error(f"Error in scraper run: {e}")
            raise
    
    def get_cumulative_stats(self) -> Dict[str, int]:
        """Get statistics about the cumulative file."""
        if not self.use_cumulative:
            return {}
        
        return self.cumulative_manager.get_statistics()