"""
Generate Final Evaluation Report (Enhanced)

This script creates a comprehensive, side-by-side comparison report that combines:
1. Business Input Data (Description, Budget, Predicted Targets)
2. Recommended Influencer Data (Handle, Niche, Tier, Raw Metrics)
3. Match Performance (Total Score, Niche Similarity, Budget Fit, Explanations)

Output: `final_evaluation_report.csv`

Usage:
    cd ml_service/evaluation
    python generate_final_report.py
"""

import sys
import json
import pandas as pd
from pathlib import Path

def main():
    print("🚀 Generating Comprehensive Final Evaluation Report...")
    
    # Paths
    EVAL_ROOT = Path(__file__).resolve().parent
    REC_CSV = EVAL_ROOT / "recommendation" / "recommendation_results.csv"
    Biz_JSON = EVAL_ROOT / "business" / "business_predictions.json"
    INF_CSV = EVAL_ROOT / "influencer" / "influencer_predictions.csv"
    OUTPUT_CSV = EVAL_ROOT / "final_evaluation_report.csv"
    
    # 1. Load Recommendation Results
    if not REC_CSV.exists():
        print(f"❌ Error: Recommendations not found at {REC_CSV}")
        return
    df_recs = pd.read_csv(REC_CSV)
    print(f"Loaded {len(df_recs)} recommendation pairs.")

    # 2. Load Business Context
    biz_context = {}
    if Biz_JSON.exists():
        try:
            with open(Biz_JSON, "r", encoding="utf-8") as f:
                biz_data = json.load(f)
            for item in biz_data:
                name = item['business_name']
                pred = item.get('prediction', {})
                processed = pred.get('processed_data', {})
                biz_context[name] = {
                    'biz_description': item.get('description', ''),
                    'biz_target_niche': processed.get('top_niche', 'N/A'),
                    'biz_target_tier': processed.get('top_popularity', 'N/A')
                }
            print(f"Loaded context for {len(biz_context)} businesses.")
        except Exception as e:
            print(f"⚠️ Warning: Could not parse business JSON: {e}")
    else:
        print(f"⚠️ Warning: Business predictions not found at {Biz_JSON}")

    # 3. Load Influencer Raw Metrics
    inf_metrics = {}
    if INF_CSV.exists():
        try:
            df_inf = pd.read_csv(INF_CSV)
            # Use username as key for easy lookup
            inf_metrics = df_inf.set_index('username').to_dict('index')
            print(f"Loaded raw metrics for {len(inf_metrics)} influencers.")
        except Exception as e:
            print(f"⚠️ Warning: Could not parse influencer CSV: {e}")
    else:
        print(f"⚠️ Warning: Influencer predictions not found at {INF_CSV}")

    # 4. Merge and Enhance
    enhanced_records = []
    
    for _, row in df_recs.iterrows():
        rec = row.to_dict()
        biz_name = rec['business_name']
        inf_handle = rec['influencer_handle']
        
        # Add Business Context
        if biz_name in biz_context:
            rec.update(biz_context[biz_name])
            
        # Add Raw Influencer Metrics & Context
        if inf_handle in inf_metrics:
            m = inf_metrics[inf_handle]
            rec['inf_engagement_rate'] = f"{m.get('engagement_rate', 0):.2f}%"
            rec['inf_quality_ratio'] = round(m.get('quality_ratio', 0), 4)
            rec['inf_avg_likes'] = int(m.get('avg_likes', 0))
            rec['inf_avg_comments'] = int(m.get('avg_comments', 0))
            
            # --- CONTEXT COLUMNS FOR EVALUATION ---
            rec['biography'] = m.get('biography', '')
            rec['recent_captions'] = m.get('recent_captions', '')
            rec['recent_hashtags'] = m.get('recent_hashtags', '')
            
            # Get full explanations if they weren't fully in rec_csv
            rec['niche_rationale_full'] = m.get('niche_classification_explanation', '')
            rec['tier_rationale_full'] = m.get('tier_classification_explanation', '')

        enhanced_records.append(rec)

    # 5. Build Final DataFrame with LOGICAL EVALUATION ORDER
    df_final = pd.DataFrame(enhanced_records)
    
    # Re-order columns exactly as requested for manual evaluation
    column_groups = [
        # 1 & 2: Business Input & Budget
        ['business_name', 'biz_description', 'business_budget'],
        
        # 3: Recommended Influencer
        ['influencer_handle'],
        
        # 4: Quality Metrics & Followers
        ['influencer_followers', 'inf_engagement_rate', 'inf_quality_ratio', 'inf_avg_likes', 'inf_avg_comments'],
        
        # 5: Tier and Price (Quality Adjusted)
        ['influencer_tier', 'influencer_price', 'benchmark_tier', 'is_quality_adjusted', 'quality_multiplier'],
        
        # 6: Context (The "Why")
        ['biography', 'recent_captions', 'recent_hashtags'],
        
        # 7: Influencer Niche
        ['influencer_niche'],
        
        # 8: Matching & Explanations
        ['match_score', 'niche_score', 'budget_score', 'price_position', 'niche_explanation', 'tier_explanation']
    ]
    
    # Flatten the preferred order and filter by existing columns
    preferred_order = [col for group in column_groups for col in group]
    existing_cols = [c for c in preferred_order if c in df_final.columns]
    
    # Add any remaining columns that were missed
    remaining_cols = [c for c in df_final.columns if c not in existing_cols]
    final_cols = existing_cols + remaining_cols
    
    df_final = df_final[final_cols]

    # Save
    df_final.to_csv(OUTPUT_CSV, index=False)
    
    print(f"\n✅ SUCCESS: Final comparison report saved to:")
    print(f"   {OUTPUT_CSV}")
    print(f"\nStats:")
    print(f" - Businesses: {df_final['business_name'].nunique()}")
    print(f" - Recs Per Business: {df_final[df_final['business_name'] == df_final['business_name'].iloc[0]].shape[0]}")
    print(f" - Total Comparison Rows: {len(df_final)}")

if __name__ == "__main__":
    main()
