<template>
  <aside class="panel-right" aria-label="上下文面板">
    <!-- Idle: doctor image -->
    <DoctorAssistantFigure v-if="store.contextPanelMode === 'idle'" />

    <!-- Thinking: doctor image with thought bubble overlay -->
    <div v-else-if="store.contextPanelMode === 'thinking'" class="thinking-wrapper">
      <div class="thinking-bubble-overlay">
        <span class="wave-char" v-for="(c, i) in '思考中...'" :key="i" :style="{ animationDelay: i * 0.2 + 's' }">{{ c }}</span>
      </div>
      <DoctorAssistantFigure label="思考中" />
    </div>

    <!-- Loading: skeleton timeline (card-specific loading) -->
    <template v-else-if="store.contextPanelMode === 'loading'">
      <div class="pr-header">
        <div class="pr-label">分析中</div>
      </div>
      <div class="timeline-skeleton">
        <div class="sk-item" v-for="i in 6" :key="i">
          <div class="sk-dot" />
          <div class="sk-line" />
        </div>
      </div>
    </template>

    <!-- Task / Completed: timeline + card -->
    <template v-else-if="store.contextPanelMode === 'task' || store.contextPanelMode === 'completed'">
      <div class="pr-header">
        <div class="pr-label">
          {{ store.displayedCard?.cardKey === 'appointment-success' ? '挂号结果' : '挂号进度' }}
        </div>
      </div>
      <RegistrationProgress />
      <div class="panel-card-container" :class="{ submitting: store.cardActionSubmitting }">
        <div v-if="store.cardActionSubmitting" class="card-submitting-overlay">
          <span class="card-spinner" />
          <span>处理中...</span>
        </div>
        <CardRenderer
          v-if="store.displayedCard"
          :card="store.displayedCard"
          @action="handleAction"
          @finish="store.resetToIdle"
        />
      </div>
    </template>

    <!-- Error -->
    <template v-else>
      <div class="pr-header">
        <div class="pr-label">异常</div>
      </div>
      <ErrorCard
        :message="store.errorMessage"
        :trace-id="store.currentTraceId"
        @retry="handleRetry"
        @restart="store.resetToIdle"
      />
    </template>
  </aside>
</template>

<script setup lang="ts">
import CardRenderer from '../cards/CardRenderer.vue';
import ErrorCard from '../cards/ErrorCard.vue';
import { useTerminalStore } from '../state/terminalStore';
import DoctorAssistantFigure from './DoctorAssistantFigure.vue';
import RegistrationProgress from './RegistrationProgress.vue';

const store = useTerminalStore();

async function handleAction(data: { actionName: string; payload: Record<string, unknown> }) {
  try {
    store.addSelectionBubble(data.payload);
    store.saveSelection(data.payload);
    await store.submitCard(data.payload);
  } catch {
    // error already set in store
  }
}

async function handleRetry() {
  store.errorMessage = '';
  // 仅清除错误，让用户在原卡片上重新操作（不自动重发旧数据）
}
</script>

<style scoped>
.panel-right {
  background: var(--surface);
  border-left: 1px solid var(--border);
  padding: 24px 20px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}

.pr-header {
  margin-bottom: 20px;
}

.pr-label {
  font-size: var(--fs-xs);
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  font-weight: 500;
}

/* Thinking bubble overlay */
.thinking-wrapper {
  position: relative;
  display: flex;
  flex-direction: column;
  flex: 1;
}

.thinking-bubble-overlay {
  position: absolute;
  top: 28%;
  left: 50%;
  transform: translateX(-50%);
  background: linear-gradient(135deg, var(--surface) 0%, var(--accent-faint) 100%);
  border: 1.5px solid var(--accent-soft);
  border-radius: 16px;
  padding: 14px 24px;
  font-size: var(--fs-body);
  color: var(--accent);
  font-weight: 600;
  z-index: 2;
  box-shadow: 0 4px 20px rgba(43, 31, 153, 0.12), 0 1px 3px rgba(0, 0, 0, 0.06);
  white-space: nowrap;
  display: flex;
  gap: 1px;
}

.thinking-bubble-overlay::after {
  content: '';
  position: absolute;
  bottom: -10px;
  left: 50%;
  transform: translateX(-50%);
  width: 0;
  height: 0;
  border-left: 10px solid transparent;
  border-right: 10px solid transparent;
  border-top: 10px solid var(--accent-soft);
}

.thinking-bubble-overlay::before {
  content: '';
  position: absolute;
  bottom: -7px;
  left: 50%;
  transform: translateX(-50%);
  width: 0;
  height: 0;
  border-left: 8px solid transparent;
  border-right: 8px solid transparent;
  border-top: 8px solid var(--surface);
  z-index: 1;
}

.wave-char {
  display: inline-block;
  animation: waveJump 2s ease-in-out infinite;
}

@keyframes waveJump {
  0%, 100% { transform: translateY(0); }
  25% { transform: translateY(-6px); }
  50% { transform: translateY(0); }
}

.panel-card-container {
  flex: 1;
  overflow-y: auto;
  position: relative;
}

.panel-card-container.submitting {
  pointer-events: none;
}

.card-submitting-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  background: rgba(255, 255, 255, 0.7);
  z-index: 10;
  border-radius: var(--radius-md);
  font-size: var(--fs-sm);
  color: var(--muted);
}

.card-spinner {
  width: 24px;
  height: 24px;
  border: 2.5px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.6s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* ─── Loading skeleton timeline ─── */
.timeline-skeleton {
  position: relative;
  padding-left: 28px;
}

.timeline-skeleton::before {
  content: '';
  position: absolute;
  left: 10px;
  top: 8px;
  bottom: 8px;
  width: 0;
  border-left: 2px dotted var(--border);
}

.sk-item {
  position: relative;
  padding: 8px 0 8px 14px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.sk-dot {
  position: absolute;
  left: -22px;
  top: 12px;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--border);
  border: 2px solid var(--surface);
  z-index: 1;
}

.sk-line {
  height: 12px;
  border-radius: 4px;
  background: linear-gradient(90deg, var(--border) 25%, var(--bg) 50%, var(--border) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.5s ease-in-out infinite;
  width: 100%;
}

@keyframes shimmer {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}
</style>
