<template>
  <div class="thinking-panel">
    <div class="panel-header">
      <div class="panel-left">
        <div class="agent-avatar-small"></div>
        <div class="panel-title-group">
          <div class="panel-title">{{ currentDisplayAgent?.agentName || '智能分析' }}</div>
        </div>
      </div>
      <div class="close-btn" @click="emit('close')">
        <div class="btn-close-icon"></div>
      </div>
    </div>

    <div class="thinking-section">
      <div class="section-content thinking-content" ref="thinkingContentRef">
        <div v-if="!thinkingSections.length" class="empty-thinking">暂无思考过程</div>
        <div v-for="(section, idx) in thinkingSections" :key="idx" :class="['timeline-item', `timeline-${section.type}`]">
          <div class="timeline-marker">
            <div class="timeline-dot"></div>
            <div v-if="idx < thinkingSections.length - 1" class="timeline-line"></div>
          </div>
          <div class="timeline-body">
            <div class="timeline-header">
              <span class="timeline-label">{{ section.label }}</span>
            </div>
            <div class="timeline-text" v-html="renderMarkdown(section.content)"></div>
          </div>
        </div>
      </div>
    </div>

    <div class="agent-horizontal-list" v-if="selectedAgentParentMsg?.agentAnswers?.length">
      <div
        v-for="agent in selectedAgentParentMsg!.agentAnswers"
        :key="agent.id"
        :class="['agent-chip', { active: currentDisplayAgent?.id === agent.id }]"
        @click="emit('select-agent', agent)"
      >
        <div class="chip-body">
          <div class="chip-icon"></div>
          <div class="chip-name">
            {{ agent.agentName || '智能分析' }}
          </div>
        </div>
        <div class="chip-status">
          <span v-if="agent.status === 'thinking'">正在连接</span>
          <span v-else>已完成</span>
        </div>
        <div class="chip-border"></div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
  import { ref } from 'vue';
  import { renderMarkdown } from './utils';
  import type { AgentAnswer, Message, ThinkingSection } from './types';

  defineProps<{
    currentDisplayAgent: AgentAnswer | null;
    selectedAgentParentMsg: Message | null;
    thinkingSections: ThinkingSection[];
  }>();

  const emit = defineEmits<{
    (e: 'select-agent', answer: AgentAnswer): void;
    (e: 'close'): void;
  }>();

  const thinkingContentRef = ref<HTMLElement | null>(null);

  defineExpose({ thinkingContentRef });
</script>

<style scoped lang="less">
  .thinking-panel {
    height: 100%;
    width: 100%;
    display: flex;
    flex-direction: column;
    padding: 12px 16px;

    .panel-header {
      display: flex;
      padding: 0;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 6px;
      flex-shrink: 0;

      .panel-left {
        display: flex;
        align-items: center;
        gap: 12px;
      }

      .agent-avatar-small {
        width: 35px;
        height: 36px;
        border-radius: 8px;
        background-image: var(--img-chat-panel-header);
        background-repeat: no-repeat;
        background-size: 100% 100%;
        flex-shrink: 0;
      }

      .panel-title-group {
        display: flex;
        flex-direction: column;
        line-height: 1;
      }

      .panel-title {
        font-size: 15px;
        font-weight: 600;
        line-height: 1.4;
        color: #0a84ff;
      }
      .close-btn {
        cursor: pointer;
        width: 36px;
        height: 36px;
        background-image: var(--img-chat-upload-bg);
        background-repeat: no-repeat;
        background-size: 100% 100%;
        display: flex;
        align-items: center;
        justify-content: center;
        flex-shrink: 0;
        transition: all 0.3s;
        margin-bottom: auto;

        .btn-close-icon {
          width: 20px;
          height: 20px;
          background-image: var(--img-chat-close-icon);
          background-repeat: no-repeat;
          background-size: 100% 100%;
        }
      }
    }

    .thinking-section {
      flex: 1;
      height: 0;
      display: flex;
      flex-direction: column;
      border: 1px solid #0a84ff;
      border-radius: 4px 4px 0 0;

      .thinking-content {
        flex: 1;
        background: rgba(10, 132, 255, 0.05);
        border: 1px solid rgba(63, 80, 106, 0.5);
        border-radius: 8px;
        padding: 20px 16px;
        overflow-y: auto;
        font-size: 12px;
        font-weight: 400;
        color: #e0e6ed;
        line-height: 1.6;

        .empty-thinking {
          color: #8b949e;
          font-style: italic;
          text-align: center;
          padding: 32px 0;
        }

        .timeline-item {
          display: flex;
          gap: 0;
          position: relative;

          .timeline-marker {
            display: flex;
            flex-direction: column;
            align-items: center;
            width: 28px;
            flex-shrink: 0;
            position: relative;
          }

          .timeline-dot {
            width: 12px;
            height: 12px;
            border-radius: 50%;
            border: 2px solid #fff;
            flex-shrink: 0;
            margin-top: 6px;
            position: relative;
            z-index: 1;
          }

          .timeline-line {
            flex: 1;
            width: 0;
            min-height: 16px;
            margin: 4px 0;
            border-left: 2px dashed #8b949e;
          }

          &.timeline-select {
            .timeline-dot {
              background: #0a84ff;
              box-shadow: 0 0 0 3px rgba(10, 132, 255, 0.15);
            }
            .timeline-header {
              color: #e0e6ed;
            }
          }
          &.timeline-params {
            display: none;
            .timeline-dot {
              background: #faad14;
              box-shadow: 0 0 0 3px rgba(223, 91, 12, 0.15);
            }
            .timeline-header {
              color: #e0e6ed;
            }
          }
          &.timeline-result {
            .timeline-dot {
              background: #52c41a;
              box-shadow: 0 0 0 3px rgba(46, 125, 50, 0.15);
            }
          }

          .timeline-body {
            flex: 1;
            min-width: 0;
            padding-bottom: 16px;
            padding-left: 12px;
          }

          .timeline-header {
            font-size: 13px;
            font-weight: 500;
            margin-bottom: 8px;
            line-height: 1.4;
          }

          .timeline-text {
            font-size: 14px;
            font-weight: 400;
            color: #e0e6ed;
            line-height: 1.6;

            :deep(p) {
              margin: 0 0 6px;
            }
            :deep(code) {
              background: rgba(10, 132, 255, 0.1);
              padding: 2px 6px;
              border-radius: 3px;
              font-family: monospace;
              font-size: 12px;
            }
            :deep(pre) {
              background: rgba(10, 132, 255, 0.1);
              padding: 10px;
              border-radius: 4px;
              overflow-x: auto;
              margin: 8px 0;
            }
            :deep(pre code) {
              background: transparent;
              padding: 0;
            }
            :deep(ul),
            :deep(ol) {
              padding-left: 20px;
              margin: 4px 0;
            }
            :deep(li) {
              margin-bottom: 2px;
            }
            :deep(h1),
            :deep(h2),
            :deep(h3),
            :deep(h4) {
              margin: 8px 0 4px;
              font-weight: 600;
            }
            :deep(blockquote) {
              border-left: 3px solid rgba(63, 80, 106, 0.5);
              padding-left: 12px;
              margin: 8px 0;
              color: #8b949e;
            }
            :deep(table) {
              border-collapse: collapse;
              width: 100%;
              margin: 8px 0;
            }
            :deep(th),
            :deep(td) {
              border: 1px solid rgba(63, 80, 106, 0.5);
              padding: 6px 10px;
              text-align: left;
            }
            :deep(th) {
              background: rgba(10, 132, 255, 0.1);
              font-weight: 600;
            }
            :deep(strong) {
              font-weight: 600;
            }
          }
        }
      }
    }

    .agent-horizontal-list {
      display: flex;
      gap: 10px;
      padding: 8px 0;
      overflow-x: auto;
      align-items: center;
      flex-shrink: 0;
      border-radius: 0 0 4px 4px;
      border-top: none;
      background-color: rgba(10, 132, 255, 0.05);
    }

    .agent-chip {
      display: flex;
      align-items: center;
      gap: 10px;
      padding: 8px 12px 8px 20px;
      background: rgba(10, 132, 255, 0.08);
      border-radius: 10px;
      border: 1px solid rgba(63, 80, 106, 0.5);
      min-width: 140px;
      cursor: pointer;
      transition: all 0.2s;
      position: relative;

      .chip-body {
        display: flex;
        align-items: start;
        gap: 8px;
        flex-direction: column;
      }

      .chip-border {
        position: absolute;
        left: 10px;
        top: 16px;
        width: 5px;
        height: 35px;
        border: 2px solid #0a84ff;
        pointer-events: none;
        border-right: none;
      }
      .chip-icon {
        width: 24px;
        height: 24px;
        background-image: var(--img-chat-thinking-bg);
        background-repeat: no-repeat;
        background-size: 100% 100%;
      }
      .chip-name {
        font-size: 13px;
        font-weight: 500;
        line-height: 1.4;
        color: #e0e6ed;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        width: 100px;
      }

      .chip-status {
        font-size: 11px;
        font-weight: 400;
        line-height: 1.3;
        color: #8b949e;
        position: absolute;
        top: 10px;
        right: 10px;
      }

      &.active {
        background-color: rgba(10, 132, 255, 0.12);
        box-shadow: 0 6px 18px rgba(10, 132, 255, 0.06);
        border-color: #0a84ff;

        .chip-status {
          color: #0a84ff;
        }
      }
    }
  }
</style>
