#!/usr/bin/env python3
"""
Tab1内容生成器 - 结构化内容库
包含：选题信息表、模型信息表、层级标签树
"""
import html as html_module
from typing import Dict, Any, List


def render_topic_info_card(topic_info: Dict[str, Any]) -> str:
    """渲染选题信息表卡片"""
    macro_topic = topic_info.get("宏观母题", "")
    sub_topics = topic_info.get("高潜子题列表", [])
    
    html = '<div class="point-card topic-card" data-card-id="topic-info">\n'
    html += '<div class="point-card-header" onclick="toggleCardDetails(\'topic-info\')">\n'
    html += '<span class="point-text">选题信息表</span>\n'
    html += '<span class="toggle-icon">▼</span>\n'
    html += '</div>\n'
    
    html += '<div class="point-card-details" id="topic-info-details">\n'
    
    if macro_topic:
        html += '<div class="detail-section">\n'
        html += '<strong>宏观母题：</strong>\n'
        html += f'<div class="detail-text">{html_module.escape(macro_topic)}</div>\n'
        html += '</div>\n'
    
    if sub_topics:
        html += '<div class="detail-section">\n'
        html += '<strong>高潜子题列表：</strong>\n'
        html += '<ul class="detail-list">\n'
        for sub_topic in sub_topics:
            html += f'<li>{html_module.escape(str(sub_topic))}</li>\n'
        html += '</ul>\n'
        html += '</div>\n'
    else:
        html += '<div class="detail-section">\n'
        html += '<strong>高潜子题列表：</strong> 暂无\n'
        html += '</div>\n'
    
    html += '</div>\n'
    html += '</div>\n'
    return html


def render_model_info_card(model_info: Dict[str, Any]) -> str:
    """渲染模型信息表卡片"""
    logic_models = model_info.get("抽象逻辑模型列表", [])
    
    html = '<div class="point-card model-card" data-card-id="model-info">\n'
    html += '<div class="point-card-header" onclick="toggleCardDetails(\'model-info\')">\n'
    html += '<span class="point-text">模型信息表</span>\n'
    html += '<span class="toggle-icon">▼</span>\n'
    html += '</div>\n'
    
    html += '<div class="point-card-details" id="model-info-details">\n'
    
    if logic_models:
        html += '<div class="detail-section">\n'
        html += '<strong>抽象逻辑模型列表：</strong>\n'
        html += '<ul class="detail-list">\n'
        for model in logic_models:
            html += f'<li class="model-item">{html_module.escape(str(model))}</li>\n'
        html += '</ul>\n'
        html += '</div>\n'
    else:
        html += '<div class="detail-section">\n'
        html += '<strong>抽象逻辑模型列表：</strong> 暂无\n'
        html += '</div>\n'
    
    html += '</div>\n'
    html += '</div>\n'
    return html


def render_label_tree_card(label_tree: Dict[str, Any]) -> str:
    """渲染层级标签树卡片"""
    html = '<div class="point-card label-tree-card" data-card-id="label-tree">\n'
    html += '<div class="point-card-header" onclick="toggleCardDetails(\'label-tree\')">\n'
    html += '<span class="point-text">层级标签树</span>\n'
    html += '<span class="toggle-icon">▼</span>\n'
    html += '</div>\n'
    
    html += '<div class="point-card-details" id="label-tree-details">\n'
    
    # L1_实例名词
    l1_nouns = label_tree.get("L1_实例名词", [])
    if l1_nouns:
        html += '<div class="detail-section">\n'
        html += '<strong>L1_实例名词：</strong>\n'
        html += '<div class="tag-group">\n'
        for noun in l1_nouns:
            html += f'<span class="label-tag l1-tag">{html_module.escape(str(noun))}</span>\n'
        html += '</div>\n'
        html += '</div>\n'
    
    # L1_限定词
    l1_qualifiers = label_tree.get("L1_限定词", [])
    if l1_qualifiers:
        html += '<div class="detail-section">\n'
        html += '<strong>L1_限定词：</strong>\n'
        html += '<div class="tag-group">\n'
        for qualifier in l1_qualifiers:
            html += f'<span class="label-tag l1-tag">{html_module.escape(str(qualifier))}</span>\n'
        html += '</div>\n'
        html += '</div>\n'
    
    # L2_具体品类
    l2_categories = label_tree.get("L2_具体品类", [])
    if l2_categories:
        html += '<div class="detail-section">\n'
        html += '<strong>L2_具体品类：</strong>\n'
        html += '<div class="tag-group">\n'
        for category in l2_categories:
            html += f'<span class="label-tag l2-tag">{html_module.escape(str(category))}</span>\n'
        html += '</div>\n'
        html += '</div>\n'
    
    # L3_兴趣领域
    l3_interests = label_tree.get("L3_兴趣领域", [])
    if l3_interests:
        html += '<div class="detail-section">\n'
        html += '<strong>L3_兴趣领域：</strong>\n'
        html += '<div class="tag-group">\n'
        for interest in l3_interests:
            html += f'<span class="label-tag l3-tag">{html_module.escape(str(interest))}</span>\n'
        html += '</div>\n'
        html += '</div>\n'
    
    # L4_情绪价值
    l4_emotions = label_tree.get("L4_情绪价值", [])
    if l4_emotions:
        html += '<div class="detail-section">\n'
        html += '<strong>L4_情绪价值：</strong>\n'
        html += '<div class="tag-group">\n'
        for emotion in l4_emotions:
            html += f'<span class="label-tag l4-tag">{html_module.escape(str(emotion))}</span>\n'
        html += '</div>\n'
        html += '</div>\n'
    
    html += '</div>\n'
    html += '</div>\n'
    return html


def generate_tab1_content(data: Dict[str, Any]) -> str:
    """生成Tab1内容：结构化内容库"""
    html = '<div class="tab-content" id="tab1">\n'
    html += '<div class="section">\n'
    html += '<h3>结构化内容库</h3>\n'
    
    structured_content = data.get("结构化内容库", {})
    
    if not structured_content:
        html += '<p>暂无数据</p>\n'
    else:
        # 选题信息表
        topic_info = structured_content.get("选题信息表", {})
        if topic_info:
            html += render_topic_info_card(topic_info)
        
        # 模型信息表
        model_info = structured_content.get("模型信息表", {})
        if model_info:
            html += render_model_info_card(model_info)
        
        # 层级标签树
        label_tree = structured_content.get("层级标签树", {})
        if label_tree:
            html += render_label_tree_card(label_tree)
    
    html += '</div>\n'
    html += '</div>\n'
    return html

