#!/usr/bin/env python3
"""
Model traffic and lead impact of 2026 initiatives:
- Template expansion (2 → 20-30 templates)
- Tools expansion (16 → 30+ tools)
- Comparison pages (54 → 100+ pages)
- Blog migration (102 → 150+ posts)
- Conversion optimization (0.36% → 0.5-0.7%)
"""

import json
from pathlib import Path
from datetime import datetime, timedelta
from dateutil.relativedelta import relativedelta

def get_prev_month(month_str):
    """Get previous month string."""
    year, month = map(int, month_str.split('-'))
    if month == 1:
        return f"{year-1}-12"
    else:
        return f"{year}-{month-1:02d}"

def model_template_impact():
    """Model traffic/lead impact of expanding from 2 to 20-30 templates."""
    
    # Current state
    current_templates = 2
    target_templates = 25  # Middle of 20-30 range
    
    # Benchmarks
    traffic_per_template = {
        'month_1': 50,      # New template, minimal traffic
        'month_2': 150,     # Starting to rank
        'month_3': 250,     # Mature
        'month_4+': 350     # Fully mature
    }
    
    conversion_rate = 0.05  # 5% average (3-8% range)
    
    # Rollout schedule (Q1-Q2: 20 templates, Q3-Q4: 5 more)
    rollout = {
        '2026-01': 3,   # Start with 3 templates
        '2026-02': 5,   # Add 2 more
        '2026-03': 8,   # Add 3 more
        '2026-04': 12,  # Add 4 more
        '2026-05': 15,  # Add 3 more
        '2026-06': 18,  # Add 3 more
        '2026-07': 20,  # Add 2 more
        '2026-08': 22,  # Add 2 more
        '2026-09': 23,  # Add 1 more
        '2026-10': 24,  # Add 1 more
        '2026-11': 25,  # Add 1 more
        '2026-12': 25   # Maintain
    }
    
    # Calculate monthly impact
    impact = {}
    template_ages = {}  # Track age of each template cohort
    prev_month = '2025-12'
    
    for month in rollout.keys():
        new_templates = rollout[month] - rollout.get(prev_month, current_templates) if month != '2026-01' else rollout[month] - current_templates
        
        # Update template ages
        for cohort_month in list(template_ages.keys()):
            template_ages[cohort_month] += 1
        
        # Add new templates
        if new_templates > 0:
            template_ages[month] = 0
        
        # Calculate traffic and leads
        total_traffic = 0
        for cohort_month, age in template_ages.items():
            cohort_prev = '2025-12' if cohort_month == '2026-01' else get_prev_month(cohort_month)
            templates_in_cohort = rollout[cohort_month] - rollout.get(cohort_prev, current_templates)
            if age == 0:
                traffic_per = traffic_per_template['month_1']
            elif age == 1:
                traffic_per = traffic_per_template['month_2']
            elif age == 2:
                traffic_per = traffic_per_template['month_3']
            else:
                traffic_per = traffic_per_template['month_4+']
            total_traffic += templates_in_cohort * traffic_per
        
        total_leads = total_traffic * conversion_rate
        
        impact[month] = {
            'templates': rollout[month],
            'new_templates': new_templates,
            'traffic': round(total_traffic),
            'leads': round(total_leads)
        }
        
        prev_month = month
    
    return impact

def model_tools_impact():
    """Model traffic/lead impact of expanding from 16 to 30+ tools."""
    
    current_tools = 16
    target_tools = 32  # 2x expansion
    
    # Benchmarks (varies by keyword)
    traffic_per_tool = {
        'low': 200,      # Long-tail keywords
        'medium': 500,   # Medium keywords
        'high': 1000     # High-volume keywords
    }
    
    conversion_rate = 0.02  # 2% average (1-3% range)
    
    # Rollout schedule (biweekly = ~2 tools/month)
    rollout = {
        '2026-01': 18,  # +2
        '2026-02': 20,  # +2
        '2026-03': 22,  # +2
        '2026-04': 24,  # +2
        '2026-05': 26,  # +2
        '2026-06': 28,  # +2
        '2026-07': 30,  # +2
        '2026-08': 31,  # +1
        '2026-09': 32,  # +1
        '2026-10': 32,  # Maintain
        '2026-11': 32,  # Maintain
        '2026-12': 32   # Maintain
    }
    
    # Calculate monthly impact (mix of low/medium/high traffic tools)
    impact = {}
    tool_ages = {}
    
    for month in rollout.keys():
        new_tools = rollout[month] - rollout.get(prev_month, current_tools) if month != '2026-01' else rollout[month] - current_tools
        
        # Update tool ages
        for cohort_month in list(tool_ages.keys()):
            tool_ages[cohort_month] += 1
        
        # Add new tools (mix: 40% low, 40% medium, 20% high)
        if new_tools > 0:
            tool_ages[month] = 0
        
        # Calculate traffic and leads
        total_traffic = 0
        for cohort_month, age in tool_ages.items():
            cohort_prev = '2025-12' if cohort_month == '2026-01' else get_prev_month(cohort_month)
            tools_in_cohort = rollout[cohort_month] - rollout.get(cohort_prev, current_tools)
            # Mature tools get full traffic (age 3+)
            if age >= 3:
                avg_traffic = (traffic_per_tool['low'] * 0.4 + traffic_per_tool['medium'] * 0.4 + traffic_per_tool['high'] * 0.2)
            elif age == 2:
                avg_traffic = (traffic_per_tool['low'] * 0.4 + traffic_per_tool['medium'] * 0.4 + traffic_per_tool['high'] * 0.2) * 0.7
            elif age == 1:
                avg_traffic = (traffic_per_tool['low'] * 0.4 + traffic_per_tool['medium'] * 0.4 + traffic_per_tool['high'] * 0.2) * 0.4
            else:
                avg_traffic = (traffic_per_tool['low'] * 0.4 + traffic_per_tool['medium'] * 0.4 + traffic_per_tool['high'] * 0.2) * 0.2
            total_traffic += tools_in_cohort * avg_traffic
        
        total_leads = total_traffic * conversion_rate
        
        impact[month] = {
            'tools': rollout[month],
            'new_tools': new_tools,
            'traffic': round(total_traffic),
            'leads': round(total_leads)
        }
        
        prev_month = month
    
    return impact

def model_comparison_impact():
    """Model traffic/lead impact of expanding comparison pages to 100+."""
    
    current_pages = 54
    target_pages = 110  # ~2x expansion
    
    traffic_per_page = 60  # Average (20-100 range)
    conversion_rate = 0.035  # 3.5% average (2-5% range)
    
    # Distribution improvement: 2-3x traffic boost
    distribution_multiplier = 2.5
    
    # Rollout schedule
    rollout = {
        '2026-01': 60,   # +6 (modular system launch)
        '2026-02': 70,   # +10
        '2026-03': 80,   # +10
        '2026-04': 90,   # +10
        '2026-05': 100,  # +10
        '2026-06': 105,  # +5
        '2026-07': 108,  # +3
        '2026-08': 110,  # +2
        '2026-09': 110,  # Maintain
        '2026-10': 110,  # Maintain
        '2026-11': 110,  # Maintain
        '2026-12': 110   # Maintain
    }
    
    impact = {}
    tool_ages = {}
    prev_month = '2025-12'
    
    for month in rollout.keys():
        # Base traffic from all pages
        base_traffic = rollout[month] * traffic_per_page
        
        # Apply distribution multiplier (gradual rollout)
        if month >= '2026-04':  # Full distribution boost after Q1
            multiplier = distribution_multiplier
        elif month >= '2026-02':
            multiplier = 1.5 + (distribution_multiplier - 1.5) * ((int(month.split('-')[1]) - 2) / 2)
        else:
            multiplier = 1.5
        
        total_traffic = base_traffic * multiplier
        total_leads = total_traffic * conversion_rate
        
        impact[month] = {
            'pages': rollout[month],
            'new_pages': rollout[month] - rollout.get(prev_month, current_pages),
            'traffic': round(total_traffic),
            'leads': round(total_leads),
            'distribution_multiplier': round(multiplier, 2)
        }
        
        prev_month = month
    
    return impact

def model_blog_impact():
    """Model traffic impact of blog migration and expansion."""
    
    current_posts = 102
    target_posts = 150
    
    # Migration boost: 15-30% improvement
    migration_boost = 0.20  # 20% average
    
    # Traffic per post (mature)
    traffic_per_post = 125  # Average (50-200 range)
    
    # New posts rollout
    new_posts_rollout = {
        '2026-01': 0,   # Migration prep
        '2026-02': 0,   # Migration
        '2026-03': 4,   # +4 (migration complete)
        '2026-04': 8,   # +4
        '2026-05': 12,  # +4
        '2026-06': 16,  # +4
        '2026-07': 20,  # +4
        '2026-08': 24,  # +4
        '2026-09': 28,  # +4
        '2026-10': 32,  # +4
        '2026-11': 36,  # +4
        '2026-12': 40   # +4 (total 48 new posts)
    }
    
    impact = {}
    prev_month = '2025-12'
    
    for month in new_posts_rollout.keys():
        total_posts = current_posts + new_posts_rollout[month]
        
        # Base traffic from all posts
        base_traffic = total_posts * traffic_per_post
        
        # Apply migration boost (after Q2)
        if month >= '2026-03':
            multiplier = 1 + migration_boost
        else:
            multiplier = 1.0
        
        total_traffic = base_traffic * multiplier
        
        impact[month] = {
            'posts': total_posts,
            'new_posts': new_posts_rollout[month],
            'traffic': round(total_traffic),
            'migration_boost': migration_boost if month >= '2026-03' else 0
        }
        
        prev_month = month
    
    return impact

def model_conversion_optimization():
    """Model conversion rate improvement impact."""
    
    current_rate = 0.0036  # 0.36% average
    target_rate = 0.006  # 0.6% average (0.5-0.7% range)
    
    # Gradual improvement timeline
    improvement_timeline = {
        '2026-01': 0.004,   # 0.40%
        '2026-02': 0.0045,  # 0.45%
        '2026-03': 0.005,   # 0.50%
        '2026-04': 0.0055,  # 0.55%
        '2026-05': 0.006,   # 0.60%
        '2026-06': 0.006,   # Maintain
        '2026-07': 0.006,   # Maintain
        '2026-08': 0.006,   # Maintain
        '2026-09': 0.006,   # Maintain
        '2026-10': 0.006,   # Maintain
        '2026-11': 0.006,   # Maintain
        '2026-12': 0.006    # Maintain
    }
    
    return improvement_timeline

def combine_impacts():
    """Combine all initiative impacts."""
    
    templates = model_template_impact()
    tools = model_tools_impact()
    comparisons = model_comparison_impact()
    blog = model_blog_impact()
    conversion = model_conversion_optimization()
    
    combined = {}
    
    for month in templates.keys():
        combined[month] = {
            'templates': templates[month],
            'tools': tools[month],
            'comparisons': comparisons[month],
            'blog': blog[month],
            'conversion_rate': conversion[month],
            'total_traffic': (
                templates[month]['traffic'] +
                tools[month]['traffic'] +
                comparisons[month]['traffic'] +
                blog[month]['traffic']
            )
        }
    
    return combined

if __name__ == "__main__":
    impacts = combine_impacts()
    
    output_file = Path(__file__).parent.parent / "06-DATA-ANALYSIS" / "initiative_impacts.json"
    with open(output_file, 'w', encoding='utf-8') as f:
        json.dump(impacts, f, indent=2, ensure_ascii=False)
    
    print("Initiative Impact Modeling Complete")
    print(f"\nTotal Traffic Impact by Month:")
    for month, data in impacts.items():
        print(f"  {month}: {data['total_traffic']:,} sessions")
    
    print(f"\nOutput saved to: {output_file}")

