#!/usr/bin/env python
# coding=utf-8
"""
素材视频内容分析 - 可视化
"""
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
from pathlib import Path

# 设置中文字体
matplotlib.rcParams['font.sans-serif'] = ['PingFang HK', 'Heiti TC', 'Arial Unicode MS']
matplotlib.rcParams['axes.unicode_minus'] = False

# 找到最新的含相似度文件
task_dir = Path(__file__).parent
output_dir = task_dir / "output"
csv_files = list(output_dir.glob("*含相似度*.csv"))
if not csv_files:
    csv_files = list(output_dir.glob("*.csv"))
if not csv_files:
    print("没有找到数据文件")
    exit(1)

latest_file = max(csv_files, key=lambda x: x.stat().st_mtime)
print(f"读取文件: {latest_file.name}")
df = pd.read_csv(latest_file)

# 相似度列配置
similarity_cols = [
    ('文章标题_视频标题_相似度', '文章标题 vs 视频标题'),
    ('分享标题_视频标题_相似度', '分享标题 vs 视频标题'),
    ('文章标题_口播_相似度', '文章标题 vs 口播'),
    ('分享标题_口播_相似度', '分享标题 vs 口播'),
    ('文章标题_一级品类_相似度', '文章标题 vs 一级品类'),
    ('文章标题_二级品类_相似度', '文章标题 vs 二级品类'),
    ('分享标题_一级品类_相似度', '分享标题 vs 一级品类'),
    ('分享标题_二级品类_相似度', '分享标题 vs 二级品类'),
]

# 过滤有效的相似度列
valid_cols = [(col, label) for col, label in similarity_cols
              if col in df.columns and df[col].notna().sum() > 100]

print(f"有效相似度指标: {len(valid_cols)} 个")

# ============================================================
# 图1: 相似度分布直方图
# ============================================================
fig1, axes1 = plt.subplots(2, 4, figsize=(16, 8))
axes1 = axes1.flatten()

for i, (col, label) in enumerate(valid_cols[:8]):
    ax = axes1[i]
    data = df[col].dropna()
    ax.hist(data, bins=30, color='steelblue', edgecolor='white', alpha=0.7)
    ax.axvline(data.mean(), color='red', linestyle='--', label=f'均值: {data.mean():.3f}')
    ax.axvline(data.median(), color='orange', linestyle='--', label=f'中位数: {data.median():.3f}')
    ax.set_title(label, fontsize=11)
    ax.set_xlabel('相似度')
    ax.set_ylabel('频数')
    ax.legend(fontsize=8)

# 隐藏多余的子图
for i in range(len(valid_cols), 8):
    axes1[i].axis('off')

plt.suptitle('相似度分布', fontsize=14, fontweight='bold')
plt.tight_layout()
plt.savefig(output_dir / '相似度分布.png', dpi=150, bbox_inches='tight')
print("已保存: 相似度分布.png")

# ============================================================
# 图2: 相似度 vs 回流率（分组柱状图）
# ============================================================
fig2, axes2 = plt.subplots(2, 4, figsize=(16, 10))
axes2 = axes2.flatten()

bins = [0, 0.3, 0.5, 0.7, 0.9, 1.0]
labels = ['0-0.3', '0.3-0.5', '0.5-0.7', '0.7-0.9', '0.9-1.0']

for i, (col, title) in enumerate(valid_cols[:8]):
    ax = axes2[i]

    # 分组计算回流率
    df['_group'] = pd.cut(df[col], bins=bins, labels=labels)
    grouped = df.groupby('_group', observed=True).agg({
        '点击uv': 'sum',
        '再分享回流uv': 'sum'
    })
    grouped['回流率'] = grouped['再分享回流uv'] / (grouped['点击uv'] + 10)

    # 绘制柱状图
    colors = plt.cm.Blues(np.linspace(0.3, 0.9, len(grouped)))
    bars = ax.bar(range(len(grouped)), grouped['回流率'] * 100, color=colors, edgecolor='white')

    # 添加数值标签
    for bar, (idx, row) in zip(bars, grouped.iterrows()):
        height = bar.get_height()
        ax.text(bar.get_x() + bar.get_width()/2., height + 0.5,
                f'{height:.1f}%', ha='center', va='bottom', fontsize=9)

    ax.set_xticks(range(len(grouped)))
    ax.set_xticklabels(grouped.index, rotation=0, fontsize=9)
    ax.set_title(title, fontsize=11)
    ax.set_xlabel('相似度区间')
    ax.set_ylabel('回流率 (%)')
    ax.set_ylim(0, max(grouped['回流率'] * 100) * 1.3)

# 清理临时列
if '_group' in df.columns:
    df.drop(columns=['_group'], inplace=True)

# 隐藏多余的子图
for i in range(len(valid_cols), 8):
    axes2[i].axis('off')

plt.suptitle('相似度 vs 回流率', fontsize=14, fontweight='bold')
plt.tight_layout()
plt.savefig(output_dir / '相似度vs回流率.png', dpi=150, bbox_inches='tight')
print("已保存: 相似度vs回流率.png")

# ============================================================
# 图3: 相似度相关性热力图
# ============================================================
sim_cols_exist = [col for col, _ in valid_cols if col in df.columns]
if len(sim_cols_exist) >= 2:
    fig3, ax3 = plt.subplots(figsize=(10, 8))

    # 计算相关性矩阵
    corr_data = df[sim_cols_exist].copy()
    corr_data['回流率'] = df['再分享回流uv'] / (df['点击uv'] + 1)
    corr_matrix = corr_data.corr()

    # 重命名列
    rename_map = {col: label for col, label in valid_cols}
    rename_map['回流率'] = '回流率'
    corr_matrix = corr_matrix.rename(index=rename_map, columns=rename_map)

    # 绘制热力图
    im = ax3.imshow(corr_matrix, cmap='RdYlBu_r', aspect='auto', vmin=-1, vmax=1)

    # 添加数值标签
    for i in range(len(corr_matrix)):
        for j in range(len(corr_matrix)):
            text = ax3.text(j, i, f'{corr_matrix.iloc[i, j]:.2f}',
                           ha='center', va='center', fontsize=9,
                           color='white' if abs(corr_matrix.iloc[i, j]) > 0.5 else 'black')

    ax3.set_xticks(range(len(corr_matrix)))
    ax3.set_yticks(range(len(corr_matrix)))
    ax3.set_xticklabels(corr_matrix.columns, rotation=45, ha='right', fontsize=10)
    ax3.set_yticklabels(corr_matrix.index, fontsize=10)

    plt.colorbar(im, ax=ax3, label='相关系数')
    plt.title('相似度指标相关性热力图', fontsize=14, fontweight='bold')
    plt.tight_layout()
    plt.savefig(output_dir / '相似度相关性.png', dpi=150, bbox_inches='tight')
    print("已保存: 相似度相关性.png")

# ============================================================
# 图4: 其他维度效果对比
# ============================================================
fig4, axes4 = plt.subplots(2, 2, figsize=(14, 10))

# 4.1 传播性判断 vs 回流率
ax = axes4[0, 0]
if '传播性判断' in df.columns:
    spread_stats = df.groupby('传播性判断').agg({
        '点击uv': 'sum',
        '再分享回流uv': 'sum'
    })
    spread_stats['回流率'] = spread_stats['再分享回流uv'] / (spread_stats['点击uv'] + 10) * 100
    spread_stats = spread_stats.sort_values('回流率', ascending=True)

    colors = plt.cm.Greens(np.linspace(0.3, 0.9, len(spread_stats)))
    bars = ax.barh(range(len(spread_stats)), spread_stats['回流率'], color=colors)
    ax.set_yticks(range(len(spread_stats)))
    ax.set_yticklabels(spread_stats.index)
    ax.set_xlabel('回流率 (%)')
    ax.set_title('AI传播性判断 vs 回流率')

    for bar, val in zip(bars, spread_stats['回流率']):
        ax.text(val + 0.3, bar.get_y() + bar.get_height()/2,
                f'{val:.1f}%', va='center', fontsize=10)

# 4.2 片尾引导 vs 回流率
ax = axes4[0, 1]
if '是否有片尾引导' in df.columns:
    guide_stats = df.groupby('是否有片尾引导').agg({
        '点击uv': 'sum',
        '再分享回流uv': 'sum'
    })
    guide_stats['回流率'] = guide_stats['再分享回流uv'] / (guide_stats['点击uv'] + 10) * 100

    colors = ['#ff7f0e', '#2ca02c']
    bars = ax.bar(range(len(guide_stats)), guide_stats['回流率'], color=colors[:len(guide_stats)])
    ax.set_xticks(range(len(guide_stats)))
    ax.set_xticklabels(guide_stats.index)
    ax.set_ylabel('回流率 (%)')
    ax.set_title('片尾引导 vs 回流率')

    for bar, val in zip(bars, guide_stats['回流率']):
        ax.text(bar.get_x() + bar.get_width()/2, val + 0.3,
                f'{val:.1f}%', ha='center', fontsize=11)

# 4.3 情感倾向 vs 回流率
ax = axes4[1, 0]
if '情感倾向' in df.columns:
    emotion_stats = df.groupby('情感倾向').agg({
        '点击uv': 'sum',
        '再分享回流uv': 'sum'
    })
    emotion_stats['回流率'] = emotion_stats['再分享回流uv'] / (emotion_stats['点击uv'] + 10) * 100
    emotion_stats = emotion_stats.sort_values('回流率', ascending=True).tail(6)

    colors = plt.cm.Purples(np.linspace(0.3, 0.9, len(emotion_stats)))
    bars = ax.barh(range(len(emotion_stats)), emotion_stats['回流率'], color=colors)
    ax.set_yticks(range(len(emotion_stats)))
    ax.set_yticklabels(emotion_stats.index)
    ax.set_xlabel('回流率 (%)')
    ax.set_title('情感倾向 vs 回流率')

    for bar, val in zip(bars, emotion_stats['回流率']):
        ax.text(val + 0.3, bar.get_y() + bar.get_height()/2,
                f'{val:.1f}%', va='center', fontsize=10)

# 4.4 视频风格 vs 回流率 (Top 10)
ax = axes4[1, 1]
if '视频风格' in df.columns:
    style_stats = df.groupby('视频风格').agg({
        '点击uv': 'sum',
        '再分享回流uv': 'sum'
    })
    style_stats['回流率'] = style_stats['再分享回流uv'] / (style_stats['点击uv'] + 10) * 100
    style_stats = style_stats[style_stats['点击uv'] > 5000]  # 过滤小样本
    style_stats = style_stats.sort_values('回流率', ascending=True).tail(10)

    colors = plt.cm.Oranges(np.linspace(0.3, 0.9, len(style_stats)))
    bars = ax.barh(range(len(style_stats)), style_stats['回流率'], color=colors)
    ax.set_yticks(range(len(style_stats)))
    ax.set_yticklabels(style_stats.index)
    ax.set_xlabel('回流率 (%)')
    ax.set_title('视频风格 vs 回流率 (Top 10)')

    for bar, val in zip(bars, style_stats['回流率']):
        ax.text(val + 0.3, bar.get_y() + bar.get_height()/2,
                f'{val:.1f}%', va='center', fontsize=10)

plt.suptitle('内容特征 vs 回流率', fontsize=14, fontweight='bold')
plt.tight_layout()
plt.savefig(output_dir / '内容特征vs回流率.png', dpi=150, bbox_inches='tight')
print("已保存: 内容特征vs回流率.png")

# ============================================================
# 图5: 综合散点图（相似度 vs 回流率）
# ============================================================
fig5, ax5 = plt.subplots(figsize=(12, 8))

# 选择覆盖率最高的两个相似度指标
main_cols = [col for col, _ in valid_cols if df[col].notna().mean() > 0.3][:2]

if len(main_cols) >= 1:
    # 采样数据（避免点太多）
    sample_df = df[df[main_cols[0]].notna()].sample(min(2000, len(df)))
    sample_df['回流率'] = sample_df['再分享回流uv'] / (sample_df['点击uv'] + 1)

    # 绘制散点图
    scatter = ax5.scatter(
        sample_df[main_cols[0]],
        sample_df['回流率'] * 100,
        c=sample_df['点击uv'],
        cmap='viridis',
        alpha=0.5,
        s=20
    )

    # 添加趋势线
    z = np.polyfit(sample_df[main_cols[0]], sample_df['回流率'] * 100, 1)
    p = np.poly1d(z)
    x_line = np.linspace(sample_df[main_cols[0]].min(), sample_df[main_cols[0]].max(), 100)
    ax5.plot(x_line, p(x_line), 'r--', linewidth=2, label=f'趋势线')

    plt.colorbar(scatter, label='点击UV')
    ax5.set_xlabel(dict(valid_cols).get(main_cols[0], main_cols[0]), fontsize=12)
    ax5.set_ylabel('回流率 (%)', fontsize=12)
    ax5.set_title('相似度 vs 回流率 散点图', fontsize=14, fontweight='bold')
    ax5.legend()

plt.tight_layout()
plt.savefig(output_dir / '相似度散点图.png', dpi=150, bbox_inches='tight')
print("已保存: 相似度散点图.png")

plt.close('all')
print(f"\n所有图表已保存到: {output_dir}")
