#!/usr/bin/env python
# coding=utf-8
"""
V2 最终报告：修正后的分析结论
"""
import pandas as pd
import json
from pathlib import Path
import glob

output_dir = Path(__file__).parent / "output"

# 读取数据
data_dir = output_dir / "v2_step2_统一口径"
all_files = glob.glob(str(data_dir / "*.csv"))
df = pd.concat([pd.read_csv(f) for f in all_files], ignore_index=True)

# 计算关键指标
ros_copc = df['real_ros'].sum() / df['pred_ros'].sum()
str_copc = df['real_str'].sum() / df['pred_str'].sum()

# 预估排名与真实排名
df['pred_rank'] = df['pred_score'].rank(ascending=False)
df['real_rank'] = df['real_score'].rank(ascending=False)
rank_corr = df['pred_rank'].corr(df['real_rank'])

# 问题视频
top_25_pct = df['pred_rank'].quantile(0.25)
bottom_50_pct = df['real_rank'].quantile(0.50)
df['is_problem'] = (df['pred_rank'] <= top_25_pct) & (df['real_rank'] > bottom_50_pct)
problem_ratio = df['is_problem'].mean() * 100

# 按真实ROS分组
ros_groups = []
for label, low, high in [('<2', 0, 2), ('2-4', 2, 4), ('4-6', 4, 6), ('>6', 6, 100)]:
    subset = df[(df['real_ros'] >= low) & (df['real_ros'] < high)]
    if len(subset) > 0:
        ros_groups.append({
            'group': label,
            'count': len(subset),
            'pred_ros': round(subset['pred_ros'].mean(), 2),
            'real_ros': round(subset['real_ros'].mean(), 2),
            'bias': round(subset['ros_bias_pct'].mean(), 1)
        })

html_content = f'''<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>低VoV高曝光问题分析报告 V2（修正版）</title>
    <style>
        * {{ margin: 0; padding: 0; box-sizing: border-box; }}
        body {{ font-family: -apple-system, BlinkMacSystemFont, sans-serif; background: #f0f2f5; padding: 40px; }}
        .report {{ max-width: 900px; margin: 0 auto; background: white; border-radius: 16px; overflow: hidden; box-shadow: 0 4px 20px rgba(0,0,0,0.1); }}
        .header {{ background: linear-gradient(135deg, #2c3e50 0%, #3498db 100%); color: white; padding: 40px; text-align: center; }}
        .header h1 {{ font-size: 28px; margin-bottom: 8px; }}
        .section {{ padding: 30px 40px; border-bottom: 1px solid #eee; }}
        .section:last-child {{ border-bottom: none; }}
        h2 {{ color: #2c3e50; margin-bottom: 20px; font-size: 20px; display: flex; align-items: center; gap: 10px; }}
        .step-num {{ background: #3498db; color: white; width: 32px; height: 32px; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-size: 14px; }}
        .old-new {{ display: grid; grid-template-columns: 1fr 1fr; gap: 20px; margin: 20px 0; }}
        .old {{ background: #ffebee; padding: 20px; border-radius: 8px; border-left: 4px solid #e74c3c; }}
        .new {{ background: #e8f5e9; padding: 20px; border-radius: 8px; border-left: 4px solid #27ae60; }}
        .old h3 {{ color: #c0392b; margin-bottom: 10px; }}
        .new h3 {{ color: #27ae60; margin-bottom: 10px; }}
        .metric-grid {{ display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px; margin: 20px 0; }}
        .metric {{ background: #f8f9fa; padding: 20px; border-radius: 8px; text-align: center; }}
        .metric-value {{ font-size: 28px; font-weight: bold; color: #2c3e50; }}
        .metric-label {{ font-size: 14px; color: #666; margin-top: 4px; }}
        table {{ width: 100%; border-collapse: collapse; margin: 20px 0; }}
        th, td {{ padding: 12px; text-align: center; border-bottom: 1px solid #eee; }}
        th {{ background: #f8f9fa; color: #555; font-weight: 600; }}
        tr.highlight {{ background: #fff3cd; }}
        .badge {{ display: inline-block; padding: 4px 12px; border-radius: 20px; font-size: 12px; }}
        .badge-danger {{ background: #ffebee; color: #c0392b; }}
        .badge-success {{ background: #e8f5e9; color: #27ae60; }}
        .badge-warning {{ background: #fff3cd; color: #856404; }}
        .conclusion {{ background: #2c3e50; color: white; padding: 40px; }}
        .conclusion h2 {{ color: white; }}
        .conclusion ul {{ margin-left: 20px; }}
        .conclusion li {{ margin-bottom: 12px; line-height: 1.8; }}
        .highlight-text {{ background: rgba(255,255,255,0.2); padding: 2px 8px; border-radius: 4px; }}
        .correction {{ background: #fff3cd; border: 2px solid #ffc107; border-radius: 8px; padding: 20px; margin: 20px 0; }}
        .correction h3 {{ color: #856404; margin-bottom: 10px; }}
    </style>
</head>
<body>
    <div class="report">
        <div class="header">
            <h1>📊 低VoV高曝光问题分析报告</h1>
            <p>V2 修正版 | 自我批判迭代后的严谨结论</p>
        </div>

        <!-- 修正说明 -->
        <div class="section">
            <h2><span class="step-num">!</span> 重要修正</h2>
            <div class="correction">
                <h3>⚠️ V1 版本存在的问题</h3>
                <ul style="margin-left:20px;">
                    <li><strong>问题定义拍脑袋</strong>：vov < 0.35 阈值无依据</li>
                    <li><strong>数据口径混乱</strong>：用两张表的数据混合分析</li>
                    <li><strong>COPC结论错误</strong>：说"ROS偏高72%"实际上是极端值拉高的均值</li>
                    <li><strong>夸大问题</strong>：说43.5%存在问题，实际只有5.8%</li>
                </ul>
            </div>
        </div>

        <!-- Step 1: 修正问题定义 -->
        <div class="section">
            <h2><span class="step-num">1</span> 修正问题定义</h2>
            <div class="old-new">
                <div class="old">
                    <h3>❌ 旧定义</h3>
                    <p>vov < 0.35 且 rank ≤ 5</p>
                    <p style="color:#666;font-size:14px;">阈值拍脑袋定的</p>
                </div>
                <div class="new">
                    <h3>✓ 新定义</h3>
                    <p>预估排名 Top 25% 但真实排名 Bottom 50%</p>
                    <p style="color:#666;font-size:14px;">数据驱动，定义"排序失准"</p>
                </div>
            </div>
        </div>

        <!-- Step 2: 修正影响面 -->
        <div class="section">
            <h2><span class="step-num">2</span> 修正影响面</h2>
            <div class="old-new">
                <div class="old">
                    <h3>❌ 旧结论</h3>
                    <p style="font-size:24px;font-weight:bold;">43.5%</p>
                    <p style="color:#666;">Top10记录存在问题</p>
                </div>
                <div class="new">
                    <h3>✓ 新结论</h3>
                    <p style="font-size:24px;font-weight:bold;">{problem_ratio:.1f}%</p>
                    <p style="color:#666;">排序失准的视频占比</p>
                </div>
            </div>
            <p style="color:#666;margin-top:12px;">问题规模比之前估计的小很多</p>
        </div>

        <!-- Step 3: 修正COPC结论 -->
        <div class="section">
            <h2><span class="step-num">3</span> 修正 COPC 结论</h2>
            <div class="old-new">
                <div class="old">
                    <h3>❌ 旧结论</h3>
                    <p>ROS 整体偏高 72%</p>
                    <p style="color:#666;font-size:14px;">这是样本偏差均值，被极端值拉高</p>
                </div>
                <div class="new">
                    <h3>✓ 新结论</h3>
                    <p>ROS COPC = {ros_copc:.2f}</p>
                    <p style="color:#666;font-size:14px;">整体预估偏低，不是偏高</p>
                </div>
            </div>

            <h3 style="margin-top:24px;margin-bottom:12px;">真正的问题：回归均值</h3>
            <table>
                <thead>
                    <tr><th>真实ROS</th><th>样本数</th><th>预估ROS</th><th>真实ROS</th><th>偏差</th></tr>
                </thead>
                <tbody>
                    {"".join([f'<tr class="{"highlight" if g["bias"] > 50 else ""}">'
                              f'<td>{g["group"]}</td>'
                              f'<td>{g["count"]}</td>'
                              f'<td>{g["pred_ros"]}</td>'
                              f'<td>{g["real_ros"]}</td>'
                              f'<td><span class="badge {"badge-danger" if g["bias"] > 50 else "badge-warning" if g["bias"] > 0 else "badge-success"}">{g["bias"]:+.1f}%</span></td>'
                              f'</tr>' for g in ros_groups])}
                </tbody>
            </table>
            <p style="color:#666;">真正的问题：<strong>极端值预估不准</strong>，低ROS偏高估，高ROS偏低估</p>
        </div>

        <!-- Step 4: 排序效果 -->
        <div class="section">
            <h2><span class="step-num">4</span> 排序效果评估</h2>
            <div class="metric-grid">
                <div class="metric">
                    <div class="metric-value">{rank_corr:.2f}</div>
                    <div class="metric-label">预估排名与真实排名相关系数</div>
                </div>
                <div class="metric">
                    <div class="metric-value">{problem_ratio:.1f}%</div>
                    <div class="metric-label">严重排序失准比例</div>
                </div>
                <div class="metric">
                    <div class="metric-value">{len(df)}</div>
                    <div class="metric-label">分析样本数</div>
                </div>
            </div>
            <p style="color:#666;">相关系数 0.40 说明排序有改进空间，但不是"严重失效"</p>
        </div>

        <!-- 修正后的结论 -->
        <div class="conclusion">
            <h2>📋 修正后的结论</h2>
            <ul>
                <li><strong>问题规模</strong>：约 <span class="highlight-text">{problem_ratio:.1f}%</span> 的视频存在排序失准，比之前估计的小</li>
                <li><strong>整体COPC</strong>：ROS预估整体偏<span class="highlight-text">低</span>（COPC={ros_copc:.2f}），不是偏高</li>
                <li><strong>真正问题</strong>：模型存在<span class="highlight-text">回归均值</span>问题，极端值预估不准</li>
                <li><strong>排序效果</strong>：相关系数 {rank_corr:.2f}，有改进空间但非灾难性</li>
            </ul>

            <h3 style="margin-top:30px;margin-bottom:16px;">💡 可执行的建议</h3>
            <ul>
                <li><strong>短期</strong>：对预估 ROS < 2 且 STR < P25 的视频增加不确定性惩罚</li>
                <li><strong>中期</strong>：在模型训练时增加极端值样本权重，改善尾部预估</li>
                <li><strong>监控</strong>：增加"预估排名vs真实排名相关系数"的日常监控</li>
                <li><strong>注意</strong>：<del>对真实ROS<2的视频衰减</del> 不可行（事先不知道真实值）</li>
            </ul>
        </div>
    </div>
</body>
</html>
'''

# 保存
html_path = output_dir / "v2_分析报告.html"
with open(html_path, 'w', encoding='utf-8') as f:
    f.write(html_content)

print(f"V2 修正版报告已生成: {html_path}")
