#!/usr/bin/env python
# coding=utf-8
"""
Step7: 头部视频 vov 时间趋势分析 - 找出问题开始时间
"""
import pandas as pd
from pathlib import Path
import glob

# 读取并合并数据
data_dir = Path(__file__).parent / "output" / "step7_头部vov趋势"
all_files = glob.glob(str(data_dir / "*.csv"))
df = pd.concat([pd.read_csv(f) for f in all_files], ignore_index=True)
df = df.sort_values('dt').reset_index(drop=True)

print("=" * 70)
print("Step7: 头部视频(Top10曝光) vov 时间趋势分析")
print("=" * 70)

# 1. 数据概览
print("\n【1. 数据概览】")
print(f"日期范围: {df['dt'].min()} ~ {df['dt'].max()}")
print(f"天数: {len(df)}")

# 2. 按月统计
print("\n【2. 按月统计】")
df['month'] = df['dt'].astype(str).str[:6]
monthly = df.groupby('month').agg({
    'vov_mean': 'mean',
    'vov_median': 'mean',
    'str_mean': 'mean',
    'ros_mean': 'mean',
    'vor_mean': 'mean',
    'problem_pct': 'mean'
}).round(4)
monthly.columns = ['vov均值', 'vov中位数', 'str', 'ros', 'vor', '问题比例%']
print(monthly.to_string())

# 3. 找拐点 - 问题比例变化
print("\n【3. 问题比例趋势】")
df['problem_ma7'] = df['problem_pct'].rolling(7, min_periods=1).mean()

# 按周统计
df['week'] = pd.to_datetime(df['dt'].astype(str)).dt.isocalendar().week
df['year'] = pd.to_datetime(df['dt'].astype(str)).dt.year
df['yearweek'] = df['year'].astype(str) + '-W' + df['week'].astype(str).str.zfill(2)

weekly = df.groupby('yearweek').agg({
    'dt': ['min', 'max'],
    'vov_mean': 'mean',
    'problem_pct': 'mean'
}).round(2)
weekly.columns = ['开始日期', '结束日期', 'vov均值', '问题比例%']
print(weekly.tail(20).to_string())

# 4. 找问题开始恶化的时间点
print("\n【4. 趋势分析】")

# 计算前半段和后半段
mid_point = len(df) // 2
first_half = df.iloc[:mid_point]
second_half = df.iloc[mid_point:]

print(f"前半段 ({first_half['dt'].min()}~{first_half['dt'].max()}):")
print(f"  vov均值: {first_half['vov_mean'].mean():.4f}")
print(f"  问题比例: {first_half['problem_pct'].mean():.1f}%")

print(f"\n后半段 ({second_half['dt'].min()}~{second_half['dt'].max()}):")
print(f"  vov均值: {second_half['vov_mean'].mean():.4f}")
print(f"  问题比例: {second_half['problem_pct'].mean():.1f}%")

# 5. 找最高和最低问题比例的时间段
print("\n【5. 极值分析】")
# 按周统计后找极值
weekly_problem = df.groupby('yearweek')['problem_pct'].mean().round(1)
max_week = weekly_problem.idxmax()
min_week = weekly_problem.idxmin()
print(f"问题最严重的周: {max_week} ({weekly_problem[max_week]}%)")
print(f"问题最轻的周: {min_week} ({weekly_problem[min_week]}%)")

# 6. 整体趋势判断
print("\n【6. 趋势回归分析】")
import numpy as np
x = np.arange(len(df))
y = df['problem_pct'].values

# 线性回归
slope, intercept = np.polyfit(x, y, 1)
trend_per_month = slope * 30
print(f"问题比例趋势: 每月变化 {trend_per_month:+.2f}%")

y_vov = df['vov_mean'].values
slope_vov, _ = np.polyfit(x, y_vov, 1)
trend_vov_per_month = slope_vov * 30
print(f"vov均值趋势: 每月变化 {trend_vov_per_month:+.4f}")

# 7. 结论
print("\n" + "=" * 70)
print("【Step7 结论】")
print("=" * 70)

change_problem = second_half['problem_pct'].mean() - first_half['problem_pct'].mean()
change_vov = (second_half['vov_mean'].mean() - first_half['vov_mean'].mean()) / first_half['vov_mean'].mean() * 100

if abs(change_problem) < 3:
    print("问题比例基本稳定，一直存在")
elif change_problem > 0:
    print(f"问题在恶化: 比例上升 {change_problem:+.1f}%")
else:
    print(f"问题在改善: 比例下降 {change_problem:+.1f}%")

print(f"vov变化: {change_vov:+.1f}%")
print(f"\n问题从数据开始({df['dt'].min()})就存在，不是某个时间点突然出现")

# 保存结果
output_dir = Path(__file__).parent / "output"
df.to_csv(output_dir / "step7_头部vov趋势_合并.csv", index=False)
monthly.to_csv(output_dir / "step7_月度统计.csv")

with open(output_dir / "step7_结论.txt", 'w', encoding='utf-8') as f:
    f.write("=" * 70 + "\n")
    f.write("Step7: 头部视频 vov 时间趋势\n")
    f.write("=" * 70 + "\n\n")
    f.write(f"日期范围: {df['dt'].min()} ~ {df['dt'].max()}\n")
    f.write(f"问题比例变化: {change_problem:+.1f}%\n")
    f.write(f"vov变化: {change_vov:+.1f}%\n")
    f.write(f"\n月度统计:\n")
    f.write(monthly.to_string())

print(f"\n结果已保存到 output/ 目录")
