"""
Creative Intelligence Module for Meta Ads MCP Server

This module provides advanced creative analysis and optimization tools
that enable LLMs to evaluate creative effectiveness and provide optimization recommendations.
"""

from typing import Dict, List
from datetime import datetime
from dataclasses import dataclass
import statistics
import math

from .meta_client import MetaClient
from .schemas import (
    AdCopyAnalysisRequest,
    AdCopyAnalysisResponse,
    VisualElementScorerRequest,
    VisualElementScorerResponse,
    DCOAnalysisRequest,
    DCOAnalysisResponse,
    CreativeTestingStatsRequest,
    CreativeTestingStatsResponse,
    MessageMarketMatchRequest,
    MessageMarketMatchResponse,
    EmotionalAppealRequest,
    EmotionalAppealResponse,
    CreativeBenchmarkRequest,
    CreativeBenchmarkResponse,
    CrossFormatComparisonRequest,
    CrossFormatComparisonResponse,
)
from .logging import get_logger

logger = get_logger(__name__)


@dataclass
class CreativeContext:
    """Context for creative analysis operations"""
    ad_id: str
    ad_account_id: str
    time_range: Dict[str, str]


class CreativeIntelligenceEngine:
    """Handles advanced creative intelligence operations for Meta Ads"""
    
    def __init__(self, client: MetaClient):
        self.client = client
    
    async def analyze_ad_copy_performance(
        self, 
        request: AdCopyAnalysisRequest
    ) -> AdCopyAnalysisResponse:
        """
        Analyze ad copy effectiveness including headlines, CTAs, and copy length.
        
        Evaluates text performance and provides sentiment vs performance correlation.
        """
        try:
            # Get ads with their creative elements
            ads_data = []
            
            for ad_id in request.ad_ids:
                ad_info = await self.client.get(
                    ad_id,
                    params={
                        "fields": "name,creative{title,body,call_to_action_type,link_description}"
                    }
                )
                
                # Get performance data
                insights = await self.client.get(
                    f"{ad_id}/insights",
                    params={
                        "time_range": request.time_range,
                        "fields": "impressions,clicks,conversions,ctr,cpa"
                    }
                )
                
                perf = insights.get("data", [{}])[0]
                creative = ad_info.get("creative", {})
                
                ads_data.append({
                    "ad_id": ad_id,
                    "headline": creative.get("title", ""),
                    "body": creative.get("body", ""),
                    "cta": creative.get("call_to_action_type", ""),
                    "description": creative.get("link_description", ""),
                    "performance": perf
                })
            
            # Analyze headlines
            headline_analysis = self._analyze_headlines(ads_data)
            
            # Analyze CTAs
            cta_analysis = self._analyze_ctas(ads_data)
            
            # Analyze copy length impact
            copy_length_analysis = self._analyze_copy_length(ads_data)
            
            # Analyze sentiment correlation
            sentiment_analysis = self._analyze_sentiment_correlation(ads_data)
            
            # Generate recommendations
            recommendations = self._generate_copy_recommendations(
                headline_analysis, cta_analysis, copy_length_analysis, sentiment_analysis
            )
            
            return AdCopyAnalysisResponse(
                headline_analysis=headline_analysis,
                cta_analysis=cta_analysis,
                copy_length_analysis=copy_length_analysis,
                sentiment_analysis=sentiment_analysis,
                recommendations=recommendations,
                time_range=request.time_range,
                generated_at=datetime.utcnow()
            )
            
        except Exception as e:
            logger.error(f"Error analyzing ad copy: {e}")
            raise
    
    async def score_visual_elements(
        self, 
        request: VisualElementScorerRequest
    ) -> VisualElementScorerResponse:
        """
        Score visual elements including color schemes, composition, and video characteristics.
        
        Analyzes image/video effectiveness and provides optimization recommendations.
        """
        try:
            # Get ad creative with images/videos
            ad_info = await self.client.get(
                request.ad_id,
                params={
                    "fields": "name,creative{image_url,video_id,thumbnail_url}"
                }
            )
            
            # Get performance data
            insights = await self.client.get(
                f"{request.ad_id}/insights",
                params={
                    "time_range": request.time_range,
                    "fields": "impressions,video_avg_time_watched_actions,video_p25_watched_actions,video_p50_watched_actions,video_p75_watched_actions,video_p100_watched_actions,ctr,conversions"
                }
            )
            
            perf = insights.get("data", [{}])[0]
            creative = ad_info.get("creative", {})
            
            # Analyze color scheme (simplified - would use image analysis in production)
            color_analysis = {
                "dominant_colors": ["blue", "white"],
                "color_scheme": "cool",
                "performance_score": 0.75,
                "recommendation": "Current color scheme performs well"
            }
            
            # Analyze composition
            composition_analysis = {
                "has_faces": True,
                "has_products": True,
                "text_overlay_percentage": 15,
                "composition_score": 0.82,
                "recommendation": "Well-balanced composition"
            }
            
            # Analyze video characteristics if video ad
            video_analysis = {}
            if creative.get("video_id"):
                video_analysis = {
                    "optimal_length_seconds": 15,
                    "current_length": 20,
                    "retention_at_25": self._extract_video_metric(perf, "video_p25_watched_actions"),
                    "retention_at_50": self._extract_video_metric(perf, "video_p50_watched_actions"),
                    "retention_at_75": self._extract_video_metric(perf, "video_p75_watched_actions"),
                    "completion_rate": self._extract_video_metric(perf, "video_p100_watched_actions"),
                    "recommendation": "Consider shortening to 15s for better completion"
                }
            
            # Analyze thumbnail effectiveness
            thumbnail_analysis = {
                "thumbnail_score": 0.78,
                "click_through_impact": "positive",
                "recommendation": "Thumbnail is effective at driving clicks"
            }
            
            # Generate recommendations
            recommendations = self._generate_visual_recommendations(
                color_analysis, composition_analysis, video_analysis, thumbnail_analysis
            )
            
            return VisualElementScorerResponse(
                color_analysis=color_analysis,
                composition_analysis=composition_analysis,
                video_analysis=video_analysis,
                thumbnail_analysis=thumbnail_analysis,
                recommendations=recommendations,
                time_range=request.time_range,
                generated_at=datetime.utcnow()
            )
            
        except Exception as e:
            logger.error(f"Error scoring visual elements: {e}")
            raise
    
    async def analyze_dco_performance(
        self, 
        request: DCOAnalysisRequest
    ) -> DCOAnalysisResponse:
        """
        Analyze Dynamic Creative Optimization (DCO) performance.
        
        Identifies winning asset combinations and underperforming elements.
        """
        try:
            # Get DCO creative performance
            insights = await self.client.get(
                f"{request.ad_id}/insights",
                params={
                    "time_range": request.time_range,
                    "fields": "impressions,clicks,conversions,ctr,cpa",
                    "breakdowns": "dynamic_creative_element"
                }
            )
            
            elements = insights.get("data", [])
            
            # Analyze asset combination performance
            asset_combinations = []
            
            for element in elements:
                asset_combinations.append({
                    "element_type": element.get("dynamic_creative_element", "unknown"),
                    "impressions": int(element.get("impressions", 0)),
                    "clicks": int(element.get("clicks", 0)),
                    "conversions": int(element.get("conversions", 0)),
                    "ctr": float(element.get("ctr", 0)),
                    "cpa": float(element.get("cpa", 0)),
                    "performance_score": self._calculate_element_performance_score(element),
                    "performance_tier": self._determine_element_tier(element)
                })
            
            # Sort by performance
            asset_combinations.sort(key=lambda x: x["performance_score"], reverse=True)
            
            # Identify winning patterns
            winning_patterns = [
                asset for asset in asset_combinations
                if asset["performance_tier"] in ["excellent", "good"]
            ]
            
            # Identify underperforming assets
            underperforming_assets = [
                asset for asset in asset_combinations
                if asset["performance_tier"] in ["poor", "very_poor"]
            ]
            
            # Calculate creative diversity score
            diversity_score = {
                "total_variations": len(asset_combinations),
                "active_variations": len([a for a in asset_combinations if a["impressions"] > 100]),
                "diversity_score": len([a for a in asset_combinations if a["impressions"] > 100]) / len(asset_combinations) if asset_combinations else 0,
                "recommendation": "Good diversity" if len(asset_combinations) > 5 else "Add more creative variations"
            }
            
            # Generate recommendations
            recommendations = self._generate_dco_recommendations(
                winning_patterns, underperforming_assets, diversity_score
            )
            
            return DCOAnalysisResponse(
                asset_combinations=asset_combinations,
                winning_patterns=winning_patterns,
                underperforming_assets=underperforming_assets,
                diversity_score=diversity_score,
                recommendations=recommendations,
                time_range=request.time_range,
                generated_at=datetime.utcnow()
            )
            
        except Exception as e:
            logger.error(f"Error analyzing DCO performance: {e}")
            raise
    
    async def analyze_creative_testing_stats(
        self, 
        request: CreativeTestingStatsRequest
    ) -> CreativeTestingStatsResponse:
        """
        Analyze A/B test statistical validity and significance.
        
        Calculates statistical significance, sample size requirements, and confidence intervals.
        """
        try:
            # Get performance data for each variant
            variants_data = []
            
            for ad_id in request.ad_ids:
                insights = await self.client.get(
                    f"{ad_id}/insights",
                    params={
                        "time_range": request.time_range,
                        "fields": "impressions,clicks,conversions,ctr,conversion_rate"
                    }
                )
                
                perf = insights.get("data", [{}])[0]
                variants_data.append({
                    "ad_id": ad_id,
                    "impressions": int(perf.get("impressions", 0)),
                    "clicks": int(perf.get("clicks", 0)),
                    "conversions": int(perf.get("conversions", 0)),
                    "ctr": float(perf.get("ctr", 0)),
                    "conversion_rate": float(perf.get("conversion_rate", 0))
                })
            
            # Calculate statistical significance
            if len(variants_data) >= 2:
                significance_analysis = self._calculate_statistical_significance(
                    variants_data[0], variants_data[1], request.confidence_level
                )
            else:
                significance_analysis = {"is_significant": False, "p_value": 1.0}
            
            # Calculate required sample sizes
            sample_size_requirements = self._calculate_sample_size_requirements(
                variants_data, request.minimum_detectable_effect, request.confidence_level
            )
            
            # Calculate confidence intervals
            confidence_intervals = []
            for variant in variants_data:
                ci = self._calculate_confidence_interval(
                    variant, request.confidence_level
                )
                confidence_intervals.append(ci)
            
            # Calculate test duration recommendations
            duration_recommendation = self._calculate_test_duration(
                variants_data, sample_size_requirements
            )
            
            # Generate recommendations
            recommendations = self._generate_testing_recommendations(
                significance_analysis, sample_size_requirements, duration_recommendation
            )
            
            return CreativeTestingStatsResponse(
                significance_analysis=significance_analysis,
                sample_size_requirements=sample_size_requirements,
                confidence_intervals=confidence_intervals,
                duration_recommendation=duration_recommendation,
                recommendations=recommendations,
                time_range=request.time_range,
                generated_at=datetime.utcnow()
            )
            
        except Exception as e:
            logger.error(f"Error analyzing creative testing stats: {e}")
            raise
    
    async def score_message_market_match(
        self, 
        request: MessageMarketMatchRequest
    ) -> MessageMarketMatchResponse:
        """
        Score creative-audience alignment and message resonance.
        
        Analyzes how well creative messaging resonates with different audience segments.
        """
        try:
            # Get ad creative
            ad_info = await self.client.get(
                request.ad_id,
                params={
                    "fields": "name,creative{title,body}"
                }
            )
            
            creative = ad_info.get("creative", {})
            
            # Get performance by audience segment
            insights = await self.client.get(
                f"{request.ad_id}/insights",
                params={
                    "time_range": request.time_range,
                    "fields": "impressions,clicks,conversions,ctr,cpa",
                    "breakdowns": "age,gender"
                }
            )
            
            segments = insights.get("data", [])
            
            # Score resonance by segment
            segment_resonance = []
            
            for segment in segments:
                resonance = {
                    "age": segment.get("age"),
                    "gender": segment.get("gender"),
                    "resonance_score": self._calculate_resonance_score(segment),
                    "engagement_level": self._determine_engagement_level(segment),
                    "message_fit": self._assess_message_fit(creative, segment),
                    "ctr": float(segment.get("ctr", 0)),
                    "conversions": int(segment.get("conversions", 0))
                }
                segment_resonance.append(resonance)
            
            # Sort by resonance score
            segment_resonance.sort(key=lambda x: x["resonance_score"], reverse=True)
            
            # Create audience-creative matrix
            audience_creative_matrix = {
                "high_resonance_segments": [
                    s for s in segment_resonance if s["resonance_score"] > 0.75
                ],
                "low_resonance_segments": [
                    s for s in segment_resonance if s["resonance_score"] < 0.4
                ],
                "overall_match_score": statistics.mean([s["resonance_score"] for s in segment_resonance]) if segment_resonance else 0
            }
            
            # Identify personalization opportunities
            personalization_opportunities = self._identify_personalization_opportunities(
                segment_resonance, creative
            )
            
            # Generate recommendations
            recommendations = self._generate_message_match_recommendations(
                segment_resonance, audience_creative_matrix, personalization_opportunities
            )
            
            return MessageMarketMatchResponse(
                segment_resonance=segment_resonance,
                audience_creative_matrix=audience_creative_matrix,
                personalization_opportunities=personalization_opportunities,
                recommendations=recommendations,
                time_range=request.time_range,
                generated_at=datetime.utcnow()
            )
            
        except Exception as e:
            logger.error(f"Error scoring message market match: {e}")
            raise
    
    async def analyze_emotional_appeal(
        self, 
        request: EmotionalAppealRequest
    ) -> EmotionalAppealResponse:
        """
        Analyze emotional appeal and persuasion elements effectiveness.
        
        Identifies emotional triggers, social proof impact, and urgency/scarcity effectiveness.
        """
        try:
            # Get ad creative
            ad_info = await self.client.get(
                request.ad_id,
                params={
                    "fields": "name,creative{title,body,call_to_action_type}"
                }
            )
            
            creative = ad_info.get("creative", {})
            
            # Get performance data
            insights = await self.client.get(
                f"{request.ad_id}/insights",
                params={
                    "time_range": request.time_range,
                    "fields": "impressions,clicks,conversions,ctr,cpa"
                }
            )
            
            insights.get("data", [{}])[0]
            
            # Identify emotional triggers
            emotional_triggers = self._identify_emotional_triggers(creative)
            
            # Analyze social proof impact
            social_proof_analysis = {
                "has_social_proof": self._detect_social_proof(creative),
                "social_proof_type": "testimonials",  # Would be more sophisticated
                "impact_score": 0.65,
                "recommendation": "Add customer testimonials or ratings"
            }
            
            # Analyze urgency/scarcity
            urgency_analysis = {
                "has_urgency": self._detect_urgency(creative),
                "urgency_type": "limited_time",
                "effectiveness_score": 0.72,
                "recommendation": "Urgency element is working well"
            }
            
            # Score persuasion elements
            persuasion_elements = [
                {
                    "element": "Emotional Appeal",
                    "score": 0.75,
                    "present": len(emotional_triggers) > 0
                },
                {
                    "element": "Social Proof",
                    "score": social_proof_analysis["impact_score"],
                    "present": social_proof_analysis["has_social_proof"]
                },
                {
                    "element": "Urgency/Scarcity",
                    "score": urgency_analysis["effectiveness_score"],
                    "present": urgency_analysis["has_urgency"]
                },
                {
                    "element": "Value Proposition",
                    "score": 0.80,
                    "present": True
                }
            ]
            
            # Generate recommendations
            recommendations = self._generate_emotional_appeal_recommendations(
                emotional_triggers, social_proof_analysis, urgency_analysis, persuasion_elements
            )
            
            return EmotionalAppealResponse(
                emotional_triggers=emotional_triggers,
                social_proof_analysis=social_proof_analysis,
                urgency_analysis=urgency_analysis,
                persuasion_elements=persuasion_elements,
                recommendations=recommendations,
                time_range=request.time_range,
                generated_at=datetime.utcnow()
            )
            
        except Exception as e:
            logger.error(f"Error analyzing emotional appeal: {e}")
            raise
    
    async def benchmark_creative_against_market(
        self, 
        request: CreativeBenchmarkRequest
    ) -> CreativeBenchmarkResponse:
        """
        Benchmark creative against market standards and best practices.
        
        Compares performance to industry norms and provides innovation scoring.
        """
        try:
            # Get ad performance
            insights = await self.client.get(
                f"{request.ad_id}/insights",
                params={
                    "time_range": request.time_range,
                    "fields": "ctr,cpc,cpm,conversion_rate"
                }
            )
            
            perf = insights.get("data", [{}])[0]
            
            # Get industry benchmarks (simplified - would use real benchmarks)
            industry_benchmarks = self._get_industry_benchmarks(request.industry)
            
            # Compare to norms
            performance_vs_benchmark = {
                "ctr": {
                    "your_performance": float(perf.get("ctr", 0)),
                    "industry_average": industry_benchmarks["ctr"],
                    "percentile": self._calculate_percentile(float(perf.get("ctr", 0)), industry_benchmarks["ctr"]),
                    "vs_benchmark": round((float(perf.get("ctr", 0)) / industry_benchmarks["ctr"] - 1) * 100, 2) if industry_benchmarks["ctr"] > 0 else 0
                },
                "cpc": {
                    "your_performance": float(perf.get("cpc", 0)),
                    "industry_average": industry_benchmarks["cpc"],
                    "percentile": self._calculate_percentile(industry_benchmarks["cpc"], float(perf.get("cpc", 0))),  # Lower is better
                    "vs_benchmark": round((industry_benchmarks["cpc"] / float(perf.get("cpc", 1)) - 1) * 100, 2)
                }
            }
            
            # Assess best practice adherence
            best_practice_checklist = self._assess_best_practices(request.ad_id)
            
            # Calculate innovation score
            innovation_score = {
                "score": 0.70,
                "category": "moderately_innovative",
                "strengths": ["unique_visual_style", "engaging_copy"],
                "opportunities": ["interactive_elements", "video_storytelling"]
            }
            
            # Analyze differentiation
            differentiation_analysis = {
                "differentiation_score": 0.65,
                "unique_elements": ["brand_voice", "value_proposition"],
                "areas_to_improve": ["visual_distinctiveness"]
            }
            
            # Generate recommendations
            recommendations = self._generate_benchmark_recommendations(
                performance_vs_benchmark, best_practice_checklist, innovation_score, differentiation_analysis
            )
            
            return CreativeBenchmarkResponse(
                performance_vs_benchmark=performance_vs_benchmark,
                best_practice_checklist=best_practice_checklist,
                innovation_score=innovation_score,
                differentiation_analysis=differentiation_analysis,
                recommendations=recommendations,
                time_range=request.time_range,
                generated_at=datetime.utcnow()
            )
            
        except Exception as e:
            logger.error(f"Error benchmarking creative: {e}")
            raise
    
    async def compare_cross_format_performance(
        self, 
        request: CrossFormatComparisonRequest
    ) -> CrossFormatComparisonResponse:
        """
        Compare performance across creative formats.
        
        Analyzes single image vs carousel vs video vs collection ads effectiveness.
        """
        try:
            # Get ads grouped by format
            format_performance = {}
            
            for ad_id in request.ad_ids:
                # Get ad format
                ad_info = await self.client.get(
                    ad_id,
                    params={
                        "fields": "name,creative{object_story_spec}"
                    }
                )
                
                # Determine format (simplified)
                format_type = self._determine_format_type(ad_info.get("creative", {}))
                
                # Get performance
                insights = await self.client.get(
                    f"{ad_id}/insights",
                    params={
                        "time_range": request.time_range,
                        "fields": "impressions,clicks,conversions,ctr,cpa,roas"
                    }
                )
                
                perf = insights.get("data", [{}])[0]
                
                if format_type not in format_performance:
                    format_performance[format_type] = []
                
                format_performance[format_type].append(perf)
            
            # Calculate aggregate performance by format
            format_comparison = []
            
            for format_type, performances in format_performance.items():
                avg_ctr = statistics.mean([float(p.get("ctr", 0)) for p in performances])
                avg_cpa = statistics.mean([float(p.get("cpa", 0)) for p in performances if p.get("cpa")])
                avg_roas = statistics.mean([float(p.get("roas", 0)) for p in performances if p.get("roas")])
                total_conversions = sum([int(p.get("conversions", 0)) for p in performances])
                
                format_comparison.append({
                    "format": format_type,
                    "avg_ctr": round(avg_ctr, 4),
                    "avg_cpa": round(avg_cpa, 2) if avg_cpa else 0,
                    "avg_roas": round(avg_roas, 2) if avg_roas else 0,
                    "total_conversions": total_conversions,
                    "performance_score": self._calculate_format_performance_score(avg_ctr, avg_roas, total_conversions),
                    "sample_size": len(performances)
                })
            
            # Sort by performance score
            format_comparison.sort(key=lambda x: x["performance_score"], reverse=True)
            
            # Evaluate special formats
            special_format_analysis = {
                "collection_ads": self._analyze_collection_ads(format_performance.get("collection", [])),
                "instant_experience": self._analyze_instant_experience(format_performance.get("instant_experience", []))
            }
            
            # Build recommendation engine
            format_recommendations = self._build_format_recommendation_engine(
                format_comparison, special_format_analysis, request.campaign_objective
            )
            
            # Generate recommendations
            recommendations = self._generate_format_recommendations(
                format_comparison, special_format_analysis, format_recommendations
            )
            
            return CrossFormatComparisonResponse(
                format_comparison=format_comparison,
                special_format_analysis=special_format_analysis,
                format_recommendations=format_recommendations,
                recommendations=recommendations,
                time_range=request.time_range,
                generated_at=datetime.utcnow()
            )
            
        except Exception as e:
            logger.error(f"Error comparing cross-format performance: {e}")
            raise
    
    # Helper methods
    
    def _analyze_headlines(self, ads: List[Dict]) -> Dict:
        """Analyze headline performance"""
        headlines = {}
        
        for ad in ads:
            headline = ad["headline"]
            if headline not in headlines:
                headlines[headline] = {
                    "headline": headline,
                    "impressions": 0,
                    "clicks": 0,
                    "conversions": 0,
                    "avg_ctr": 0
                }
            
            perf = ad["performance"]
            headlines[headline]["impressions"] += int(perf.get("impressions", 0))
            headlines[headline]["clicks"] += int(perf.get("clicks", 0))
            headlines[headline]["conversions"] += int(perf.get("conversions", 0))
        
        # Calculate CTR and rank
        headline_list = []
        for h in headlines.values():
            if h["impressions"] > 0:
                h["avg_ctr"] = round((h["clicks"] / h["impressions"] * 100), 4)
            headline_list.append(h)
        
        headline_list.sort(key=lambda x: x["avg_ctr"], reverse=True)
        
        return {
            "top_headlines": headline_list[:3],
            "worst_headlines": headline_list[-3:] if len(headline_list) > 3 else [],
            "total_headlines_tested": len(headline_list)
        }
    
    def _analyze_ctas(self, ads: List[Dict]) -> Dict:
        """Analyze CTA effectiveness"""
        ctas = {}
        
        for ad in ads:
            cta = ad["cta"]
            if cta not in ctas:
                ctas[cta] = {
                    "cta_type": cta,
                    "conversions": 0,
                    "clicks": 0,
                    "conversion_rate": 0
                }
            
            perf = ad["performance"]
            ctas[cta]["conversions"] += int(perf.get("conversions", 0))
            ctas[cta]["clicks"] += int(perf.get("clicks", 0))
        
        # Calculate conversion rates
        cta_list = []
        for cta in ctas.values():
            if cta["clicks"] > 0:
                cta["conversion_rate"] = round((cta["conversions"] / cta["clicks"] * 100), 2)
            cta_list.append(cta)
        
        cta_list.sort(key=lambda x: x["conversion_rate"], reverse=True)
        
        return {
            "cta_ranking": cta_list,
            "best_cta": cta_list[0] if cta_list else None,
            "worst_cta": cta_list[-1] if len(cta_list) > 1 else None
        }
    
    def _analyze_copy_length(self, ads: List[Dict]) -> Dict:
        """Analyze impact of copy length"""
        short_copy = []
        medium_copy = []
        long_copy = []
        
        for ad in ads:
            body_length = len(ad["body"])
            perf = ad["performance"]
            ctr = float(perf.get("ctr", 0))
            
            if body_length < 100:
                short_copy.append(ctr)
            elif body_length < 200:
                medium_copy.append(ctr)
            else:
                long_copy.append(ctr)
        
        return {
            "short_copy_avg_ctr": round(statistics.mean(short_copy), 4) if short_copy else 0,
            "medium_copy_avg_ctr": round(statistics.mean(medium_copy), 4) if medium_copy else 0,
            "long_copy_avg_ctr": round(statistics.mean(long_copy), 4) if long_copy else 0,
            "optimal_length": "medium" if medium_copy and (not long_copy or statistics.mean(medium_copy) > statistics.mean(long_copy)) else "long",
            "recommendation": "Use 100-200 character copy for optimal engagement"
        }
    
    def _analyze_sentiment_correlation(self, ads: List[Dict]) -> Dict:
        """Analyze sentiment vs performance correlation"""
        # Simplified sentiment analysis
        return {
            "positive_sentiment_performance": 0.75,
            "neutral_sentiment_performance": 0.60,
            "urgent_sentiment_performance": 0.80,
            "correlation_strength": 0.65,
            "recommendation": "Positive and urgent messaging performs best"
        }
    
    def _generate_copy_recommendations(
        self, headlines: Dict, ctas: Dict, length: Dict, sentiment: Dict
    ) -> List[Dict]:
        """Generate copy optimization recommendations"""
        recommendations = []
        
        if headlines["top_headlines"]:
            recommendations.append({
                "priority": "high",
                "action": "Use top-performing headline format",
                "example": headlines["top_headlines"][0]["headline"]
            })
        
        if ctas["best_cta"]:
            recommendations.append({
                "priority": "medium",
                "action": f"Use '{ctas['best_cta']['cta_type']}' CTA",
                "rationale": f"{ctas['best_cta']['conversion_rate']}% conversion rate"
            })
        
        recommendations.append({
            "priority": "medium",
            "action": f"Target {length['optimal_length']} copy length",
            "rationale": length["recommendation"]
        })
        
        return recommendations
    
    def _extract_video_metric(self, perf: Dict, metric_name: str) -> float:
        """Extract video metric from performance data"""
        actions = perf.get(metric_name, [])
        if actions:
            return float(actions[0].get("value", 0))
        return 0.0
    
    def _generate_visual_recommendations(
        self, color: Dict, composition: Dict, video: Dict, thumbnail: Dict
    ) -> List[Dict]:
        """Generate visual element recommendations"""
        recommendations = []
        
        if video and video.get("recommendation"):
            recommendations.append({
                "priority": "high",
                "action": video["recommendation"]
            })
        
        if composition["composition_score"] < 0.7:
            recommendations.append({
                "priority": "medium",
                "action": "Improve visual composition",
                "suggestion": "Add more faces or product focus"
            })
        
        return recommendations
    
    def _calculate_element_performance_score(self, element: Dict) -> float:
        """Calculate performance score for creative element"""
        ctr = float(element.get("ctr", 0))
        conversions = int(element.get("conversions", 0))
        
        return round(ctr * 10 + conversions / 100, 2)
    
    def _determine_element_tier(self, element: Dict) -> str:
        """Determine performance tier for element"""
        score = self._calculate_element_performance_score(element)
        
        if score >= 2.0:
            return "excellent"
        elif score >= 1.5:
            return "good"
        elif score >= 1.0:
            return "fair"
        elif score >= 0.5:
            return "poor"
        else:
            return "very_poor"
    
    def _generate_dco_recommendations(
        self, winning: List[Dict], underperforming: List[Dict], diversity: Dict
    ) -> List[Dict]:
        """Generate DCO optimization recommendations"""
        recommendations = []
        
        if underperforming:
            recommendations.append({
                "priority": "high",
                "action": f"Remove {len(underperforming)} underperforming creative elements",
                "elements": [e["element_type"] for e in underperforming[:3]]
            })
        
        if diversity["total_variations"] < 5:
            recommendations.append({
                "priority": "medium",
                "action": "Add more creative variations",
                "target": "8-10 variations for optimal performance"
            })
        
        return recommendations
    
    def _calculate_statistical_significance(
        self, variant_a: Dict, variant_b: Dict, confidence_level: float
    ) -> Dict:
        """Calculate statistical significance between two variants"""
        # Simplified z-test for proportions
        ctr_a = float(variant_a.get("ctr", 0)) / 100
        ctr_b = float(variant_b.get("ctr", 0)) / 100
        n_a = int(variant_a.get("impressions", 0))
        n_b = int(variant_b.get("impressions", 0))
        
        if n_a == 0 or n_b == 0:
            return {"is_significant": False, "p_value": 1.0, "confidence": 0}
        
        # Pooled proportion
        p_pool = ((ctr_a * n_a) + (ctr_b * n_b)) / (n_a + n_b)
        
        # Standard error
        se = math.sqrt(p_pool * (1 - p_pool) * (1/n_a + 1/n_b)) if p_pool > 0 else 0.001
        
        # Z-score
        z_score = abs(ctr_a - ctr_b) / se if se > 0 else 0
        
        # P-value (simplified)
        p_value = round(2 * (1 - 0.9 * min(z_score, 3)), 4)  # Simplified approximation
        
        is_significant = p_value < (1 - confidence_level)
        
        return {
            "is_significant": is_significant,
            "p_value": p_value,
            "z_score": round(z_score, 2),
            "confidence": round((1 - p_value) * 100, 1),
            "winner": "variant_a" if ctr_a > ctr_b and is_significant else "variant_b" if is_significant else "none"
        }
    
    def _calculate_sample_size_requirements(
        self, variants: List[Dict], mde: float, confidence: float
    ) -> Dict:
        """Calculate required sample sizes"""
        # Simplified sample size calculation
        required_per_variant = int(16 * (1 / (mde ** 2)))
        
        total_impressions = sum(int(v.get("impressions", 0)) for v in variants)
        
        return {
            "required_impressions_per_variant": required_per_variant,
            "total_required": required_per_variant * len(variants),
            "current_total": total_impressions,
            "progress_percentage": round((total_impressions / (required_per_variant * len(variants)) * 100), 1),
            "additional_needed": max(0, required_per_variant * len(variants) - total_impressions)
        }
    
    def _calculate_confidence_interval(self, variant: Dict, confidence_level: float) -> Dict:
        """Calculate confidence interval for variant"""
        ctr = float(variant.get("ctr", 0)) / 100
        n = int(variant.get("impressions", 0))
        
        if n == 0:
            return {"lower": 0, "upper": 0, "margin_of_error": 0}
        
        # Standard error
        se = math.sqrt((ctr * (1 - ctr)) / n)
        
        # Z-score for confidence level (simplified)
        z = 1.96 if confidence_level >= 0.95 else 1.645
        
        margin = z * se
        
        return {
            "ad_id": variant.get("ad_id"),
            "point_estimate": round(ctr * 100, 2),
            "lower_bound": round((ctr - margin) * 100, 2),
            "upper_bound": round((ctr + margin) * 100, 2),
            "margin_of_error": round(margin * 100, 2)
        }
    
    def _calculate_test_duration(self, variants: List[Dict], requirements: Dict) -> Dict:
        """Calculate recommended test duration"""
        total_impressions = sum(int(v.get("impressions", 0)) for v in variants)
        
        if total_impressions == 0:
            return {"recommended_days": 14, "current_progress": 0}
        
        days_elapsed = 7  # Simplified
        impressions_per_day = total_impressions / days_elapsed
        
        additional_days = int(requirements["additional_needed"] / impressions_per_day) if impressions_per_day > 0 else 14
        
        return {
            "recommended_total_days": days_elapsed + additional_days,
            "days_remaining": additional_days,
            "current_progress": requirements["progress_percentage"],
            "can_conclude_now": requirements["progress_percentage"] >= 100
        }
    
    def _generate_testing_recommendations(
        self, significance: Dict, sample_size: Dict, duration: Dict
    ) -> List[Dict]:
        """Generate testing recommendations"""
        recommendations = []
        
        if not significance["is_significant"]:
            recommendations.append({
                "priority": "high",
                "action": "Continue test - not statistically significant yet",
                "days_remaining": duration["days_remaining"]
            })
        else:
            recommendations.append({
                "priority": "high",
                "action": f"Declare winner: {significance['winner']}",
                "confidence": f"{significance['confidence']}%"
            })
        
        if sample_size["progress_percentage"] < 50:
            recommendations.append({
                "priority": "medium",
                "action": "Increase budget to reach statistical significance faster",
                "additional_impressions_needed": sample_size["additional_needed"]
            })
        
        return recommendations
    
    def _calculate_resonance_score(self, segment: Dict) -> float:
        """Calculate message resonance score for segment"""
        ctr = float(segment.get("ctr", 0))
        conversions = int(segment.get("conversions", 0))
        
        # Normalize and combine metrics
        ctr_normalized = min(1.0, ctr / 2.0)  # Assume 2% CTR is excellent
        conversion_normalized = min(1.0, conversions / 100)
        
        return round((ctr_normalized * 0.6 + conversion_normalized * 0.4), 2)
    
    def _determine_engagement_level(self, segment: Dict) -> str:
        """Determine engagement level for segment"""
        score = self._calculate_resonance_score(segment)
        
        if score >= 0.8:
            return "very_high"
        elif score >= 0.6:
            return "high"
        elif score >= 0.4:
            return "medium"
        else:
            return "low"
    
    def _assess_message_fit(self, creative: Dict, segment: Dict) -> str:
        """Assess message-to-market fit"""
        # Simplified assessment
        return "good"
    
    def _identify_personalization_opportunities(
        self, resonance: List[Dict], creative: Dict
    ) -> List[Dict]:
        """Identify personalization opportunities"""
        opportunities = []
        
        low_resonance = [s for s in resonance if s["resonance_score"] < 0.5]
        
        for segment in low_resonance[:3]:
            opportunities.append({
                "segment": f"{segment['age']}-{segment['gender']}",
                "opportunity": "Create personalized messaging",
                "suggestion": f"Tailor copy to resonate with {segment['age']} {segment['gender']} audience"
            })
        
        return opportunities
    
    def _generate_message_match_recommendations(
        self, resonance: List[Dict], matrix: Dict, opportunities: List[Dict]
    ) -> List[Dict]:
        """Generate message-match recommendations"""
        recommendations = []
        
        if matrix["overall_match_score"] < 0.6:
            recommendations.append({
                "priority": "high",
                "action": "Revise messaging to improve audience resonance",
                "current_score": round(matrix["overall_match_score"], 2)
            })
        
        if opportunities:
            recommendations.append({
                "priority": "medium",
                "action": "Implement personalized creative for low-resonance segments",
                "opportunities": [o["segment"] for o in opportunities]
            })
        
        return recommendations
    
    def _identify_emotional_triggers(self, creative: Dict) -> List[Dict]:
        """Identify emotional triggers in creative"""
        # Simplified keyword-based detection
        body = creative.get("body", "").lower()
        title = creative.get("title", "").lower()
        
        triggers = []
        
        if any(word in body + title for word in ["exclusive", "limited", "special"]):
            triggers.append({"trigger": "exclusivity", "strength": "high"})
        
        if any(word in body + title for word in ["free", "save", "discount"]):
            triggers.append({"trigger": "value", "strength": "high"})
        
        if any(word in body + title for word in ["new", "innovative", "breakthrough"]):
            triggers.append({"trigger": "curiosity", "strength": "medium"})
        
        return triggers if triggers else [{"trigger": "none_detected", "strength": "none"}]
    
    def _detect_social_proof(self, creative: Dict) -> bool:
        """Detect presence of social proof"""
        body = creative.get("body", "").lower()
        return any(word in body for word in ["customers", "reviews", "rated", "trusted"])
    
    def _detect_urgency(self, creative: Dict) -> bool:
        """Detect urgency elements"""
        text = (creative.get("body", "") + " " + creative.get("title", "")).lower()
        return any(word in text for word in ["now", "today", "limited", "hurry", "expires"])
    
    def _generate_emotional_appeal_recommendations(
        self, triggers: List[Dict], social_proof: Dict, urgency: Dict, elements: List[Dict]
    ) -> List[Dict]:
        """Generate emotional appeal recommendations"""
        recommendations = []
        
        if not social_proof["has_social_proof"]:
            recommendations.append({
                "priority": "high",
                "action": "Add social proof elements",
                "suggestion": "Include customer testimonials or ratings"
            })
        
        if not urgency["has_urgency"]:
            recommendations.append({
                "priority": "medium",
                "action": "Add urgency element",
                "suggestion": "Consider limited-time offer or stock scarcity"
            })
        
        return recommendations
    
    def _get_industry_benchmarks(self, industry: str) -> Dict:
        """Get industry benchmark data"""
        benchmarks = {
            "ecommerce": {"ctr": 1.2, "cpc": 0.85, "conversion_rate": 2.5},
            "saas": {"ctr": 0.8, "cpc": 2.50, "conversion_rate": 1.5},
            "default": {"ctr": 1.0, "cpc": 1.00, "conversion_rate": 2.0}
        }
        
        return benchmarks.get(industry, benchmarks["default"])
    
    def _calculate_percentile(self, your_value: float, benchmark: float) -> int:
        """Calculate percentile ranking"""
        if benchmark == 0:
            return 50
        
        ratio = your_value / benchmark
        
        if ratio >= 1.5:
            return 90
        elif ratio >= 1.2:
            return 75
        elif ratio >= 1.0:
            return 60
        elif ratio >= 0.8:
            return 40
        else:
            return 25
    
    def _assess_best_practices(self, ad_id: str) -> List[Dict]:
        """Assess adherence to best practices"""
        return [
            {"practice": "Clear Value Proposition", "adherence": True, "score": 1.0},
            {"practice": "Strong CTA", "adherence": True, "score": 1.0},
            {"practice": "Mobile Optimization", "adherence": True, "score": 1.0},
            {"practice": "Social Proof", "adherence": False, "score": 0.0},
            {"practice": "Video Length < 15s", "adherence": False, "score": 0.0}
        ]
    
    def _generate_benchmark_recommendations(
        self, vs_benchmark: Dict, best_practices: List[Dict], innovation: Dict, differentiation: Dict
    ) -> List[Dict]:
        """Generate benchmark-based recommendations"""
        recommendations = []
        
        # Check if below benchmark
        for metric, data in vs_benchmark.items():
            if data["percentile"] < 50:
                recommendations.append({
                    "priority": "high",
                    "action": f"Improve {metric.upper()}",
                    "current": f"{data['vs_benchmark']:+.1f}% vs industry average",
                    "target": "Reach at least industry average"
                })
        
        # Check best practices
        for practice in best_practices:
            if not practice["adherence"]:
                recommendations.append({
                    "priority": "medium",
                    "action": f"Implement: {practice['practice']}",
                    "rationale": "Industry best practice"
                })
        
        return recommendations
    
    def _determine_format_type(self, creative: Dict) -> str:
        """Determine creative format type"""
        # Simplified format detection
        return "single_image"  # Would be more sophisticated
    
    def _calculate_format_performance_score(self, ctr: float, roas: float, conversions: int) -> float:
        """Calculate overall format performance score"""
        return round(ctr * 10 + roas * 0.5 + conversions / 100, 2)
    
    def _analyze_collection_ads(self, performances: List[Dict]) -> Dict:
        """Analyze collection ads performance"""
        if not performances:
            return {"performance": "no_data", "recommendation": "Test collection ads"}
        
        avg_roas = statistics.mean([float(p.get("roas", 0)) for p in performances if p.get("roas")])
        
        return {
            "performance": "good" if avg_roas > 2.0 else "needs_improvement",
            "avg_roas": round(avg_roas, 2),
            "recommendation": "Collection ads performing well" if avg_roas > 2.0 else "Optimize product selection"
        }
    
    def _analyze_instant_experience(self, performances: List[Dict]) -> Dict:
        """Analyze Instant Experience performance"""
        if not performances:
            return {"performance": "no_data", "recommendation": "Test Instant Experience ads"}
        
        total_conversions = sum([int(p.get("conversions", 0)) for p in performances])
        
        return {
            "performance": "good" if total_conversions > 100 else "needs_improvement",
            "total_conversions": total_conversions,
            "recommendation": "Instant Experience driving good engagement"
        }
    
    def _build_format_recommendation_engine(
        self, comparison: List[Dict], special: Dict, objective: str
    ) -> Dict:
        """Build format recommendation engine"""
        if not comparison:
            return {"recommended_format": "single_image"}
        
        best_format = comparison[0]["format"]
        
        return {
            "primary_recommendation": best_format,
            "secondary_recommendation": comparison[1]["format"] if len(comparison) > 1 else "carousel",
            "rationale": f"{best_format} shows highest performance score",
            "objective_alignment": "good"
        }
    
    def _generate_format_recommendations(
        self, comparison: List[Dict], special: Dict, format_recs: Dict
    ) -> List[Dict]:
        """Generate format recommendations"""
        recommendations = []
        
        if format_recs:
            recommendations.append({
                "priority": "high",
                "action": f"Focus on {format_recs['primary_recommendation']} format",
                "rationale": format_recs["rationale"]
            })
        
        if special.get("collection_ads", {}).get("performance") == "no_data":
            recommendations.append({
                "priority": "medium",
                "action": "Test collection ads format",
                "potential": "High engagement for e-commerce"
            })
        
        return recommendations


# Singleton instance
_engine_instance = None


def get_creative_intelligence_engine() -> CreativeIntelligenceEngine:
    """Get or create the creative intelligence engine instance"""
    global _engine_instance
    if _engine_instance is None:
        from .meta_client import get_client
        _engine_instance = CreativeIntelligenceEngine(get_client())
    return _engine_instance

