#!/usr/bin/env python3
"""
Tab2内容生成器 - L3单元解构
包含：单元列表，每个单元包含单元编号、时间范围、单元核心概括、完整文案、实质
"""
import html as html_module
from typing import Dict, Any, List


def render_unit_card(unit: Dict[str, Any], idx: int) -> str:
    """渲染单元卡片"""
    unit_num = unit.get("单元编号", idx)
    time_range = unit.get("时间范围", "")
    core_summary = unit.get("单元核心概括", "")
    full_text = unit.get("完整文案", "")
    substance = unit.get("实质", {})
    
    card_id = f'unit-{unit_num}'
    
    html = f'<div class="point-card unit-card" data-card-id="{card_id}">\n'
    html += f'<div class="point-card-header" onclick="toggleCardDetails(\'{card_id}\')">\n'
    html += f'<span class="point-number">单元 #{unit_num}</span>\n'
    if time_range:
        html += f'<span class="point-time">{html_module.escape(time_range)}</span>\n'
    if core_summary:
        html += f'<span class="point-text">{html_module.escape(core_summary)}</span>\n'
    html += '<span class="toggle-icon">▼</span>\n'
    html += '</div>\n'
    
    html += f'<div class="point-card-details" id="{card_id}-details">\n'
    
    # 完整文案
    if full_text:
        html += '<div class="detail-section">\n'
        html += '<strong>完整文案：</strong>\n'
        html += f'<div class="detail-text">{html_module.escape(full_text)}</div>\n'
        html += '</div>\n'
    
    # 实质内容
    if substance:
        # 具体元素
        concrete_elements = substance.get("具体元素", {})
        if concrete_elements:
            html += '<div class="detail-section">\n'
            html += '<strong>具体元素：</strong>\n'
            
            keywords = concrete_elements.get("关键词", [])
            if keywords:
                html += '<div class="sub-section">\n'
                html += '<strong>关键词：</strong>\n'
                html += '<div class="tag-group">\n'
                for keyword in keywords:
                    html += f'<span class="keyword-tag">{html_module.escape(str(keyword))}</span>\n'
                html += '</div>\n'
                html += '</div>\n'
            
            forms = concrete_elements.get("对应形式", {})
            if forms:
                html += '<div class="sub-section">\n'
                html += '<strong>对应形式：</strong>\n'
                if forms.get("文案形式"):
                    html += f'<div class="form-item"><strong>文案形式：</strong>{html_module.escape(forms["文案形式"])}</div>\n'
                if forms.get("画面形式"):
                    html += f'<div class="form-item"><strong>画面形式：</strong>{html_module.escape(forms["画面形式"])}</div>\n'
                if forms.get("声音形式"):
                    html += f'<div class="form-item"><strong>声音形式：</strong>{html_module.escape(forms["声音形式"])}</div>\n'
                html += '</div>\n'
            
            html += '</div>\n'
        
        # 具象概念
        concrete_concepts = substance.get("具象概念", {})
        if concrete_concepts:
            html += '<div class="detail-section">\n'
            html += '<strong>具象概念：</strong>\n'
            
            keywords = concrete_concepts.get("关键词", [])
            if keywords:
                html += '<div class="sub-section">\n'
                html += '<strong>关键词：</strong>\n'
                html += '<div class="tag-group">\n'
                for keyword in keywords:
                    html += f'<span class="keyword-tag">{html_module.escape(str(keyword))}</span>\n'
                html += '</div>\n'
                html += '</div>\n'
            
            forms = concrete_concepts.get("对应形式", {})
            if forms:
                html += '<div class="sub-section">\n'
                html += '<strong>对应形式：</strong>\n'
                if forms.get("文案形式"):
                    html += f'<div class="form-item"><strong>文案形式：</strong>{html_module.escape(forms["文案形式"])}</div>\n'
                if forms.get("画面形式"):
                    html += f'<div class="form-item"><strong>画面形式：</strong>{html_module.escape(forms["画面形式"])}</div>\n'
                if forms.get("声音形式"):
                    html += f'<div class="form-item"><strong>声音形式：</strong>{html_module.escape(forms["声音形式"])}</div>\n'
                html += '</div>\n'
            
            html += '</div>\n'
        
        # 抽象概念
        abstract_concepts = substance.get("抽象概念", {})
        if abstract_concepts:
            html += '<div class="detail-section">\n'
            html += '<strong>抽象概念：</strong>\n'
            
            keywords = abstract_concepts.get("关键词", [])
            if keywords:
                html += '<div class="sub-section">\n'
                html += '<strong>关键词：</strong>\n'
                html += '<div class="tag-group">\n'
                for keyword in keywords:
                    html += f'<span class="keyword-tag">{html_module.escape(str(keyword))}</span>\n'
                html += '</div>\n'
                html += '</div>\n'
            
            forms = abstract_concepts.get("对应形式", {})
            if forms:
                html += '<div class="sub-section">\n'
                html += '<strong>对应形式：</strong>\n'
                if forms.get("文案形式"):
                    html += f'<div class="form-item"><strong>文案形式：</strong>{html_module.escape(forms["文案形式"])}</div>\n'
                if forms.get("画面形式"):
                    html += f'<div class="form-item"><strong>画面形式：</strong>{html_module.escape(forms["画面形式"])}</div>\n'
                if forms.get("声音形式"):
                    html += f'<div class="form-item"><strong>声音形式：</strong>{html_module.escape(forms["声音形式"])}</div>\n'
                html += '</div>\n'
            
            html += '</div>\n'
    
    html += '</div>\n'
    html += '</div>\n'
    return html


def generate_tab2_content(data: Dict[str, Any]) -> str:
    """生成Tab2内容：L3单元解构"""
    html = '<div class="tab-content" id="tab2" style="display: none;">\n'
    html += '<div class="section">\n'
    html += '<h3>L3单元解构</h3>\n'
    
    l3_deconstruction = data.get("L3单元解构", {})
    unit_list = l3_deconstruction.get("单元列表", [])
    
    if not unit_list:
        html += '<p>暂无数据</p>\n'
    else:
        html += f'<div class="section-info">共 {len(unit_list)} 个单元</div>\n'
        for idx, unit in enumerate(unit_list, start=1):
            html += render_unit_card(unit, idx)
    
    html += '</div>\n'
    html += '</div>\n'
    return html

