<template>
  <div class="review-overlay" @click.self="$emit('close')">
    <div class="review-window">
      <div class="review-header">
        <div>
          <div class="title">对话回顾</div>
          <div class="subtitle">选择要生成回顾文件的对话</div>
        </div>
        <div class="header-actions">
          <div v-if="generatedPath" class="hint">已生成 {{ generatedPath }}</div>
          <button type="button" class="ghost-btn" @click="$emit('close')" :disabled="submitting">
            关闭
          </button>
        </div>
      </div>

      <div class="review-body">
        <div class="review-left">
          <div class="list-head">
            <span>对话列表</span>
            <span class="counts" v-if="conversations.length">共 {{ conversations.length }} 条</span>
          </div>
          <div class="conversation-list" :class="{ loading }">
            <div v-if="loading" class="empty">正在加载...</div>
            <div v-else-if="!conversations.length" class="empty">暂无对话</div>
            <template v-else>
              <button
                v-for="conv in conversations"
                :key="conv.id"
                type="button"
                class="conversation-item"
                :class="{
                  active: conv.id === selectedId,
                  current: conv.id === currentConversationId
                }"
                @click="$emit('select', conv.id)"
                :disabled="submitting || conv.id === currentConversationId"
              >
                <div class="row">
                  <span class="title">{{ conv.title || '未命名对话' }}</span>
                  <span v-if="conv.id === currentConversationId" class="tag current-tag">当前</span>
                </div>
                <div class="meta">
                  <span>{{ formatUpdatedAt(conv.updated_at) }}</span>
                  <span>
                    {{ conv.total_messages || 0 }}条
                    <span v-if="(conv.total_tools || 0) > 0"> · {{ conv.total_tools }}工具</span>
                  </span>
                </div>
              </button>
            </template>
            <div class="list-footer" v-if="hasMore || conversations.length">
              <button
                type="button"
                class="ghost-btn"
                @click="$emit('load-more')"
                :disabled="loadingMore || !hasMore || submitting"
              >
                {{ loadingMore ? '载入中...' : hasMore ? '加载更多' : '没有更多了' }}
              </button>
            </div>
          </div>
        </div>

        <div class="review-right">
          <div class="preview-header">
            <span>预览（前 {{ previewLimit }} 条）</span>
            <span v-if="preview && preview.length" class="preview-count"
              >{{ preview.length }} 条</span
            >
          </div>
          <div class="preview-box" :class="{ loading: previewLoading }">
            <div v-if="previewLoading" class="placeholder">
              <img :src="icons.loading" alt="" class="big-icon" />
              <div class="text-main">预览生成中...</div>
            </div>
            <div v-else-if="previewError" class="placeholder error">
              <img :src="icons.error" alt="" class="big-icon" />
              <div class="text-main">{{ previewError }}</div>
            </div>
            <div v-else-if="!preview || !preview.length" class="placeholder">
              <img :src="icons.empty" alt="" class="big-icon" />
              <div class="text-main">选择左侧对话以查看预览</div>
              <div class="text-sub">最多展示前 {{ previewLimit }} 条</div>
            </div>
            <div v-else class="preview-list">
              <div v-for="(line, idx) in preview" :key="idx" class="preview-line">
                {{ line }}
              </div>
            </div>
          </div>
        </div>
      </div>

      <div class="review-footer">
        <label class="toggle-send">
          <input
            type="checkbox"
            :checked="sendToModel"
            @change="$emit('toggle-send', ($event.target as HTMLInputElement).checked)"
          />
          <span class="switch"></span>
          <span class="label">是否发送给模型</span>
        </label>
        <button
          type="button"
          class="primary-btn"
          @click="$emit('confirm')"
          :disabled="!selectedId || submitting"
        >
          {{ submitting ? '生成中...' : '确认' }}
        </button>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
defineOptions({ name: 'ConversationReviewDialog' });

defineProps<{
  open: boolean;
  conversations: Array<{
    id: string;
    title: string;
    updated_at: string | number;
    total_messages?: number;
    total_tools?: number;
  }>;
  selectedId: string | null;
  loading: boolean;
  loadingMore: boolean;
  hasMore: boolean;
  submitting: boolean;
  currentConversationId: string | null;
  preview: string[];
  previewLoading: boolean;
  previewError?: string | null;
  previewLimit?: number;
  sendToModel: boolean;
  generatedPath?: string | null;
}>();

const icons = {
  loading: new URL('../../../icons/clock.svg', import.meta.url).href,
  error: new URL('../../../icons/triangle-alert.svg', import.meta.url).href,
  empty: new URL('../../../icons/file.svg', import.meta.url).href
};

defineEmits<{
  (event: 'close'): void;
  (event: 'select', id: string): void;
  (event: 'load-more'): void;
  (event: 'confirm'): void;
}>();

const formatUpdatedAt = (value: string | number) => {
  if (!value) return '-';
  const date = new Date(value);
  if (Number.isNaN(date.getTime())) return '-';
  const pad = (n: number) => String(n).padStart(2, '0');
  return `${pad(date.getMonth() + 1)}/${pad(date.getDate())} ${pad(date.getHours())}:${pad(date.getMinutes())}`;
};
</script>

<style scoped>
.review-overlay {
  position: fixed;
  inset: 0;
  background: var(--theme-overlay-scrim, rgba(18, 19, 22, 0.4));
  backdrop-filter: blur(8px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 420;
  padding: 24px;
}

.review-window {
  width: min(1100px, 96vw);
  height: min(720px, 88vh);
  background: var(--theme-surface-card, var(--claude-panel));
  border-radius: 20px;
  box-shadow: var(--theme-shadow-strong, 0 28px 80px rgba(15, 23, 42, 0.25));
  display: flex;
  flex-direction: column;
  overflow: hidden;
  border: 1px solid var(--theme-control-border, var(--claude-border));
}

.review-header,
.review-footer {
  padding: 16px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--theme-surface-muted, var(--claude-left-rail));
}

.review-header .title {
  font-size: 18px;
  font-weight: 700;
  color: var(--claude-text);
}

.review-header .subtitle {
  font-size: 13px;
  color: var(--claude-text-secondary);
  margin-top: 4px;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.hint {
  font-size: 12px;
  color: var(--claude-text-secondary);
  padding: 6px 10px;
  border-radius: 10px;
  background: var(--theme-surface-soft, var(--claude-panel));
  border: 1px solid var(--theme-control-border, var(--claude-border));
  max-width: 280px;
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
}

.review-body {
  flex: 1 1 auto;
  display: grid;
  grid-template-columns: 420px 1fr;
  min-height: 0;
}

.review-left {
  border-right: 1px solid var(--theme-control-border, var(--claude-border));
  padding: 12px 14px 14px;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.list-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-weight: 600;
  color: var(--claude-text);
  margin-bottom: 8px;
}

.counts {
  font-size: 12px;
  color: var(--claude-text-secondary);
}

.conversation-list {
  flex: 1 1 auto;
  overflow-y: auto;
  overflow-x: hidden;
  border: 1px solid var(--theme-control-border, var(--claude-border));
  border-radius: 12px;
  background: var(--theme-surface-card, var(--claude-panel));
  padding: 8px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  scrollbar-width: none;
}
.conversation-list::-webkit-scrollbar {
  width: 0;
  height: 0;
}

.conversation-item {
  width: 100%;
  max-width: 360px;
  align-self: center;
  text-align: left;
  background: var(--theme-surface-card, var(--claude-panel));
  border: 1px solid var(--theme-control-border, var(--claude-border));
  border-radius: 10px;
  padding: 14px 14px;
  cursor: pointer;
  transition:
    border-color 0.15s ease,
    box-shadow 0.15s ease,
    transform 0.15s ease;
  overflow: hidden;
  min-height: 64px;
  line-height: 1.4;
}

.conversation-item:hover {
  border-color: var(--claude-accent);
  box-shadow: var(--theme-shadow-soft, 0 6px 18px rgba(15, 23, 42, 0.12));
  transform: translateY(-1px);
}

.conversation-item.active {
  border-color: var(--claude-accent-strong);
  box-shadow: 0 8px 22px rgba(218, 119, 86, 0.22);
}

.conversation-item.current {
  border-color: var(--claude-accent, #d14b31);
}

.conversation-item .row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  color: var(--claude-text);
}

.conversation-item .title {
  flex: 1 1 auto;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.conversation-item .tag {
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--theme-surface-soft, rgba(79, 70, 229, 0.12));
  color: var(--claude-text-secondary);
  font-size: 12px;
}

.current-tag {
  background: rgba(218, 119, 86, 0.18);
  color: var(--claude-accent, #d14b31);
}

.conversation-item .meta {
  margin-top: 6px;
  font-size: 12px;
  color: var(--claude-text-secondary);
  display: flex;
  gap: 10px;
}

.empty {
  text-align: center;
  color: var(--claude-text-secondary);
  padding: 20px 0;
}

.list-footer {
  margin-top: 10px;
  display: flex;
  justify-content: center;
}

.review-right {
  display: flex;
  flex-direction: column;
  padding: 12px;
  min-height: 0;
  background: var(--theme-surface-card, var(--claude-panel));
}

.preview-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-weight: 600;
  color: var(--claude-text);
  padding: 8px 0;
}

.preview-count {
  font-size: 12px;
  color: var(--claude-text-secondary);
}

.preview-box {
  flex: 1 1 auto;
  border: 1px solid var(--theme-control-border, var(--claude-border));
  border-radius: 12px;
  background: var(--theme-surface-card, var(--claude-panel));
  padding: 10px;
  overflow: auto;
  width: 100%;
}

.preview-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-family: Menlo, Consolas, 'SFMono-Regular', monospace;
  font-size: 13px;
  color: var(--claude-text);
  white-space: pre-wrap;
  word-break: break-all;
}

.preview-line {
  padding: 6px 8px;
  border-radius: 8px;
  background: var(--theme-surface-soft, var(--claude-panel));
  border: 1px solid var(--theme-control-border, var(--claude-border));
}

.placeholder {
  text-align: center;
  color: var(--claude-text-secondary);
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 20px;
  align-items: center;
  justify-content: center;
}

.big-icon {
  width: 42px;
  height: 42px;
  object-fit: contain;
  opacity: 0.85;
}

.text-main {
  font-weight: 600;
  font-size: 15px;
}

.text-sub {
  font-size: 13px;
  color: var(--claude-text-secondary);
}

.placeholder.error .text-main {
  color: #dc2626;
}

.review-footer {
  gap: 14px;
  justify-content: space-between;
  align-items: center;
}

.ghost-btn,
.primary-btn {
  border: none;
  outline: none;
  cursor: pointer;
  border-radius: 10px;
  padding: 10px 18px;
  font-weight: 600;
  font-size: 14px;
  transition:
    transform 0.12s ease,
    box-shadow 0.12s ease,
    background 0.12s ease;
}

.toggle-send {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  color: var(--claude-text);
}

.toggle-send input {
  display: none;
}

.toggle-send .switch {
  width: 38px;
  height: 22px;
  border-radius: 999px;
  background: var(--theme-control-border, var(--claude-border));
  position: relative;
  transition: background 0.2s ease;
}

.toggle-send .switch::after {
  content: '';
  position: absolute;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--theme-surface-card, #ffffff);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.18);
  top: 2px;
  left: 2px;
  transition: transform 0.2s ease;
}

.toggle-send input:checked + .switch {
  background: var(--claude-accent-strong);
}

.toggle-send input:checked + .switch::after {
  transform: translateX(16px);
}

.toggle-send .label {
  user-select: none;
}

.ghost-btn {
  background: var(--theme-surface-soft, var(--claude-panel));
  color: var(--claude-text);
  border: 1px solid var(--theme-control-border, var(--claude-border));
}

.ghost-btn:hover:not(:disabled) {
  background: var(--theme-surface-muted, var(--claude-left-rail));
}

.primary-btn {
  background: linear-gradient(135deg, var(--claude-accent), var(--claude-accent-strong));
  color: #fffaf0;
  box-shadow: 0 10px 24px rgba(212, 75, 49, 0.28);
}

.primary-btn:hover:not(:disabled) {
  transform: translateY(-1px);
}

.ghost-btn:disabled,
.primary-btn:disabled,
.conversation-item:disabled {
  opacity: 0.55;
  cursor: not-allowed;
  box-shadow: none;
  transform: none;
}
@media (max-width: 900px) {
  .review-body {
    grid-template-columns: 1fr;
  }
  .review-left {
    border-right: none;
    border-bottom: 1px solid var(--theme-control-border, var(--claude-border));
  }
}
</style>
