<!-- 研究报告展示卡片：执行摘要、关键发现、章节化报告、一页式摘要、后续方向 -->
<template>
  <a-card class="report-card" :bordered="false">
    <template #title>
      <div class="card-title-row">
        <span>研究报告</span>
        <a-tag v-if="reportHasContent" color="success">已生成</a-tag>
      </div>
    </template>

    <a-empty v-if="!reportHasContent && !isRunning" description="先生成计划，再开始研究。" />

    <div v-else-if="isRunning && !reportHasContent" class="report-loading">
      <a-spin size="large" />
      <div class="report-loading__title">正在生成研究报告</div>
      <div class="report-loading__desc">研究已启动，首批进度、发现或章节返回后会显示在这里。</div>
    </div>

    <div v-else class="report-stack">
      <a-alert
        v-if="hasGuideEvidence"
        type="warning"
        show-icon
        class="guide-inline-alert"
        message="本轮结论包含办事指南结构化证据"
        description="当前研究报告已结合 Guide 结构化字段，适合直接核对事项材料、条件、收费、时限、窗口和咨询信息。"
      />

      <!-- 执行摘要 -->
      <div v-if="report.executiveSummary" class="report-section report-section--summary">
        <div class="report-section__title">执行摘要</div>
        <MarkdownRenderer :content="report.executiveSummary" @reference-click="handleReferenceClick" />
      </div>

      <!-- 关键发现 -->
      <div v-if="report.findings.length" class="report-section">
        <div class="report-section__title">关键发现</div>
        <div class="finding-list">
          <a-card
            v-for="finding in report.findings"
            :key="`${finding.title}-${finding.content}`"
            size="small"
            class="finding-card"
          >
            <template #title>
              <div class="finding-card__title-row">
                <span>{{ finding.title }}</span>
                <a-tag :color="strengthColor(finding.strength)">{{ strengthLabel(finding.strength) }}</a-tag>
              </div>
            </template>
            <MarkdownRenderer :content="finding.content" @reference-click="handleReferenceClick" />
          </a-card>
        </div>
      </div>

      <!-- 章节化报告 -->
      <div v-if="report.sections.length" class="report-section">
        <div class="report-section__title">章节化报告</div>
        <a-card
          v-for="section in report.sections"
          :key="section.title"
          size="small"
          class="section-card"
        >
          <template #title>
            <div class="section-card__title-row">
              <span>{{ section.title }}</span>
              <a-space size="small">
                <a-button
                  size="small"
                  type="link"
                  :disabled="!canRerunSection"
                  @click="emit('rerun-section', section)"
                >
                  重跑本节
                </a-button>
                <a-button size="small" type="link" @click="emit('continue-from-section', section)">
                  转成新任务
                </a-button>
              </a-space>
            </div>
          </template>
          <div v-if="section.summary" class="section-card__summary">{{ section.summary }}</div>
          <MarkdownRenderer :content="section.content" @reference-click="handleReferenceClick" />
        </a-card>
      </div>

      <!-- 一页式摘要 -->
      <div v-if="report.onePageSummary" class="report-section report-section--summary">
        <div class="report-section__title">一页式摘要</div>
        <MarkdownRenderer :content="report.onePageSummary" @reference-click="handleReferenceClick" />
      </div>

      <!-- 建议后续方向 -->
      <div v-if="report.recommendedNextSteps.length" class="report-section">
        <div class="report-section__title">建议后续方向</div>
        <ol class="simple-list">
          <li v-for="item in report.recommendedNextSteps" :key="item">{{ item }}</li>
        </ol>
      </div>
    </div>
  </a-card>
</template>

<script setup lang="ts">
import MarkdownRenderer from '@/components/MarkdownRenderer.vue'
import type { ResearchReport, ResearchSectionResult } from '@/types/research'
import type { ResearchReference } from '@/types/document'

const props = defineProps<{
  /** 研究报告数据 */
  report: ResearchReport
  /** 报告是否包含有效内容 */
  reportHasContent: boolean
  /** 是否正在执行研究 */
  isRunning: boolean
  /** 是否允许章节重跑 */
  canRerunSection: boolean
  /** 当前报告是否使用了 Guide 结构化证据 */
  hasGuideEvidence?: boolean
  /** 引用材料列表，按 [1]/[2] 顺序映射 */
  references: ResearchReference[]
}>()

const emit = defineEmits<{
  (e: 'rerun-section', section: ResearchSectionResult): void
  (e: 'continue-from-section', section: ResearchSectionResult): void
  (e: 'preview-reference', reference: ResearchReference): void
  (e: 'preview-reference-by-index', index: number): void
}>()

function handleReferenceClick(index: number) {
  emit('preview-reference-by-index', index)
}

function strengthLabel(strength: string): string {
  if (strength === 'high') return '强证据'
  if (strength === 'low') return '弱证据'
  return '中证据'
}

function strengthColor(strength: string): string {
  if (strength === 'high') return 'green'
  if (strength === 'low') return 'orange'
  return 'gold'
}
</script>

<style scoped lang="less">
.report-card {
  border-radius: 18px;
  box-shadow: 0 10px 40px rgba(14, 30, 62, 0.06);
}

.card-title-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.report-stack {
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.guide-inline-alert {
  border-radius: 16px;
  background: linear-gradient(180deg, #fff8e1 0%, #fffdf5 100%);
  border-color: #ffd666;
}

.report-loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 240px;
  gap: 12px;
  color: #595959;
  text-align: center;
}

.report-loading__title { font-size: 15px; font-weight: 600; color: #1f1f1f; }
.report-loading__desc { max-width: 320px; line-height: 1.6; }

.report-section {
  padding: 16px;
  border-radius: 16px;
  background: #fafbfc;
}

.report-section--summary {
  background: linear-gradient(180deg, #f8fbff 0%, #eef6ff 100%);
}

.report-section__title { margin-bottom: 12px; font-size: 15px; font-weight: 700; }

.finding-list { display: grid; gap: 12px; }

.finding-card, .section-card { border-radius: 14px; }

.finding-card__title-row,
.section-card__title-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.section-card + .section-card { margin-top: 12px; }

.section-card__summary { margin-bottom: 10px; color: #595959; }

.simple-list { margin: 0; padding-left: 20px; }
.simple-list li { margin-bottom: 6px; line-height: 1.6; }
</style>
