#!/usr/bin/env python3
"""
社交媒体账号综合评估主脚本

整合：活跃度 + 内容 + 网络 + 主题叙事 + 粉丝风险 + 报告生成
"""

import json
import sys
from pathlib import Path
from datetime import datetime
from typing import List, Dict, Any

# 添加脚本目录到路径
sys.path.insert(0, str(Path(__file__).parent))

from evaluate_account import evaluate_social_account_comprehensive
from generate_report import generate_full_report


def evaluate_account_with_report(
    account_info: Dict[str, Any],
    tweets: List[Dict[str, Any]],
    network_history: List[Dict[str, Any]] = None,
    following_accounts: List[Dict[str, Any]] = None,
    followers_sample: List[Dict[str, Any]] = None,
    interactions: List[Dict[str, Any]] = None,
    days_back: int = 90
) -> Dict[str, Any]:
    """
    社交媒体账号综合得评主入口

    Args:
        account_info: 账号信息
        tweets: 推文列表
        network_history: 网络历史数据
        following_accounts: 关注的账号列表
        followers_sample: 粉丝样本
        interactions: 交互记录
        days_back: 分析天数

    Returns:
        Dict: 综合评估结果
            - account_info: 账号信息
            - analysis_summary: 分析摘要
            - detailed_analysis: 详细分析结果
            - recommendations: 建议
            - report: Markdown报告
            - generated_at: 生成时间
    """
    # 执行综合评估
    evaluation_result = evaluate_social_account_comprehensive(
        account_info=account_info,
        tweets=tweets,
        network_history=network_history,
        following_accounts=following_accounts,
        followers_sample=followers_sample,
        interactions=interactions,
        days_back=days_back
    )

    # 生成报告
    report = generate_full_report(evaluation_result)

    # 添加报告到结果中
    evaluation_result["report"] = report

    return evaluation_result


def main():
    """命令行入口"""
    if len(sys.argv) > 1:
        # 从文件读取
        with open(sys.argv[1], 'r', encoding='utf-8') as f:
            input_data = json.load(f)
    else:
        # 使用示例数据
        input_data = {
            "account_info": {
                "id": "elonmusk",
                "username": "Elon Musk",
                "bio": "Mars, cars, chips & rockets",
                "location": "Austin, Texas",
                "created_at": "2009-06-02",
                "followers_count": 150000000,
                "following_count": 100,
                "verified": True,
            },
            "tweets": [
                {
                    "id": "1",
                    "text": "Exciting announcement about climate policy #policy #climate",
                    "created_at": "2026-04-01T10:00:00Z",
                    "like_count": 50000,
                    "retweet_count": 20000,
                    "reply_count": 5000,
                },
                {
                    "id": "2",
                    "text": "We need to act on climate change immediately #climate #urgent",
                    "created_at": "2026-04-01T11:00:00Z",
                    "like_count": 40000,
                    "retweet_count": 15000,
                    "reply_count": 4000,
                },
                {
                    "id": "3",
                    "text": "The new climate policy is terrible for our economy #policy",
                    "created_at": "2026-04-01T12:00:00Z",
                    "like_count": 30000,
                    "retweet_count": 10000,
                    "reply_count": 3000,
                },
                {
                    "id": "4",
                    "text": "Climate action is growing across society #support",
                    "created_at": "2026-03-20T10:00:00Z",
                    "like_count": 25000,
                    "retweet_count": 8000,
                    "reply_count": 2000,
                },
                {
                    "id": "5",
                    "text": "We must oppose current policy #policy",
                    "created_at": "2026-03-20T11:00:00Z",
                    "like_count": 20000,
                    "retweet_count": 6000,
                    "reply_count": 1500,
                },
            ],
            "network_history": [
                {"date": "2026-03-01", "followers_count": 148000000, "following_count": 100},
                {"date": "2026-03-15", "followers_count": 149000000, "following_count": 100},
                {"date": "2026-03-30", "followers_count": 149500000, "following_count": 100},
                {"date": "2026-04-01", "followers_count": 150000000, "following_count": 100},
            ],
            "following_accounts": [
                {
                    "id": "1",
                    "username": "news_org",
                    "bio": "Breaking news from around the world",
                    "followers_count": 500000,
                    "verified": True
                },
                {
                    "id": "2",
                    "username": "gov_official",
                    "bio": "Official government account",
                    "followers_count": 1000000,
                    "verified": True
                },
            ],
            "followers_sample": [
                {
                    "id": "1",
                    "username": "real_user",
                    "followers_count": 10000,
                    "following_count": 500,
                    "verified": True,
                    "default_profile_image": False,
                    "description": "Real user with bio"
                },
                {
                    "id": "2",
                    "username": "suspect_bot",
                    "followers_count": 10,
                    "following_count": 5000,
                    "verified": False,
                    "default_profile_image": True,
                    "description": ""
                },
            ],
            "interactions": [
                {
                    "user_id": "1",
                    "username": "frequent_interactor",
                    "interaction_type": "retweet",
                    "count": 50
                },
                {
                    "user_id": "2",
                    "username": "dialogue_partner",
                    "interaction_type": "reply",
                    "count": 30
                },
            ],
            "days_back": 90
        }

    # 执行评估
    result = evaluate_account_with_report(
        account_info=input_data.get("account_info", {}),
        tweets=input_data.get("tweets", []),
        network_history=input_data.get("network_history"),
        following_accounts=input_data.get("following_accounts"),
        followers_sample=input_data.get("followers_sample"),
        interactions=input_data.get("interactions"),
        days_back=input_data.get("days_back", 90)
    )

    # 输出报告
    print(result["report"])

    # 如果需要完整JSON，取消注释以下内容
    # print("\n" + "="*80 + "\n")
    # print("完整的JSON结果：")
    # print(json.dumps(result, ensure_ascii=False, indent=2, default=str))


if __name__ == "__main__":
    main()
