"""
Content Item Parser for HVAC Know It All

Extracts individual content items from aggregated markdown files for classification.
"""

import re
from pathlib import Path
from typing import List, Dict, Any, Optional
from dataclasses import dataclass
import logging

logger = logging.getLogger(__name__)


@dataclass
class ContentItem:
    """Represents a single content item extracted from markdown."""
    
    id: str
    source: str
    raw_content: str
    metadata: Dict[str, Any]
    
    @property
    def url(self) -> Optional[str]:
        """Extract URL from content."""
        # WordPress permalink
        if 'Permalink:' in self.raw_content:
            match = re.search(r'## Permalink: (.+)', self.raw_content)
            if match:
                return match.group(1).strip()
        
        # YouTube/Instagram/Podcast links  
        if '## Link:' in self.raw_content:
            match = re.search(r'## Link: (.+)', self.raw_content)
            if match:
                return match.group(1).strip()
                
        # Episode link for podcasts
        if '## Episode Link:' in self.raw_content:
            match = re.search(r'## Episode Link: (.+)', self.raw_content)
            if match:
                return match.group(1).strip()
                
        return None
    
    @property  
    def title(self) -> Optional[str]:
        """Extract title from content."""
        match = re.search(r'## Title: (.+)', self.raw_content)
        if match:
            return match.group(1).strip()
        
        # For content without explicit titles, generate from description or type
        if not match and self.content_type:
            description = self.description
            if description:
                # Use first sentence or first 50 chars of description as title
                first_sentence = description.split('.')[0].strip()
                if len(first_sentence) > 10 and len(first_sentence) <= 100:
                    return first_sentence
                elif len(description) > 50:
                    return description[:50].strip() + "..."
                else:
                    return description.strip()
            else:
                return f"{self.content_type.title()} - {self.id}"
        
        return None
    
    @property
    def author(self) -> Optional[str]:
        """Extract author from content."""
        match = re.search(r'## Author: (.+)', self.raw_content)
        return match.group(1).strip() if match else None
    
    @property
    def publish_date(self) -> Optional[str]:
        """Extract publish date from content."""
        # WordPress format
        match = re.search(r'## Publish Date: (.+)', self.raw_content)
        if match:
            return match.group(1).strip()
            
        # YouTube upload date
        match = re.search(r'## Upload Date: (.+)', self.raw_content)
        if match:
            return match.group(1).strip()
            
        return None
    
    @property
    def content_type(self) -> Optional[str]:
        """Extract content type from content."""
        match = re.search(r'## Type: (.+)', self.raw_content)
        return match.group(1).strip() if match else None
    
    @property
    def word_count(self) -> Optional[int]:
        """Extract or calculate word count."""
        # If explicit word count exists
        match = re.search(r'## Word Count: (\d+)', self.raw_content)
        if match:
            return int(match.group(1))
            
        # Calculate from description/content
        description_match = re.search(r'## Description:\s*(.+?)(?=\n## |\n--|\Z)', 
                                     self.raw_content, re.DOTALL)
        if description_match:
            content_text = description_match.group(1)
            # Remove markdown formatting and count words
            clean_text = re.sub(r'[#*`\[\]()]', '', content_text)
            return len(clean_text.split())
            
        return None
    
    @property
    def categories(self) -> List[str]:
        """Extract categories/tags."""
        categories = []
        
        # WordPress categories
        cat_match = re.search(r'## Categories: (.+)', self.raw_content)
        if cat_match:
            categories.extend([c.strip() for c in cat_match.group(1).split(',')])
        
        # WordPress tags
        tag_match = re.search(r'## Tags: (.+)', self.raw_content)
        if tag_match:
            categories.extend([t.strip() for t in tag_match.group(1).split(',')])
            
        # Instagram hashtags
        hashtag_match = re.search(r'## Hashtags: (.+)', self.raw_content)
        if hashtag_match:
            hashtags = [h.strip() for h in hashtag_match.group(1).split(',')]
            categories.extend(hashtags)
            
        return categories
    
    @property
    def description(self) -> Optional[str]:
        """Extract main content/description."""
        # Look for description section
        match = re.search(r'## Description:\s*(.+?)(?=\n## |\n--|\Z)', 
                         self.raw_content, re.DOTALL)
        if match:
            return match.group(1).strip()
            
        return None


class ContentParser:
    """Parses markdown files to extract individual content items."""
    
    def __init__(self):
        self.logger = logging.getLogger(f"{__name__}.{self.__class__.__name__}")
    
    def parse_markdown_file(self, file_path: Path) -> List[ContentItem]:
        """Parse a single markdown file and extract all content items."""
        try:
            content = file_path.read_text(encoding='utf-8')
            source = self._extract_source_from_filename(file_path.name)
            
            # Split into individual items using ID markers
            items = self._split_content_items(content)
            
            parsed_items = []
            for item_content in items:
                if self._is_valid_content_item(item_content):
                    item_id = self._extract_item_id(item_content)
                    if item_id:
                        content_item = ContentItem(
                            id=item_id,
                            source=source,
                            raw_content=item_content,
                            metadata=self._extract_metadata(item_content)
                        )
                        
                        # Skip Property.com links as requested
                        if content_item.url and 'property.com' not in content_item.url.lower():
                            parsed_items.append(content_item)
                        elif not content_item.url:
                            parsed_items.append(content_item)
            
            self.logger.info(f"Parsed {len(parsed_items)} items from {file_path.name}")
            return parsed_items
            
        except Exception as e:
            self.logger.error(f"Error parsing {file_path}: {e}")
            return []
    
    def _extract_source_from_filename(self, filename: str) -> str:
        """Extract source name from filename."""
        # Pattern: hkia_SOURCE_timestamp.md
        match = re.match(r'hkia_([a-z]+)_\d+_\d+\.md', filename)
        return match.group(1) if match else 'unknown'
    
    def _split_content_items(self, content: str) -> List[str]:
        """Split content into individual items using ID markers."""
        # Split by ID markers (# ID: xxx)
        items = re.split(r'\n# ID: ', content)
        
        # Add back the ID marker to items (except first which might be header)
        processed_items = []
        for i, item in enumerate(items):
            if i == 0:
                # First item might not have ID marker or might be file header
                if item.strip() and '# ID:' in item:
                    processed_items.append(item)
            else:
                processed_items.append(f"# ID: {item}")
                
        return processed_items
    
    def _is_valid_content_item(self, content: str) -> bool:
        """Check if content represents a valid item to process."""
        # Must have ID 
        has_id = re.search(r'# ID: (.+)', content) is not None
        has_content = len(content.strip()) > 100  # Minimum content threshold
        
        # Must have either title OR be from Instagram/social media (which may not have titles)
        has_title = re.search(r'## Title: (.+)', content) is not None
        has_type = re.search(r'## Type: (.+)', content) is not None
        has_description = re.search(r'## Description:', content) is not None
        
        # Valid if has ID and sufficient content AND either title or social media format
        return has_id and has_content and (has_title or (has_type and has_description))
    
    def _extract_item_id(self, content: str) -> Optional[str]:
        """Extract item ID from content."""
        match = re.search(r'# ID: (.+)', content)
        return match.group(1).strip() if match else None
    
    def _extract_metadata(self, content: str) -> Dict[str, Any]:
        """Extract additional metadata from content."""
        metadata = {}
        
        # Extract various metrics if available
        patterns = {
            'views': r'## Views: (.+)',
            'likes': r'## Likes: (.+)', 
            'comments': r'## Comments: (.+)',
            'duration': r'## Duration: (.+)',
            'engagement_rate': r'## Engagement Rate: (.+)',
            'mentions': r'## Mentions: (.+)',
        }
        
        for key, pattern in patterns.items():
            match = re.search(pattern, content)
            if match:
                value = match.group(1).strip()
                # Convert numeric values
                if key in ['views', 'likes', 'comments'] and value.isdigit():
                    metadata[key] = int(value)
                else:
                    metadata[key] = value
        
        return metadata

    def parse_multiple_files(self, file_paths: List[Path]) -> List[ContentItem]:
        """Parse multiple markdown files."""
        all_items = []
        
        for file_path in file_paths:
            items = self.parse_markdown_file(file_path)
            all_items.extend(items)
            
        self.logger.info(f"Total parsed items across {len(file_paths)} files: {len(all_items)}")
        return all_items


def main():
    """Test the content parser."""
    parser = ContentParser()
    
    # Test with current files
    data_dir = Path("data/markdown_current")
    markdown_files = list(data_dir.glob("hkia_*.md"))
    
    if markdown_files:
        items = parser.parse_multiple_files(markdown_files[:2])  # Test with first 2 files
        
        print(f"Parsed {len(items)} content items")
        for item in items[:3]:  # Show first 3
            print(f"\n--- {item.source} Item ---")
            print(f"ID: {item.id}")
            print(f"Title: {item.title}")
            print(f"URL: {item.url}")
            print(f"Author: {item.author}")
            print(f"Type: {item.content_type}")
            print(f"Word Count: {item.word_count}")
            print(f"Categories: {item.categories[:3]}")  # First 3 categories
            print(f"Description: {item.description[:100] if item.description else 'N/A'}...")


if __name__ == "__main__":
    main()