"""
航空公司情感分析数据概览模块
提供数据加载、探索性分析和可视化功能
"""

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots
from pathlib import Path
import warnings
warnings.filterwarnings('ignore')

# 设置中文字体支持
plt.rcParams['font.sans-serif'] = ['SimHei', 'Microsoft YaHei']
plt.rcParams['axes.unicode_minus'] = False

class DataOverview:
    """数据概览分析类"""
    
    def __init__(self, data_path="data/Tweets.csv"):
        """
        初始化数据概览分析器
        
        Args:
            data_path: 数据文件路径
        """
        self.data_path = Path(data_path)
        self.df = None
        self.load_data()
        
    def load_data(self):
        """加载数据"""
        try:
            # 尝试不同的路径格式
            if self.data_path.exists():
                self.df = pd.read_csv(self.data_path)
            else:
                # 尝试相对路径
                alt_path = Path("../data/Tweets.csv")
                if alt_path.exists():
                    self.df = pd.read_csv(alt_path)
                else:
                    # 尝试绝对路径
                    alt_path = Path("d:/HuaweiMoveData/Users/马艺洁/Desktop/MLwork/bigwork/data/Tweets.csv")
                    if alt_path.exists():
                        self.df = pd.read_csv(alt_path)
                    else:
                        raise FileNotFoundError(f"找不到数据文件: {self.data_path}")
            
            print(f"✅ 成功加载数据，共 {len(self.df)} 条记录")
            
        except Exception as e:
            print(f"❌ 数据加载失败: {e}")
            # 创建示例数据用于演示
            self.create_sample_data()
    
    def create_sample_data(self):
        """创建示例数据用于演示"""
        print("⚠️ 使用示例数据进行演示")
        
        # 创建示例数据
        airlines = ['United', 'US Airways', 'American', 'Southwest', 'Delta', 'Virgin America']
        sentiments = ['positive', 'negative', 'neutral']
        
        sample_data = []
        for i in range(1000):
            airline = np.random.choice(airlines)
            sentiment = np.random.choice(sentiments, p=[0.2, 0.6, 0.2])
            
            sample_data.append({
                'tweet_id': f'tweet_{i}',
                'airline': airline,
                'airline_sentiment': sentiment,
                'airline_sentiment_confidence': np.random.uniform(0.7, 1.0),
                'negativereason': f'reason_{np.random.randint(1, 10)}' if sentiment == 'negative' else None,
                'negativereason_confidence': np.random.uniform(0.5, 1.0) if sentiment == 'negative' else None,
                'retweet_count': np.random.poisson(2),
                'text': f'This is a sample tweet about {airline} airline.',
                'tweet_created': pd.Timestamp.now() - pd.Timedelta(days=np.random.randint(1, 365)),
                'user_timezone': np.random.choice(['Eastern Time', 'Central Time', 'Pacific Time'])
            })
        
        self.df = pd.DataFrame(sample_data)
    
    def get_basic_info(self):
        """获取数据基本信息"""
        info = {
            'total_records': len(self.df),
            'total_columns': len(self.df.columns),
            'data_types': self.df.dtypes.to_dict()
        }
        return info
    
    def get_sentiment_distribution(self):
        """获取情感分布"""
        sentiment_counts = self.df['airline_sentiment'].value_counts()
        sentiment_percentages = (sentiment_counts / len(self.df) * 100).round(2)
        
        return {
            'counts': sentiment_counts.to_dict(),
            'percentages': sentiment_percentages.to_dict()
        }
    
    def get_airline_distribution(self):
        """获取航空公司分布"""
        airline_counts = self.df['airline'].value_counts()
        airline_percentages = (airline_counts / len(self.df) * 100).round(2)
        
        return {
            'counts': airline_counts.to_dict(),
            'percentages': airline_percentages.to_dict()
        }
    
    def get_negative_reasons(self):
        """获取负面原因分布"""
        if 'negativereason' in self.df.columns:
            negative_df = self.df[self.df['airline_sentiment'] == 'negative']
            reason_counts = negative_df['negativereason'].value_counts()
            return reason_counts.to_dict()
        return {}
    
    def create_sentiment_by_airline_chart(self):
        """创建航空公司情感分布图表"""
        # 创建交叉表
        cross_tab = pd.crosstab(self.df['airline'], self.df['airline_sentiment'])
        
        # 创建Plotly图表
        fig = px.bar(cross_tab, 
                     barmode='group',
                     title='航空公司情感分布',
                     labels={'value': '数量', 'airline': '航空公司', 'airline_sentiment': '情感'})
        
        fig.update_layout(
            xaxis_title='航空公司',
            yaxis_title='推文数量',
            legend_title='情感类型'
        )
        
        return fig
    
    def create_sentiment_pie_chart(self):
        """创建情感分布饼图"""
        sentiment_counts = self.df['airline_sentiment'].value_counts()
        
        fig = px.pie(values=sentiment_counts.values, 
                     names=sentiment_counts.index,
                     title='情感分布饼图',
                     color_discrete_sequence=px.colors.qualitative.Set3)
        
        return fig
    

    
    def create_time_series_analysis(self):
        """创建时间序列分析"""
        if 'tweet_created' in self.df.columns:
            # 转换时间列
            df_time = self.df.copy()
            df_time['tweet_created'] = pd.to_datetime(df_time['tweet_created'])
            df_time['date'] = df_time['tweet_created'].dt.date
            
            # 按日期分组
            daily_counts = df_time.groupby(['date', 'airline_sentiment']).size().unstack(fill_value=0)
            
            # 创建时间序列图表
            fig = px.line(daily_counts, 
                         title='每日情感趋势',
                         labels={'value': '推文数量', 'date': '日期'})
            
            fig.update_layout(
                xaxis_title='日期',
                yaxis_title='推文数量',
                legend_title='情感类型'
            )
            
            return fig
        return None
    
    def create_retweet_analysis(self):
        """创建转发分析"""
        if 'retweet_count' in self.df.columns:
            # 按情感分组分析转发
            retweet_stats = self.df.groupby('airline_sentiment')['retweet_count'].agg(['mean', 'median', 'max']).round(2)
            
            # 创建箱线图
            fig = px.box(self.df, 
                        x='airline_sentiment', 
                        y='retweet_count',
                        title='不同情感的转发次数分布',
                        color='airline_sentiment')
            
            fig.update_layout(
                xaxis_title='情感类型',
                yaxis_title='转发次数',
                showlegend=False
            )
            
            return fig, retweet_stats.to_dict()
        return None, {}
    
    def generate_comprehensive_report(self):
        """生成综合报告"""
        report = {
            'basic_info': self.get_basic_info(),
            'sentiment_distribution': self.get_sentiment_distribution(),
            'airline_distribution': self.get_airline_distribution(),
            'negative_reasons': self.get_negative_reasons(),
            'charts': {
                'sentiment_by_airline': self.create_sentiment_by_airline_chart(),
                'sentiment_pie': self.create_sentiment_pie_chart(),
                'time_series': self.create_time_series_analysis(),
                'retweet_analysis': self.create_retweet_analysis()[0]
            }
        }
        
        return report

def show_data_overview():
    """显示数据概览"""
    import streamlit as st
    
    # 航空主题样式 - 使用更强大的CSS选择器
    st.markdown("""
    <style>
        /* 全局样式 */
        [data-testid="stAppViewContainer"] {
            background: linear-gradient(135deg, #e6f7ff 0%, #f0f8ff 100%);
        }
        
        /* 标题样式 */
        [data-testid="stTitle"] {
            color: #0066cc !important;
            font-weight: bold !important;
            text-align: center;
            padding: 20px 0;
        }
        
        /* 子标题样式 */
        .subheader {
            color: #004080 !important;
            border-left: 4px solid #0066cc;
            padding-left: 12px;
            margin: 20px 0 10px 0;
            font-size: 1.5rem !important;
            font-weight: 600 !important;
        }
        
        /* 指标卡片样式 */
        [data-testid="stMetric"] {
            background: white;
            border-radius: 10px;
            padding: 15px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        
        /* 按钮样式 */
        [data-testid="stButton"]>button {
            background-color: #0066cc !important;
            color: white !important;
            border-radius: 8px !important;
            border: none !important;
            padding: 8px 20px !important;
        }
        
        /* 表格样式 */
        [data-testid="stDataFrame"] {
            border-radius: 8px !important;
            overflow: hidden !important;
        }
    </style>
    """, unsafe_allow_html=True)
    
    st.title("✈️ 航空公司情感分析数据概览")
    
    # 初始化数据概览器
    overview = DataOverview()
    
    # 基本信息
    st.markdown("<h2 class='subheader'>📋 数据基本信息</h2>", unsafe_allow_html=True)
    basic_info = overview.get_basic_info()
    
    col1, col2 = st.columns(2)
    with col1:
        st.metric("总样本数", f"{basic_info['total_records']:,}")
    with col2:
        st.metric("特征数量", basic_info['total_columns'])
    
    # 情感分布
    st.markdown("<h2 class='subheader'>😊 情感分布分析</h2>", unsafe_allow_html=True)
    sentiment_info = overview.get_sentiment_distribution()
    
    col1, col2, col3 = st.columns(3)
    for i, (sentiment, count) in enumerate(sentiment_info['counts'].items()):
        with [col1, col2, col3][i]:
            percentage = sentiment_info['percentages'][sentiment]
            st.metric(f"{sentiment.capitalize()}情感", f"{count:,}", f"{percentage}%")
    
    # 情感分布图表
    fig_pie = overview.create_sentiment_pie_chart()
    st.plotly_chart(fig_pie, use_container_width=True)
    
    # 航空公司情感分布
    st.markdown("<h2 class='subheader'>✈️ 航空公司情感分布</h2>", unsafe_allow_html=True)
    fig_airline = overview.create_sentiment_by_airline_chart()
    st.plotly_chart(fig_airline, use_container_width=True)
    
    # 时间序列分析
    st.markdown("<h2 class='subheader'>⏰ 时间趋势分析</h2>", unsafe_allow_html=True)
    fig_time = overview.create_time_series_analysis()
    if fig_time:
        st.plotly_chart(fig_time, use_container_width=True)
    
    # 转发分析
    st.markdown("<h2 class='subheader'>🔄 转发行为分析</h2>", unsafe_allow_html=True)
    fig_retweet, retweet_stats = overview.create_retweet_analysis()
    if fig_retweet:
        st.plotly_chart(fig_retweet, use_container_width=True)
        
        # 显示统计信息
        if retweet_stats:
            st.write("转发统计信息:")
            st.dataframe(pd.DataFrame(retweet_stats).T)
    
    # 负面原因分析
    st.markdown("<h2 class='subheader'>🔍 负面原因分析</h2>", unsafe_allow_html=True)
    negative_reasons = overview.get_negative_reasons()
    if negative_reasons:
        reasons_df = pd.DataFrame(list(negative_reasons.items()), columns=['负面原因', '数量'])
        reasons_df['占比'] = (reasons_df['数量'] / reasons_df['数量'].sum() * 100).round(2)
        st.dataframe(reasons_df, use_container_width=True)
    
    st.success("✅ 数据概览分析完成！")

if __name__ == "__main__":
    # 测试数据概览功能
    overview = DataOverview()
    
    print("=" * 60)
    print("航空公司情感分析数据概览")
    print("=" * 60)
    
    # 基本信息
    basic_info = overview.get_basic_info()
    print(f"\n📊 基本信息:")
    print(f"   总样本数: {basic_info['total_records']:,}")
    print(f"   特征数量: {basic_info['total_columns']}")
    
    # 情感分布
    sentiment_info = overview.get_sentiment_distribution()
    print(f"\n😊 情感分布:")
    for sentiment, count in sentiment_info['counts'].items():
        percentage = sentiment_info['percentages'][sentiment]
        print(f"   {sentiment.capitalize()}: {count:,} ({percentage}%)")
    
    # 航空公司分布
    airline_info = overview.get_airline_distribution()
    print(f"\n✈️ 航空公司分布:")
    for airline, count in airline_info['counts'].items():
        percentage = airline_info['percentages'][airline]
        print(f"   {airline}: {count:,} ({percentage}%)")
    
    print("\n" + "=" * 60)
    print("数据概览测试完成")
    print("=" * 60)