#!/usr/bin/env python3
"""
Tab5内容生成器 - 实质与形式的双向支撑关系图
展示：选题点(来自实质) ← 实质点 → 形式点 → 选题点(来自形式)
"""
import html as html_module
import json
from typing import Dict, Any, List


def generate_tab5_content(data: Dict[str, Any]) -> str:
    """生成Tab5内容：实质与形式的双向支撑关系图（4列布局）"""
    html = '<div class="tab-content" id="tab5">\n'

    # 提取数据
    script_data = data.get('脚本理解', {})
    form_list = script_data.get('形式列表', [])
    substance_list = script_data.get('实质列表', [])
    inspiration_points = data.get('灵感点', [])
    purpose_points = data.get('目的点', [])
    key_points = data.get('关键点', [])

    if not substance_list and not form_list:
        html += '<div class="empty-state">暂无实质点和形式点数据</div>\n'
        html += '</div>\n'
        return html

    # 分类实质点
    concrete_elements = []
    concrete_concepts = []
    implicit_concepts = []
    abstract_concepts = []

    for substance in substance_list:
        dimension_2 = substance.get('维度', {}).get('二级', '')
        elem_type = substance.get('类型', '')
        
        # 隐含概念：优先通过类型判断（因为维度二级可能是"隐含概念"或"抽象概念"）
        if elem_type == '隐含概念':
            implicit_concepts.append(substance)
        elif dimension_2 == '具体元素':
            concrete_elements.append(substance)
        elif dimension_2 == '具象概念':  # 修改：从"具体概念"改为"具象概念"
            concrete_concepts.append(substance)
        elif dimension_2 == '抽象概念':
            abstract_concepts.append(substance)

    # 分类形式点
    concrete_element_forms = []
    concrete_concept_forms = []
    overall_forms = []

    for form in form_list:
        dimension_2 = form.get('维度', {}).get('二级', '')
        if dimension_2 == '具体元素形式':
            concrete_element_forms.append(form)
        elif dimension_2 == '具象概念形式':  # 修改：从"具体概念形式"改为"具象概念形式"
            concrete_concept_forms.append(form)
        elif dimension_2 == '整体形式':
            overall_forms.append(form)

    # 构建关系数据
    relationships = build_bidirectional_relationships(
        concrete_elements, concrete_concepts, implicit_concepts, abstract_concepts,
        concrete_element_forms, concrete_concept_forms, overall_forms,
        inspiration_points, purpose_points, key_points
    )

    # 添加标题和说明
    html += '<div class="tab5-header">\n'
    html += '<h2 class="tab5-title">实质与形式的双向支撑关系图</h2>\n'
    html += '<div class="tab5-description">\n'
    html += '<p>4列展示：<strong>选题点(实质评分)</strong> ← <strong>实质点</strong> | <strong>形式点</strong> → <strong>选题点(形式评分)</strong></p>\n'
    html += '<ul class="relationship-rules">\n'
    html += '<li>左侧选题点：显示实质点对灵感点、关键点、目的点的支撑关系</li>\n'
    html += '<li>右侧选题点：显示形式点对灵感点、关键点、目的点的支撑关系</li>\n'
    html += '</ul>\n'
    html += '</div>\n'
    html += '</div>\n'

    # SVG 连线容器（放在4列布局之前，作为背景层）
    html += '<div class="tab5-svg-container">\n'
    html += '<svg id="tab5-connection-svg" width="100%" height="100%">\n'
    html += '<!-- 连线将在这里绘制 -->\n'
    html += '</svg>\n'
    html += '</div>\n'

    # 主要内容区域（4列布局）
    html += '<div class="tab5-four-column-container">\n'

    # 第1列：左侧选题点（来自实质点的评分）
    html += '<div class="tab5-column tab5-left-targets">\n'
    html += '<h3 class="panel-title">选题点<br/><span style="font-size:0.8em;font-weight:normal;color:#6c757d;">(实质支撑)</span></h3>\n'

    # 灵感点（左）
    if inspiration_points:
        html += '<div class="target-group">\n'
        html += '<h4 class="group-title">灵感点</h4>\n'
        html += '<div class="target-items">\n'
        for idx, point in enumerate(inspiration_points, 1):
            # 使用提取的特征中的特征名称，每个特征名称一个卡片
            features = point.get('提取的特征', [])
            feature_names = [f.get('特征名称', '') for f in features if f.get('特征名称')]

            if feature_names:
                # 为每个特征名称创建一个独立ID的卡片
                for feature_idx, feature_name in enumerate(feature_names, 1):
                    feature_id = f'inspiration-{idx}-{feature_idx}'
                    html += f'<div class="target-card inspiration-card" data-type="inspiration-substance" data-id="{feature_id}" onclick="selectLeftTarget(' + f"'inspiration', '{idx}-{feature_idx}'" + ')">\n'
                    html += f'<div class="card-number">#{idx}-{feature_idx}</div>\n'
                    html += f'<div class="card-text">{html_module.escape(feature_name)}</div>\n'
                    html += '</div>\n'
            else:
                # 如果没有特征，使用原始的灵感点文本
                display_text = point.get('灵感点', '')
                html += f'<div class="target-card inspiration-card" data-type="inspiration-substance" data-id="inspiration-{idx}" onclick="selectLeftTarget(' + f"'inspiration', {idx}" + ')">\n'
                html += f'<div class="card-number">#{idx}</div>\n'
                html += f'<div class="card-text">{html_module.escape(display_text)}</div>\n'
                html += '</div>\n'
        html += '</div>\n'
        html += '</div>\n'

    # 关键点（左）
    if key_points:
        html += '<div class="target-group">\n'
        html += '<h4 class="group-title">关键点</h4>\n'
        html += '<div class="target-items">\n'
        for idx, point in enumerate(key_points, 1):
            # 使用提取的特征中的特征名称，每个特征名称一个卡片
            features = point.get('提取的特征', [])
            feature_names = [f.get('特征名称', '') for f in features if f.get('特征名称')]

            if feature_names:
                # 为每个特征名称创建一个独立ID的卡片
                for feature_idx, feature_name in enumerate(feature_names, 1):
                    feature_id = f'keypoint-{idx}-{feature_idx}'
                    html += f'<div class="target-card keypoint-card" data-type="keypoint-substance" data-id="{feature_id}" onclick="selectLeftTarget(' + f"'keypoint', '{idx}-{feature_idx}'" + ')">\n'
                    html += f'<div class="card-number">#{idx}-{feature_idx}</div>\n'
                    html += f'<div class="card-text">{html_module.escape(feature_name)}</div>\n'
                    html += '</div>\n'
            else:
                # 如果没有特征，使用原始的关键点文本
                display_text = point.get('关键点', '')
                html += f'<div class="target-card keypoint-card" data-type="keypoint-substance" data-id="keypoint-{idx}" onclick="selectLeftTarget(' + f"'keypoint', {idx}" + ')">\n'
                html += f'<div class="card-number">#{idx}</div>\n'
                html += f'<div class="card-text">{html_module.escape(display_text)}</div>\n'
                html += '</div>\n'
        html += '</div>\n'
        html += '</div>\n'

    # 目的点（左）
    if purpose_points:
        html += '<div class="target-group">\n'
        html += '<h4 class="group-title">目的点</h4>\n'
        html += '<div class="target-items">\n'
        for idx, point in enumerate(purpose_points, 1):
            # 使用提取的特征中的特征名称，每个特征名称一个卡片
            features = point.get('提取的特征', [])
            feature_names = [f.get('特征名称', '') for f in features if f.get('特征名称')]

            if feature_names:
                # 为每个特征名称创建一个独立ID的卡片
                for feature_idx, feature_name in enumerate(feature_names, 1):
                    feature_id = f'purpose-{idx}-{feature_idx}'
                    html += f'<div class="target-card purpose-card" data-type="purpose-substance" data-id="{feature_id}" onclick="selectLeftTarget(' + f"'purpose', '{idx}-{feature_idx}'" + ')">\n'
                    html += f'<div class="card-number">#{idx}-{feature_idx}</div>\n'
                    html += f'<div class="card-text">{html_module.escape(feature_name)}</div>\n'
                    html += '</div>\n'
            else:
                # 如果没有特征，使用原始的目的点文本
                display_text = point.get('目的点', '')
                html += f'<div class="target-card purpose-card" data-type="purpose-substance" data-id="purpose-{idx}" onclick="selectLeftTarget(' + f"'purpose', {idx}" + ')">\n'
                html += f'<div class="card-number">#{idx}</div>\n'
                html += f'<div class="card-text">{html_module.escape(display_text)}</div>\n'
                html += '</div>\n'
        html += '</div>\n'
        html += '</div>\n'

    html += '</div>\n'

    # 第2列：实质点
    html += '<div class="tab5-column tab5-substances">\n'
    html += '<h3 class="panel-title">实质点</h3>\n'

    # 具体元素
    if concrete_elements:
        html += '<div class="substance-group">\n'
        html += '<h4 class="group-title">具体元素</h4>\n'
        html += '<div class="substance-items">\n'
        for substance in concrete_elements:
            html += render_substance_card(substance, 'concrete-element')
        html += '</div>\n'
        html += '</div>\n'

    # 具象概念
    if concrete_concepts:
        html += '<div class="substance-group">\n'
        html += '<h4 class="group-title">具象概念</h4>\n'
        html += '<div class="substance-items">\n'
        for substance in concrete_concepts:
            html += render_substance_card(substance, 'concrete-concept')
        html += '</div>\n'
        html += '</div>\n'

    # 隐含概念
    if implicit_concepts:
        html += '<div class="substance-group">\n'
        html += '<h4 class="group-title">隐含概念</h4>\n'
        html += '<div class="substance-items">\n'
        for substance in implicit_concepts:
            html += render_substance_card(substance, 'implicit-concept')
        html += '</div>\n'
        html += '</div>\n'

    # 抽象概念
    if abstract_concepts:
        html += '<div class="substance-group">\n'
        html += '<h4 class="group-title">抽象概念</h4>\n'
        html += '<div class="substance-items">\n'
        for substance in abstract_concepts:
            html += render_substance_card(substance, 'abstract-concept')
        html += '</div>\n'
        html += '</div>\n'

    html += '</div>\n'

    # 第3列：形式点
    html += '<div class="tab5-column tab5-forms">\n'
    html += '<h3 class="panel-title">形式点</h3>\n'

    # 具体元素形式
    if concrete_element_forms:
        html += '<div class="form-group">\n'
        html += '<h4 class="group-title">具体元素形式</h4>\n'
        html += '<div class="form-items">\n'
        for form in concrete_element_forms:
            html += render_form_card(form, 'concrete-element-form')
        html += '</div>\n'
        html += '</div>\n'

    # 具象概念形式
    if concrete_concept_forms:
        html += '<div class="form-group">\n'
        html += '<h4 class="group-title">具象概念形式</h4>\n'
        html += '<div class="form-items">\n'
        for form in concrete_concept_forms:
            html += render_form_card(form, 'concrete-concept-form')
        html += '</div>\n'
        html += '</div>\n'

    # 整体形式
    if overall_forms:
        html += '<div class="form-group">\n'
        html += '<h4 class="group-title">整体形式</h4>\n'
        html += '<div class="form-items">\n'
        for form in overall_forms:
            html += render_form_card(form, 'overall-form')
        html += '</div>\n'
        html += '</div>\n'

    html += '</div>\n'

    # 第4列：右侧选题点（来自形式点的评分）
    html += '<div class="tab5-column tab5-right-targets">\n'
    html += '<h3 class="panel-title">选题点<br/><span style="font-size:0.8em;font-weight:normal;color:#6c757d;">(形式支撑)</span></h3>\n'

    # 灵感点（右）
    if inspiration_points:
        html += '<div class="target-group">\n'
        html += '<h4 class="group-title">灵感点</h4>\n'
        html += '<div class="target-items">\n'
        for idx, point in enumerate(inspiration_points, 1):
            # 使用提取的特征中的特征名称，每个特征名称一个卡片
            features = point.get('提取的特征', [])
            feature_names = [f.get('特征名称', '') for f in features if f.get('特征名称')]

            if feature_names:
                # 为每个特征名称创建一个独立ID的卡片
                for feature_idx, feature_name in enumerate(feature_names, 1):
                    feature_id = f'inspiration-{idx}-{feature_idx}'
                    html += f'<div class="target-card inspiration-card" data-type="inspiration-form" data-id="{feature_id}" onclick="selectRightTarget(' + f"'inspiration', '{idx}-{feature_idx}'" + ')">\n'
                    html += f'<div class="card-number">#{idx}-{feature_idx}</div>\n'
                    html += f'<div class="card-text">{html_module.escape(feature_name)}</div>\n'
                    html += '</div>\n'
            else:
                # 如果没有特征，使用原始的灵感点文本
                display_text = point.get('灵感点', '')
                html += f'<div class="target-card inspiration-card" data-type="inspiration-form" data-id="inspiration-{idx}" onclick="selectRightTarget(' + f"'inspiration', {idx}" + ')">\n'
                html += f'<div class="card-number">#{idx}</div>\n'
                html += f'<div class="card-text">{html_module.escape(display_text)}</div>\n'
                html += '</div>\n'
        html += '</div>\n'
        html += '</div>\n'

    # 关键点（右）
    if key_points:
        html += '<div class="target-group">\n'
        html += '<h4 class="group-title">关键点</h4>\n'
        html += '<div class="target-items">\n'
        for idx, point in enumerate(key_points, 1):
            # 使用提取的特征中的特征名称，每个特征名称一个卡片
            features = point.get('提取的特征', [])
            feature_names = [f.get('特征名称', '') for f in features if f.get('特征名称')]

            if feature_names:
                # 为每个特征名称创建一个独立ID的卡片
                for feature_idx, feature_name in enumerate(feature_names, 1):
                    feature_id = f'keypoint-{idx}-{feature_idx}'
                    html += f'<div class="target-card keypoint-card" data-type="keypoint-form" data-id="{feature_id}" onclick="selectRightTarget(' + f"'keypoint', '{idx}-{feature_idx}'" + ')">\n'
                    html += f'<div class="card-number">#{idx}-{feature_idx}</div>\n'
                    html += f'<div class="card-text">{html_module.escape(feature_name)}</div>\n'
                    html += '</div>\n'
            else:
                # 如果没有特征，使用原始的关键点文本
                display_text = point.get('关键点', '')
                html += f'<div class="target-card keypoint-card" data-type="keypoint-form" data-id="keypoint-{idx}" onclick="selectRightTarget(' + f"'keypoint', {idx}" + ')">\n'
                html += f'<div class="card-number">#{idx}</div>\n'
                html += f'<div class="card-text">{html_module.escape(display_text)}</div>\n'
                html += '</div>\n'
        html += '</div>\n'
        html += '</div>\n'

    # 目的点（右）
    if purpose_points:
        html += '<div class="target-group">\n'
        html += '<h4 class="group-title">目的点</h4>\n'
        html += '<div class="target-items">\n'
        for idx, point in enumerate(purpose_points, 1):
            # 使用提取的特征中的特征名称，每个特征名称一个卡片
            features = point.get('提取的特征', [])
            feature_names = [f.get('特征名称', '') for f in features if f.get('特征名称')]

            if feature_names:
                # 为每个特征名称创建一个独立ID的卡片
                for feature_idx, feature_name in enumerate(feature_names, 1):
                    feature_id = f'purpose-{idx}-{feature_idx}'
                    html += f'<div class="target-card purpose-card" data-type="purpose-form" data-id="{feature_id}" onclick="selectRightTarget(' + f"'purpose', '{idx}-{feature_idx}'" + ')">\n'
                    html += f'<div class="card-number">#{idx}-{feature_idx}</div>\n'
                    html += f'<div class="card-text">{html_module.escape(feature_name)}</div>\n'
                    html += '</div>\n'
            else:
                # 如果没有特征，使用原始的目的点文本
                display_text = point.get('目的点', '')
                html += f'<div class="target-card purpose-card" data-type="purpose-form" data-id="purpose-{idx}" onclick="selectRightTarget(' + f"'purpose', {idx}" + ')">\n'
                html += f'<div class="card-number">#{idx}</div>\n'
                html += f'<div class="card-text">{html_module.escape(display_text)}</div>\n'
                html += '</div>\n'
        html += '</div>\n'
        html += '</div>\n'

    html += '</div>\n'
    html += '</div>\n'

    # 嵌入关系数据
    html += '<script>\n'
    html += f'const tab5Relationships = {json.dumps(relationships, ensure_ascii=False)};\n'
    html += '</script>\n'

    html += '</div>\n'
    return html


def render_substance_card(substance: Dict[str, Any], css_class: str) -> str:
    """渲染实质点卡片"""
    substance_id = substance.get('id', '')
    substance_name = substance.get('名称', '')
    description = substance.get('描述', '')

    html = f'<div class="substance-card {css_class}" data-id="{html_module.escape(substance_id)}" onclick="selectSubstance(' + f"'{substance_id}'" + ')">\n'
    html += f'<div class="card-header">\n'
    html += f'<div class="card-id">#{html_module.escape(substance_id)}</div>\n'
    html += f'<div class="card-name">{html_module.escape(substance_name)}</div>\n'
    html += '</div>\n'
    if description:
        html += f'<div class="card-description">{html_module.escape(description[:50])}{"..." if len(description) > 50 else ""}</div>\n'
    html += '</div>\n'
    return html


def render_form_card(form: Dict[str, Any], css_class: str) -> str:
    """渲染形式点卡片"""
    form_id = form.get('id', '')
    form_name = form.get('名称', '')
    description = form.get('描述', '')

    html = f'<div class="form-card {css_class}" data-id="{html_module.escape(form_id)}" onclick="selectForm(' + f"'{form_id}'" + ')">\n'
    html += f'<div class="card-header">\n'
    html += f'<div class="card-id">#{html_module.escape(form_id)}</div>\n'
    html += f'<div class="card-name">{html_module.escape(form_name)}</div>\n'
    html += '</div>\n'
    if description:
        html += f'<div class="card-description">{html_module.escape(description[:50])}{"..." if len(description) > 50 else ""}</div>\n'
    html += '</div>\n'
    return html


def build_bidirectional_relationships(
    concrete_elements: List[Dict],
    concrete_concepts: List[Dict],
    implicit_concepts: List[Dict],
    abstract_concepts: List[Dict],
    concrete_element_forms: List[Dict],
    concrete_concept_forms: List[Dict],
    overall_forms: List[Dict],
    inspiration_points: List[Dict],
    purpose_points: List[Dict],
    key_points: List[Dict]
) -> Dict[str, Any]:
    """
    构建双向支撑关系数据

    返回结构：
    {
        "substance_to_target": {
            "substance_id": {
                "inspiration": [{target_id, score, ...}],
                "purpose": [...],
                "keypoint": [...]
            }
        },
        "form_to_target": {
            "form_id": {
                "inspiration": [{target_id, score, ...}],
                "purpose": [...],
                "keypoint": [...]
            }
        },
        "target_from_substance": {
            "inspiration-1": [substance_ids with scores],
            "keypoint-1": [...],
            "purpose-1": [...]
        },
        "target_from_form": {
            "inspiration-1": [form_ids with scores],
            ...
        },
        "form_to_substance": {
            "form_id": [substance_ids],
            ...
        },
        "substance_from_form": {
            "substance_id": [form_ids],
            ...
        }
    }
    """
    relationships = {
        "substance_to_target": {},
        "form_to_target": {},
        "target_from_substance": {},
        "target_from_form": {},
        "form_to_substance": {},  # 新增：形式点→实质点
        "substance_from_form": {}  # 新增：实质点←形式点（反向）
    }

    # 合并所有实质点和形式点
    all_substances = concrete_elements + concrete_concepts + implicit_concepts + abstract_concepts
    all_forms = concrete_element_forms + concrete_concept_forms + overall_forms

    # 阈值设定
    # 实质点侧已不再使用多维度评分，仅依赖意图支撑关系进行连线
    FORM_THRESHOLD = 0.5  # 形式点和选题点的阈值

    # 1. 构建实质点到选题点的关系（基于「意图支撑」，不再使用多维度评分）
    for substance in all_substances:
        substance_id = substance.get('id', '')
        if not substance_id:
            continue

        dimension_2 = substance.get('维度', {}).get('二级', '')
        intention_support = substance.get('意图支撑', {}) or {}

        relationships["substance_to_target"][substance_id] = {
            "name": substance.get('名称', ''),
            "type": dimension_2,
            "inspiration": [],
            "purpose": [],
            "keypoint": []
        }

        # 处理灵感点（意图支撑）
        for support_item in intention_support.get('灵感点', []):
            point_name = support_item.get('点')
            if not point_name:
                continue

            # 在灵感点列表中查找匹配的点，以便复用Tab1中的卡片id/特征结构
            idx = next(
                (i for i, p in enumerate(inspiration_points, 1)
                 if isinstance(p, dict) and p.get('灵感点') == point_name),
                None
            )
            if idx is None:
                continue

            point = inspiration_points[idx - 1]
            features = point.get('提取的特征', [])
            feature_names = [f.get('特征名称', '') for f in features if f.get('特征名称')]

            if feature_names:
                # 为每个特征创建独立的关系
                for feature_idx in range(1, len(feature_names) + 1):
                    target_id = f'inspiration-{idx}-{feature_idx}'
                    relationships["substance_to_target"][substance_id]["inspiration"].append({
                        "target_id": target_id,
                        "point": point_name,
                        "support_reason": support_item.get('支撑理由', '')
                    })

                    # 反向关系
                    if target_id not in relationships["target_from_substance"]:
                        relationships["target_from_substance"][target_id] = []
                    relationships["target_from_substance"][target_id].append({
                        "substance_id": substance_id,
                        "name": substance.get('名称', ''),
                        "type": dimension_2,
                        "support_reason": support_item.get('支撑理由', '')
                    })
            else:
                # 没有特征，使用主索引
                target_id = f'inspiration-{idx}'
                relationships["substance_to_target"][substance_id]["inspiration"].append({
                    "target_id": target_id,
                    "point": point_name,
                    "support_reason": support_item.get('支撑理由', '')
                })

                # 反向关系
                if target_id not in relationships["target_from_substance"]:
                    relationships["target_from_substance"][target_id] = []
                relationships["target_from_substance"][target_id].append({
                    "substance_id": substance_id,
                    "name": substance.get('名称', ''),
                    "type": dimension_2,
                    "support_reason": support_item.get('支撑理由', '')
                })

        # 处理目的点（意图支撑）
        for support_item in intention_support.get('目的点', []):
            point_name = support_item.get('点')
            if not point_name:
                continue

            idx = next(
                (i for i, p in enumerate(purpose_points, 1)
                 if isinstance(p, dict) and p.get('目的点') == point_name),
                None
            )
            if idx is None:
                continue

            point = purpose_points[idx - 1]
            features = point.get('提取的特征', [])
            feature_names = [f.get('特征名称', '') for f in features if f.get('特征名称')]

            if feature_names:
                for feature_idx in range(1, len(feature_names) + 1):
                    target_id = f'purpose-{idx}-{feature_idx}'
                    relationships["substance_to_target"][substance_id]["purpose"].append({
                        "target_id": target_id,
                        "point": point_name,
                        "support_reason": support_item.get('支撑理由', '')
                    })

                    if target_id not in relationships["target_from_substance"]:
                        relationships["target_from_substance"][target_id] = []
                    relationships["target_from_substance"][target_id].append({
                        "substance_id": substance_id,
                        "name": substance.get('名称', ''),
                        "type": dimension_2,
                        "support_reason": support_item.get('支撑理由', '')
                    })
            else:
                target_id = f'purpose-{idx}'
                relationships["substance_to_target"][substance_id]["purpose"].append({
                    "target_id": target_id,
                    "point": point_name,
                    "support_reason": support_item.get('支撑理由', '')
                })

                if target_id not in relationships["target_from_substance"]:
                    relationships["target_from_substance"][target_id] = []
                relationships["target_from_substance"][target_id].append({
                    "substance_id": substance_id,
                    "name": substance.get('名称', ''),
                    "type": dimension_2,
                    "support_reason": support_item.get('支撑理由', '')
                })

        # 处理关键点（意图支撑）
        for support_item in intention_support.get('关键点', []):
            point_name = support_item.get('点')
            if not point_name:
                continue

            idx = next(
                (i for i, p in enumerate(key_points, 1)
                 if isinstance(p, dict) and p.get('关键点') == point_name),
                None
            )
            if idx is None:
                continue

            point = key_points[idx - 1]
            features = point.get('提取的特征', [])
            feature_names = [f.get('特征名称', '') for f in features if f.get('特征名称')]

            if feature_names:
                for feature_idx in range(1, len(feature_names) + 1):
                    target_id = f'keypoint-{idx}-{feature_idx}'
                    relationships["substance_to_target"][substance_id]["keypoint"].append({
                        "target_id": target_id,
                        "point": point_name,
                        "support_reason": support_item.get('支撑理由', '')
                    })

                    if target_id not in relationships["target_from_substance"]:
                        relationships["target_from_substance"][target_id] = []
                    relationships["target_from_substance"][target_id].append({
                        "substance_id": substance_id,
                        "name": substance.get('名称', ''),
                        "type": dimension_2,
                        "support_reason": support_item.get('支撑理由', '')
                    })
            else:
                target_id = f'keypoint-{idx}'
                relationships["substance_to_target"][substance_id]["keypoint"].append({
                    "target_id": target_id,
                    "point": point_name,
                    "support_reason": support_item.get('支撑理由', '')
                })

                if target_id not in relationships["target_from_substance"]:
                    relationships["target_from_substance"][target_id] = []
                relationships["target_from_substance"][target_id].append({
                    "substance_id": substance_id,
                    "name": substance.get('名称', ''),
                    "type": dimension_2,
                    "support_reason": support_item.get('支撑理由', '')
                })

    # 2. 构建形式点到选题点的关系
    for form in all_forms:
        form_id = form.get('id', '')
        if not form_id:
            continue

        dimension_2 = form.get('维度', {}).get('二级', '')
        multi_scores = form.get('多维度评分', {})

        relationships["form_to_target"][form_id] = {
            "name": form.get('名称', ''),
            "type": dimension_2,
            "inspiration": [],
            "purpose": [],
            "keypoint": []
        }

        # 处理灵感点
        if '灵感点' in multi_scores:
            for idx, score_data in enumerate(multi_scores['灵感点'], 1):
                semantic_score = score_data.get('语义相似度', 0)
                text_score = score_data.get('文本相似度', 0)
                avg_score = (semantic_score + text_score) / 2

                if avg_score > FORM_THRESHOLD:
                    # 获取对应的灵感点，并为每个特征创建子索引关系
                    if idx <= len(inspiration_points):
                        point = inspiration_points[idx - 1]
                        features = point.get('提取的特征', [])
                        feature_names = [f.get('特征名称', '') for f in features if f.get('特征名称')]

                        if feature_names:
                            # 为每个特征创建独立的关系
                            for feature_idx in range(1, len(feature_names) + 1):
                                target_id = f'inspiration-{idx}-{feature_idx}'
                                relationships["form_to_target"][form_id]["inspiration"].append({
                                    "target_id": target_id,
                                    "point": score_data.get('点', ''),
                                    "avg_score": avg_score
                                })

                                # 反向关系
                                if target_id not in relationships["target_from_form"]:
                                    relationships["target_from_form"][target_id] = []
                                relationships["target_from_form"][target_id].append({
                                    "form_id": form_id,
                                    "name": form.get('名称', ''),
                                    "type": dimension_2,
                                    "score": avg_score
                                })
                        else:
                            # 没有特征，使用主索引
                            target_id = f'inspiration-{idx}'
                            relationships["form_to_target"][form_id]["inspiration"].append({
                                "target_id": target_id,
                                "point": score_data.get('点', ''),
                                "avg_score": avg_score
                            })

                            # 反向关系
                            if target_id not in relationships["target_from_form"]:
                                relationships["target_from_form"][target_id] = []
                            relationships["target_from_form"][target_id].append({
                                "form_id": form_id,
                                "name": form.get('名称', ''),
                                "type": dimension_2,
                                "score": avg_score
                            })

        # 处理目的点
        if '目的点' in multi_scores:
            for idx, score_data in enumerate(multi_scores['目的点'], 1):
                semantic_score = score_data.get('语义相似度', 0)
                text_score = score_data.get('文本相似度', 0)
                avg_score = (semantic_score + text_score) / 2

                if avg_score > FORM_THRESHOLD:
                    # 获取对应的目的点，并为每个特征创建子索引关系
                    if idx <= len(purpose_points):
                        point = purpose_points[idx - 1]
                        features = point.get('提取的特征', [])
                        feature_names = [f.get('特征名称', '') for f in features if f.get('特征名称')]

                        if feature_names:
                            # 为每个特征创建独立的关系
                            for feature_idx in range(1, len(feature_names) + 1):
                                target_id = f'purpose-{idx}-{feature_idx}'
                                relationships["form_to_target"][form_id]["purpose"].append({
                                    "target_id": target_id,
                                    "point": score_data.get('点', ''),
                                    "avg_score": avg_score
                                })

                                # 反向关系
                                if target_id not in relationships["target_from_form"]:
                                    relationships["target_from_form"][target_id] = []
                                relationships["target_from_form"][target_id].append({
                                    "form_id": form_id,
                                    "name": form.get('名称', ''),
                                    "type": dimension_2,
                                    "score": avg_score
                                })
                        else:
                            # 没有特征，使用主索引
                            target_id = f'purpose-{idx}'
                            relationships["form_to_target"][form_id]["purpose"].append({
                                "target_id": target_id,
                                "point": score_data.get('点', ''),
                                "avg_score": avg_score
                            })

                            # 反向关系
                            if target_id not in relationships["target_from_form"]:
                                relationships["target_from_form"][target_id] = []
                            relationships["target_from_form"][target_id].append({
                                "form_id": form_id,
                                "name": form.get('名称', ''),
                                "type": dimension_2,
                                "score": avg_score
                            })

        # 处理关键点
        if '关键点' in multi_scores:
            for idx, score_data in enumerate(multi_scores['关键点'], 1):
                semantic_score = score_data.get('语义相似度', 0)
                text_score = score_data.get('文本相似度', 0)
                avg_score = (semantic_score + text_score) / 2

                if avg_score > FORM_THRESHOLD:
                    # 获取对应的关键点，并为每个特征创建子索引关系
                    if idx <= len(key_points):
                        point = key_points[idx - 1]
                        features = point.get('提取的特征', [])
                        feature_names = [f.get('特征名称', '') for f in features if f.get('特征名称')]

                        if feature_names:
                            # 为每个特征创建独立的关系
                            for feature_idx in range(1, len(feature_names) + 1):
                                target_id = f'keypoint-{idx}-{feature_idx}'
                                relationships["form_to_target"][form_id]["keypoint"].append({
                                    "target_id": target_id,
                                    "point": score_data.get('点', ''),
                                    "avg_score": avg_score
                                })

                                # 反向关系
                                if target_id not in relationships["target_from_form"]:
                                    relationships["target_from_form"][target_id] = []
                                relationships["target_from_form"][target_id].append({
                                    "form_id": form_id,
                                    "name": form.get('名称', ''),
                                    "type": dimension_2,
                                    "score": avg_score
                                })
                        else:
                            # 没有特征，使用主索引
                            target_id = f'keypoint-{idx}'
                            relationships["form_to_target"][form_id]["keypoint"].append({
                                "target_id": target_id,
                                "point": score_data.get('点', ''),
                                "avg_score": avg_score
                            })

                            # 反向关系
                            if target_id not in relationships["target_from_form"]:
                                relationships["target_from_form"][target_id] = []
                            relationships["target_from_form"][target_id].append({
                                "form_id": form_id,
                                "name": form.get('名称', ''),
                                "type": dimension_2,
                                "score": avg_score
                            })

    # 3. 构建形式点到实质点的支撑关系
    for form in all_forms:
        form_id = form.get('id', '')
        if not form_id:
            continue

        dimension_2 = form.get('维度', {}).get('二级', '')
        support_data = form.get('支撑', [])

        if not support_data:
            continue

        # 初始化形式点的支撑关系
        if form_id not in relationships["form_to_substance"]:
            relationships["form_to_substance"][form_id] = []

        # 支撑字段可能是列表或字典
        support_items = []
        if isinstance(support_data, list):
            # 列表格式：[{id, 名称}, ...]
            support_items = support_data
        elif isinstance(support_data, dict):
            # 字典格式：{'具体元素': [...], '具象概念': [...]}
            for _category, items in support_data.items():
                if isinstance(items, list):
                    support_items.extend(items)

        for support_item in support_items:
            if not isinstance(support_item, dict):
                continue

            substance_id = support_item.get('id', '')
            substance_name = support_item.get('名称', '')

            if not substance_id:
                continue

            # 形式点 → 实质点
            relationships["form_to_substance"][form_id].append({
                "substance_id": substance_id,
                "name": substance_name
            })

            # 实质点 ← 形式点（反向）
            if substance_id not in relationships["substance_from_form"]:
                relationships["substance_from_form"][substance_id] = []

            relationships["substance_from_form"][substance_id].append({
                "form_id": form_id,
                "name": form.get('名称', ''),
                "type": dimension_2
            })

    return relationships
