"""
Blog topic analyzer for extracting technical topics and themes from competitive content.

This module analyzes social media content to identify blog-worthy technical topics,
with emphasis on HVACRSchool blog content as the primary data source.
"""

import re
import logging
from pathlib import Path
from typing import Dict, List, Set, Tuple, Optional
from collections import Counter, defaultdict
from dataclasses import dataclass
import json

logger = logging.getLogger(__name__)

@dataclass
class TopicAnalysis:
    """Results of topic analysis from competitive content."""
    primary_topics: Dict[str, int]  # Main technical topics with frequency
    secondary_topics: Dict[str, int]  # Supporting topics
    keyword_clusters: Dict[str, List[str]]  # Related keywords grouped by theme
    technical_depth_scores: Dict[str, float]  # Topic complexity scores
    content_gaps: List[str]  # Identified content opportunities
    hvacr_school_priority_topics: Dict[str, int]  # HVACRSchool emphasis analysis

class BlogTopicAnalyzer:
    """
    Analyzes competitive content to identify blog topic opportunities.
    
    Focuses on technical depth analysis with HVACRSchool blog content as primary
    data source and social media content as supplemental validation data.
    """
    
    def __init__(self, competitive_data_dir: Path):
        self.competitive_data_dir = Path(competitive_data_dir)
        self.hvacr_school_weight = 3.0  # Weight HVACRSchool content 3x higher
        self.social_weight = 1.0
        
        # Technical keyword categories for HVAC blog content
        self.technical_keywords = {
            'refrigeration': ['refrigerant', 'compressor', 'evaporator', 'condenser', 'txv', 'expansion', 'superheat', 'subcooling', 'manifold'],
            'electrical': ['electrical', 'voltage', 'amperage', 'capacitor', 'contactor', 'relay', 'transformer', 'wiring', 'multimeter'],
            'troubleshooting': ['troubleshoot', 'diagnostic', 'problem', 'issue', 'repair', 'fix', 'maintenance', 'service', 'fault'],
            'installation': ['install', 'setup', 'commissioning', 'startup', 'ductwork', 'piping', 'mounting', 'connection'],
            'systems': ['heat pump', 'furnace', 'boiler', 'chiller', 'vrf', 'vav', 'split system', 'package unit'],
            'controls': ['thermostat', 'control', 'automation', 'sensor', 'programming', 'sequence', 'logic', 'bms'],
            'efficiency': ['efficiency', 'energy', 'seer', 'eer', 'cop', 'performance', 'optimization', 'savings'],
            'codes_standards': ['code', 'standard', 'regulation', 'compliance', 'ashrae', 'nec', 'imc', 'certification']
        }
        
        # Blog-worthy topic indicators
        self.blog_indicators = [
            'how to', 'guide', 'tutorial', 'step by step', 'best practices',
            'common mistakes', 'troubleshooting guide', 'installation guide',
            'code requirements', 'safety', 'efficiency tips', 'maintenance schedule'
        ]
        
    def analyze_competitive_content(self) -> TopicAnalysis:
        """
        Analyze all competitive content to identify blog topic opportunities.
        
        Returns:
            TopicAnalysis with comprehensive topic opportunity data
        """
        logger.info("Starting comprehensive blog topic analysis...")
        
        # Load and analyze HVACRSchool blog content (primary data)
        hvacr_topics = self._analyze_hvacr_school_content()
        
        # Load and analyze social media content (supplemental data)  
        social_topics = self._analyze_social_media_content()
        
        # Combine and weight the results
        combined_analysis = self._combine_topic_analyses(hvacr_topics, social_topics)
        
        # Identify content gaps and opportunities
        content_gaps = self._identify_content_gaps(combined_analysis)
        
        # Calculate technical depth scores
        depth_scores = self._calculate_technical_depth_scores(combined_analysis)
        
        # Create keyword clusters
        keyword_clusters = self._create_keyword_clusters(combined_analysis)
        
        result = TopicAnalysis(
            primary_topics=combined_analysis['primary'],
            secondary_topics=combined_analysis['secondary'],
            keyword_clusters=keyword_clusters,
            technical_depth_scores=depth_scores,
            content_gaps=content_gaps,
            hvacr_school_priority_topics=hvacr_topics.get('primary', {})
        )
        
        logger.info(f"Blog topic analysis complete. Found {len(result.primary_topics)} primary topics")
        return result
        
    def _analyze_hvacr_school_content(self) -> Dict:
        """Analyze HVACRSchool blog content as primary data source."""
        logger.info("Analyzing HVACRSchool blog content (primary data source)...")
        
        # Look for HVACRSchool content in both blog and YouTube directories
        hvacr_files = []
        for pattern in ["hvacrschool/backlog/*.md", "hvacrschool_youtube/backlog/*.md"]:
            hvacr_files.extend(self.competitive_data_dir.glob(pattern))
        if not hvacr_files:
            logger.warning("No HVACRSchool content files found")
            return {'primary': {}, 'secondary': {}}
            
        topics = {'primary': Counter(), 'secondary': Counter()}
        
        for file_path in hvacr_files:
            try:
                content = file_path.read_text(encoding='utf-8')
                file_topics = self._extract_topics_from_content(content, is_blog_content=True)
                
                # Weight blog content higher
                for topic, count in file_topics['primary'].items():
                    topics['primary'][topic] += count * self.hvacr_school_weight
                for topic, count in file_topics['secondary'].items():
                    topics['secondary'][topic] += count * self.hvacr_school_weight
                    
            except Exception as e:
                logger.warning(f"Error analyzing {file_path}: {e}")
                
        return {
            'primary': dict(topics['primary'].most_common(50)),
            'secondary': dict(topics['secondary'].most_common(100))
        }
        
    def _analyze_social_media_content(self) -> Dict:
        """Analyze social media content as supplemental data."""
        logger.info("Analyzing social media content (supplemental data)...")
        
        # Get all competitive intelligence files except HVACRSchool
        social_files = []
        for competitor_dir in self.competitive_data_dir.glob("*"):
            if competitor_dir.is_dir() and 'hvacrschool' not in competitor_dir.name.lower():
                social_files.extend(competitor_dir.glob("*/backlog/*.md"))
                
        topics = {'primary': Counter(), 'secondary': Counter()}
        
        for file_path in social_files:
            try:
                content = file_path.read_text(encoding='utf-8')
                file_topics = self._extract_topics_from_content(content, is_blog_content=False)
                
                # Apply social media weight
                for topic, count in file_topics['primary'].items():
                    topics['primary'][topic] += count * self.social_weight
                for topic, count in file_topics['secondary'].items():
                    topics['secondary'][topic] += count * self.social_weight
                    
            except Exception as e:
                logger.warning(f"Error analyzing {file_path}: {e}")
                
        return {
            'primary': dict(topics['primary'].most_common(100)),
            'secondary': dict(topics['secondary'].most_common(200))
        }
        
    def _extract_topics_from_content(self, content: str, is_blog_content: bool = False) -> Dict:
        """Extract technical topics from content with blog-focus scoring."""
        primary_topics = Counter()
        secondary_topics = Counter()
        
        # Extract titles and descriptions
        titles = re.findall(r'## Title: (.+)', content)
        descriptions = re.findall(r'\*\*Description:\*\* (.+?)(?=\n\n|\*\*)', content, re.DOTALL)
        
        # Combine all text content
        all_text = ' '.join(titles + descriptions).lower()
        
        # Score topics based on technical keyword presence
        for category, keywords in self.technical_keywords.items():
            category_score = 0
            for keyword in keywords:
                # Count keyword occurrences
                count = len(re.findall(r'\b' + re.escape(keyword) + r'\b', all_text))
                category_score += count
                
                # Bonus for blog-worthy indicators
                for indicator in self.blog_indicators:
                    if indicator in all_text and keyword in all_text:
                        category_score += 2 if is_blog_content else 1
                        
            if category_score > 0:
                if category_score >= 5:  # High relevance threshold
                    primary_topics[category] += category_score
                else:
                    secondary_topics[category] += category_score
                    
        # Extract specific technical terms that appear frequently
        technical_terms = re.findall(r'\b(?:hvac|refrigeration|compressor|heat pump|thermostat|ductwork|refrigerant|installation|maintenance|troubleshooting|diagnostic|efficiency|control|sensor|valve|motor|fan|coil|filter|cleaning|repair|service|commissioning|startup|safety|code|standard|regulation|ashrae|seer|eer|cop)\b', all_text)
        
        for term in technical_terms:
            if term not in [kw for kws in self.technical_keywords.values() for kw in kws]:
                secondary_topics[f"specific_{term}"] += 1
                
        return {
            'primary': dict(primary_topics),
            'secondary': dict(secondary_topics)
        }
        
    def _combine_topic_analyses(self, hvacr_topics: Dict, social_topics: Dict) -> Dict:
        """Combine HVACRSchool and social media topic analyses with proper weighting."""
        combined = {'primary': Counter(), 'secondary': Counter()}
        
        # Add HVACRSchool topics (already weighted)
        for topic, count in hvacr_topics['primary'].items():
            combined['primary'][topic] += count
        for topic, count in hvacr_topics['secondary'].items():
            combined['secondary'][topic] += count
            
        # Add social media topics (already weighted)
        for topic, count in social_topics['primary'].items():
            combined['primary'][topic] += count
        for topic, count in social_topics['secondary'].items():
            combined['secondary'][topic] += count
            
        return {
            'primary': dict(combined['primary'].most_common(30)),
            'secondary': dict(combined['secondary'].most_common(50))
        }
        
    def _identify_content_gaps(self, combined_analysis: Dict) -> List[str]:
        """Identify content gaps based on topic analysis."""
        gaps = []
        
        # Check for underrepresented but important technical areas
        important_areas = ['electrical', 'controls', 'codes_standards', 'efficiency']
        
        for area in important_areas:
            primary_score = combined_analysis['primary'].get(area, 0)
            secondary_score = combined_analysis['secondary'].get(area, 0)
            
            if primary_score < 10:  # Underrepresented in primary topics
                gaps.append(f"Advanced {area.replace('_', ' ')} content opportunity")
                
        # Look for specific topic combinations that are missing
        topic_combinations = [
            "Troubleshooting + Electrical Systems",
            "Installation + Code Compliance", 
            "Maintenance + Efficiency Optimization",
            "Controls + System Integration",
            "Refrigeration + Advanced Diagnostics"
        ]
        
        gaps.extend(topic_combinations)  # All are potential opportunities
        
        return gaps
        
    def _calculate_technical_depth_scores(self, combined_analysis: Dict) -> Dict[str, float]:
        """Calculate technical depth scores for topics."""
        depth_scores = {}
        
        for topic, count in combined_analysis['primary'].items():
            # Base score from frequency
            base_score = min(count / 100.0, 1.0)  # Normalize to 0-1
            
            # Bonus for technical complexity indicators
            complexity_bonus = 0.0
            if any(term in topic for term in ['advanced', 'diagnostic', 'troubleshooting', 'system']):
                complexity_bonus = 0.2
                
            depth_scores[topic] = min(base_score + complexity_bonus, 1.0)
            
        return depth_scores
        
    def _create_keyword_clusters(self, combined_analysis: Dict) -> Dict[str, List[str]]:
        """Create keyword clusters from topic analysis."""
        clusters = {}
        
        for category, keywords in self.technical_keywords.items():
            if category in combined_analysis['primary'] or category in combined_analysis['secondary']:
                # Include related keywords for this category
                clusters[category] = keywords.copy()
                
        return clusters
        
    def export_analysis(self, analysis: TopicAnalysis, output_path: Path):
        """Export topic analysis to JSON for further processing."""
        export_data = {
            'primary_topics': analysis.primary_topics,
            'secondary_topics': analysis.secondary_topics, 
            'keyword_clusters': analysis.keyword_clusters,
            'technical_depth_scores': analysis.technical_depth_scores,
            'content_gaps': analysis.content_gaps,
            'hvacr_school_priority_topics': analysis.hvacr_school_priority_topics,
            'analysis_metadata': {
                'hvacr_weight': self.hvacr_school_weight,
                'social_weight': self.social_weight,
                'total_primary_topics': len(analysis.primary_topics),
                'total_secondary_topics': len(analysis.secondary_topics)
            }
        }
        
        output_path.write_text(json.dumps(export_data, indent=2))
        logger.info(f"Topic analysis exported to {output_path}")