#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
分类树可视化执行脚本
"""
import os
import sys
from pathlib import Path

# 添加当前目录到路径
current_dir = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, current_dir)

from classification_tree_visualizer import visualize_classification_tree


def main():
    # 设置路径
    base_dir = current_dir

    # 必需的文件
    optimized_data_path = os.path.join(base_dir, "optimized_clustered_data_gemini-3-pro-preview.json")
    posts_dir = base_dir  # 当前目录作为帖子目录

    # 可选的关联分析文件
    dimension_associations_path = os.path.join(base_dir, "dimension_associations_analysis.json")
    intra_dimension_associations_path = os.path.join(base_dir, "intra_dimension_associations_analysis.json")
    expanded_orthogonal_combinations_path = os.path.join(base_dir, "expanded_orthogonal_combinations.json")
    enriched_xuanti_point_map_path = os.path.join(base_dir, "enriched_xuanti_point_map.json")

    # 检查必需文件是否存在
    if not os.path.exists(optimized_data_path):
        print(f"❌ 找不到优化数据文件: {optimized_data_path}")
        return

    print(f"📂 优化数据文件: {optimized_data_path}")
    print(f"📂 帖子目录: {posts_dir}")

    # 检查可选文件
    if os.path.exists(dimension_associations_path):
        print(f"✅ 找到跨维度关联分析数据")
    if os.path.exists(intra_dimension_associations_path):
        print(f"✅ 找到维度内部关联分析数据")
    if os.path.exists(expanded_orthogonal_combinations_path):
        print(f"✅ 找到扩展正交组合数据")
    if os.path.exists(enriched_xuanti_point_map_path):
        print(f"✅ 找到丰富选题点映射数据")

    # xuanti_point_map 使用空字典（如果没有外部依赖）
    xuanti_point_map = {}

    print("\n🚀 开始生成可视化...")

    try:
        output_path = visualize_classification_tree(
            optimized_data_path=optimized_data_path,
            posts_dir=posts_dir,
            xuanti_point_map=xuanti_point_map,
            dimension_associations_path=dimension_associations_path if os.path.exists(dimension_associations_path) else None,
            intra_dimension_associations_path=intra_dimension_associations_path if os.path.exists(intra_dimension_associations_path) else None,
            expanded_orthogonal_combinations_path=expanded_orthogonal_combinations_path if os.path.exists(expanded_orthogonal_combinations_path) else None,
            enriched_xuanti_point_map_path=enriched_xuanti_point_map_path if os.path.exists(enriched_xuanti_point_map_path) else None
        )

        print(f"\n🎉 可视化完成！")
        print(f"📄 输出文件: {output_path}")
        print(f"\n💡 请在浏览器中打开: file://{output_path}")

    except Exception as e:
        print(f"\n❌ 生成可视化失败: {e}")
        import traceback
        traceback.print_exc()


if __name__ == "__main__":
    main()
