"""
Content gap analyzer for identifying blog content opportunities.

Compares competitive content topics against existing HVAC Know It All blog content
to identify strategic content gaps and positioning opportunities.
"""

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 ContentGap:
    """Represents a content gap opportunity."""
    topic: str
    competitive_strength: int  # How well competitors cover this topic (1-10)
    our_coverage: int  # How well we currently cover this topic (1-10) 
    opportunity_score: float  # Combined opportunity score
    suggested_approach: str  # Recommended content strategy
    supporting_keywords: List[str]  # Keywords to target
    competitor_examples: List[str]  # Examples from competitor analysis

@dataclass
class ContentGapAnalysis:
    """Results of content gap analysis."""
    high_opportunity_gaps: List[ContentGap]  # Score > 7.0
    medium_opportunity_gaps: List[ContentGap]  # Score 4.0-7.0  
    low_opportunity_gaps: List[ContentGap]  # Score < 4.0
    content_strengths: List[str]  # Areas where we already excel
    competitive_threats: List[str]  # Areas where competitors dominate

class ContentGapAnalyzer:
    """
    Analyzes content gaps between competitive content and existing HVAC Know It All content.
    
    Identifies strategic opportunities by comparing topic coverage, technical depth,
    and engagement patterns between competitive content and our existing blog.
    """
    
    def __init__(self, competitive_data_dir: Path, hkia_blog_dir: Path):
        self.competitive_data_dir = Path(competitive_data_dir)
        self.hkia_blog_dir = Path(hkia_blog_dir)
        
        # Gap analysis scoring weights
        self.weights = {
            'competitive_weakness': 0.4,  # Higher score if competitors are weak
            'our_weakness': 0.3,  # Higher score if we're currently weak  
            'market_demand': 0.2,  # Based on engagement/view data
            'technical_complexity': 0.1  # Bonus for advanced topics
        }
        
        # Content positioning strategies
        self.positioning_strategies = {
            'technical_authority': "Position as the definitive technical resource",
            'practical_guidance': "Focus on step-by-step practical implementation", 
            'advanced_professional': "Target experienced HVAC professionals",
            'comprehensive_coverage': "Provide more thorough coverage than competitors",
            'unique_angle': "Approach from a unique perspective not covered by others",
            'case_study_focus': "Use real-world case studies and examples"
        }
        
    def analyze_content_gaps(self, competitive_topics: Dict) -> ContentGapAnalysis:
        """
        Perform comprehensive content gap analysis.
        
        Args:
            competitive_topics: Topic analysis from BlogTopicAnalyzer
            
        Returns:
            ContentGapAnalysis with identified opportunities
        """
        logger.info("Starting content gap analysis...")
        
        # Analyze our existing content coverage
        our_coverage = self._analyze_hkia_content_coverage()
        
        # Analyze competitive content strength by topic
        competitive_strength = self._analyze_competitive_strength(competitive_topics)
        
        # Calculate market demand indicators
        market_demand = self._calculate_market_demand(competitive_topics)
        
        # Identify content gaps
        gaps = self._identify_content_gaps(
            our_coverage, 
            competitive_strength, 
            market_demand
        )
        
        # Categorize gaps by opportunity score
        high_gaps = [gap for gap in gaps if gap.opportunity_score > 7.0]
        medium_gaps = [gap for gap in gaps if 4.0 <= gap.opportunity_score <= 7.0]
        low_gaps = [gap for gap in gaps if gap.opportunity_score < 4.0]
        
        # Identify our content strengths
        strengths = self._identify_content_strengths(our_coverage, competitive_strength)
        
        # Identify competitive threats
        threats = self._identify_competitive_threats(our_coverage, competitive_strength)
        
        result = ContentGapAnalysis(
            high_opportunity_gaps=sorted(high_gaps, key=lambda x: x.opportunity_score, reverse=True),
            medium_opportunity_gaps=sorted(medium_gaps, key=lambda x: x.opportunity_score, reverse=True),
            low_opportunity_gaps=sorted(low_gaps, key=lambda x: x.opportunity_score, reverse=True),
            content_strengths=strengths,
            competitive_threats=threats
        )
        
        logger.info(f"Content gap analysis complete. Found {len(high_gaps)} high-opportunity gaps")
        return result
        
    def _analyze_hkia_content_coverage(self) -> Dict[str, int]:
        """Analyze existing HVAC Know It All blog content coverage by topic."""
        logger.info("Analyzing existing HKIA blog content coverage...")
        
        coverage = Counter()
        
        # Look for markdown files in various possible locations
        blog_patterns = [
            self.hkia_blog_dir / "*.md",
            Path("/mnt/nas/hvacknowitall/markdown_current") / "*.md",
            Path("data/markdown_current") / "*.md"
        ]
        
        blog_files = []
        for pattern in blog_patterns:
            if pattern.parent.exists():
                blog_files.extend(pattern.parent.glob(pattern.name))
                # Also check subdirectories
                for subdir in pattern.parent.iterdir():
                    if subdir.is_dir():
                        blog_files.extend(subdir.glob("*.md"))
                
        if not blog_files:
            logger.warning("No existing HKIA blog content found")
            return {}
            
        # Analyze content topics
        technical_categories = [
            'refrigeration', 'electrical', 'troubleshooting', 'installation', 
            'systems', 'controls', 'efficiency', 'codes_standards', 'maintenance',
            'heat_pump', 'furnace', 'air_conditioning', 'commercial', 'residential'
        ]
        
        for file_path in blog_files:
            try:
                content = file_path.read_text(encoding='utf-8').lower()
                
                for category in technical_categories:
                    # Count occurrences and weight by content depth
                    category_keywords = self._get_category_keywords(category)
                    category_score = 0
                    
                    for keyword in category_keywords:
                        matches = len(re.findall(r'\b' + re.escape(keyword) + r'\b', content))
                        category_score += matches
                        
                    if category_score > 0:
                        coverage[category] += min(category_score, 10)  # Cap per article
                        
            except Exception as e:
                logger.warning(f"Error analyzing HKIA content {file_path}: {e}")
                
        logger.info(f"Analyzed {len(blog_files)} HKIA blog files")
        return dict(coverage)
        
    def _analyze_competitive_strength(self, competitive_topics: Dict) -> Dict[str, int]:
        """Analyze how strongly competitors cover each topic."""
        strength = {}
        
        # Combine primary and secondary topics with weighting
        for topic, count in competitive_topics.get('primary_topics', {}).items():
            strength[topic] = min(count / 10, 10)  # Normalize to 1-10 scale
            
        for topic, count in competitive_topics.get('secondary_topics', {}).items():
            if topic not in strength:
                strength[topic] = min(count / 20, 5)  # Lower weight for secondary
            else:
                strength[topic] += min(count / 20, 3)
                
        return strength
        
    def _calculate_market_demand(self, competitive_topics: Dict) -> Dict[str, float]:
        """Calculate market demand indicators based on engagement data."""
        # For now, use topic frequency as demand proxy
        # In future iterations, incorporate actual engagement metrics
        demand = {}
        
        total_mentions = sum(competitive_topics.get('primary_topics', {}).values())
        if total_mentions == 0:
            return {}
            
        for topic, count in competitive_topics.get('primary_topics', {}).items():
            demand[topic] = count / total_mentions * 10  # Normalize to 0-10
            
        return demand
        
    def _identify_content_gaps(self, our_coverage: Dict, competitive_strength: Dict, market_demand: Dict) -> List[ContentGap]:
        """Identify specific content gaps with scoring."""
        gaps = []
        
        # Get all topics from competitive analysis
        all_topics = set(competitive_strength.keys()) | set(market_demand.keys())
        
        for topic in all_topics:
            our_score = our_coverage.get(topic, 0)
            comp_score = competitive_strength.get(topic, 0) 
            demand_score = market_demand.get(topic, 0)
            
            # Calculate opportunity score
            competitive_weakness = max(0, 10 - comp_score)  # Higher if competitors are weak
            our_weakness = max(0, 10 - our_score)  # Higher if we're weak
            technical_complexity = self._get_technical_complexity_bonus(topic)
            
            opportunity_score = (
                competitive_weakness * self.weights['competitive_weakness'] +
                our_weakness * self.weights['our_weakness'] +
                demand_score * self.weights['market_demand'] +  
                technical_complexity * self.weights['technical_complexity']
            )
            
            # Only include significant opportunities
            if opportunity_score > 2.0:
                gap = ContentGap(
                    topic=topic,
                    competitive_strength=int(comp_score),
                    our_coverage=int(our_score),
                    opportunity_score=opportunity_score,
                    suggested_approach=self._suggest_content_approach(topic, our_score, comp_score),
                    supporting_keywords=self._get_category_keywords(topic),
                    competitor_examples=[]  # Would be populated with actual examples
                )
                gaps.append(gap)
                
        return gaps
        
    def _identify_content_strengths(self, our_coverage: Dict, competitive_strength: Dict) -> List[str]:
        """Identify areas where we already excel."""
        strengths = []
        
        for topic, our_score in our_coverage.items():
            comp_score = competitive_strength.get(topic, 0)
            if our_score > comp_score + 3:  # We're significantly stronger
                strengths.append(f"{topic.replace('_', ' ').title()}: Strong advantage over competitors")
                
        return strengths
        
    def _identify_competitive_threats(self, our_coverage: Dict, competitive_strength: Dict) -> List[str]:
        """Identify areas where competitors dominate."""
        threats = []
        
        for topic, comp_score in competitive_strength.items():
            our_score = our_coverage.get(topic, 0)
            if comp_score > our_score + 5:  # Competitors significantly stronger
                threats.append(f"{topic.replace('_', ' ').title()}: Competitors have strong advantage")
                
        return threats
        
    def _suggest_content_approach(self, topic: str, our_score: int, comp_score: int) -> str:
        """Suggest content strategy approach based on competitive landscape."""
        
        if our_score < 3 and comp_score < 5:
            return self.positioning_strategies['technical_authority']
        elif our_score < 3 and comp_score >= 5:
            return self.positioning_strategies['unique_angle']
        elif our_score >= 3 and comp_score < 5:
            return self.positioning_strategies['comprehensive_coverage']
        else:
            return self.positioning_strategies['advanced_professional']
            
    def _get_technical_complexity_bonus(self, topic: str) -> float:
        """Get technical complexity bonus for advanced topics."""
        advanced_indicators = [
            'troubleshooting', 'diagnostic', 'advanced', 'system', 'control',
            'electrical', 'refrigeration', 'commercial', 'codes_standards'
        ]
        
        bonus = 0.0
        for indicator in advanced_indicators:
            if indicator in topic.lower():
                bonus += 1.0
                
        return min(bonus, 3.0)  # Cap at 3.0
        
    def _get_category_keywords(self, category: str) -> List[str]:
        """Get keywords for a specific category."""
        keyword_map = {
            'refrigeration': ['refrigerant', 'compressor', 'evaporator', 'condenser', 'superheat', 'subcooling'],
            'electrical': ['electrical', 'voltage', 'amperage', 'capacitor', 'contactor', 'relay', 'wiring'],
            'troubleshooting': ['troubleshoot', 'diagnostic', 'problem', 'repair', 'maintenance', 'service'],
            'installation': ['install', 'setup', 'commissioning', 'startup', 'ductwork', 'piping'],
            'systems': ['heat pump', 'furnace', 'boiler', 'chiller', 'split system', 'package unit'],
            'controls': ['thermostat', 'control', 'automation', 'sensor', 'programming', 'bms'],
            'efficiency': ['efficiency', 'energy', 'seer', 'eer', 'cop', 'performance', 'optimization'],
            'codes_standards': ['code', 'standard', 'regulation', 'compliance', 'ashrae', 'nec', 'imc']
        }
        
        return keyword_map.get(category, [category])
        
    def export_gap_analysis(self, analysis: ContentGapAnalysis, output_path: Path):
        """Export content gap analysis to JSON."""
        export_data = {
            'high_opportunity_gaps': [
                {
                    'topic': gap.topic,
                    'competitive_strength': gap.competitive_strength,
                    'our_coverage': gap.our_coverage,
                    'opportunity_score': gap.opportunity_score,
                    'suggested_approach': gap.suggested_approach,
                    'supporting_keywords': gap.supporting_keywords
                }
                for gap in analysis.high_opportunity_gaps
            ],
            'medium_opportunity_gaps': [
                {
                    'topic': gap.topic,
                    'competitive_strength': gap.competitive_strength,
                    'our_coverage': gap.our_coverage,
                    'opportunity_score': gap.opportunity_score,
                    'suggested_approach': gap.suggested_approach,
                    'supporting_keywords': gap.supporting_keywords
                }
                for gap in analysis.medium_opportunity_gaps
            ],
            'content_strengths': analysis.content_strengths,
            'competitive_threats': analysis.competitive_threats,
            'analysis_summary': {
                'total_high_opportunities': len(analysis.high_opportunity_gaps),
                'total_medium_opportunities': len(analysis.medium_opportunity_gaps),
                'total_strengths': len(analysis.content_strengths),
                'total_threats': len(analysis.competitive_threats)
            }
        }
        
        output_path.write_text(json.dumps(export_data, indent=2))
        logger.info(f"Content gap analysis exported to {output_path}")