<!-- 单条消息组件 -->
<script setup lang="ts">
import type { ThinkingStatus } from 'vue-element-plus-x/types/Thinking';
import { Delete, Document, DocumentCopy, Edit, Refresh } from '@element-plus/icons-vue';
import { ElButton, ElCheckbox, ElIcon, ElInput, ElTooltip } from 'element-plus';
import MarkedMarkdown from '@/components/MarkedMarkdown/index.vue';
import type { MessageItem } from '@/composables/chat';

const props = defineProps<{
  item: MessageItem;
  isDeleteMode: boolean;
  isEditing: boolean;
  editContent: string;
  isSending: boolean;
  isSelected: boolean;
}>();

/**
 * 格式化时间
 * 将 ISO 时间字符串格式化为 yyyy-MM-dd HH:mm:ss
 */
function formatTime(time: string | undefined): string {
  if (!time) return '';

  try {
    const date = new Date(time);
    if (Number.isNaN(date.getTime())) return time;

    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    const hours = String(date.getHours()).padStart(2, '0');
    const minutes = String(date.getMinutes()).padStart(2, '0');
    const seconds = String(date.getSeconds()).padStart(2, '0');

    return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
  }
  catch {
    return time;
  }
}

const emit = defineEmits<{
  (e: 'toggleSelection', item: MessageItem): void;
  (e: 'edit', item: MessageItem): void;
  (e: 'cancelEdit'): void;
  (e: 'submitEdit', item: MessageItem): void;
  (e: 'update:editContent', value: string): void;
  (e: 'copy', item: MessageItem): void;
  (e: 'regenerate', item: MessageItem): void;
  (e: 'delete', item: MessageItem): void;
  (e: 'imagePreview', url: string): void;
}>();

/**
 * 检查消息是否有有效ID
 */
function hasValidId(item: MessageItem): boolean {
  return item.id !== undefined && (typeof item.id === 'string' || (typeof item.id === 'number' && item.id > 0));
}

/**
 * 处理思考链状态变化
 */
function handleThinkingChange(payload: { value: boolean; status: ThinkingStatus }) {
  // 可以在这里添加处理逻辑
}
</script>

<template>
  <div
    class="message-wrapper"
    :class="{
      'message-wrapper--ai': item.role !== 'user',
      'message-wrapper--user': item.role === 'user',
      'message-wrapper--delete-mode': isDeleteMode,
      'message-wrapper--selected': isSelected && isDeleteMode,
    }"
    @click="isDeleteMode && item.id && emit('toggleSelection', item)"
  >
    <!-- 删除模式：勾选框 -->
    <div v-if="isDeleteMode && item.id" class="message-wrapper__checkbox">
      <ElCheckbox
        :model-value="isSelected"
        @click.stop
        @update:model-value="emit('toggleSelection', item)"
      />
    </div>

    <!-- 消息内容区域 -->
    <div class="message-wrapper__content">
      <!-- 思考链（仅AI消息） -->
      <template v-if="item.reasoning_content && !isDeleteMode && item.role !== 'user'">
        <Thinking
          v-model="item.thinlCollapse"
          :content="item.reasoning_content"
          :status="item.thinkingStatus"
          class="message-wrapper__thinking"
          @change="handleThinkingChange"
        />
      </template>

      <!-- AI 消息内容 -->
      <template v-if="item.role !== 'user'">
        <div class="message-content message-content--ai">
          <MarkedMarkdown
            v-if="item.content"
            class="message-content__markdown"
            :content="item.content"
          />
        </div>
      </template>

      <!-- 用户消息内容 -->
      <template v-if="item.role === 'user'">
        <div
          class="message-content message-content--user"
          :class="{ 'message-content--editing': isEditing }"
        >
          <!-- 编辑模式 -->
          <template v-if="isEditing">
            <div class="message-content__edit-wrapper">
              <ElInput
                :model-value="editContent"
                type="textarea"
                :autosize="{ minRows: 3, maxRows: 10 }"
                placeholder="编辑消息内容"
                @update:model-value="emit('update:editContent', $event)"
              />
              <div class="message-content__edit-actions">
                <ElButton
                  type="primary"
                  size="small"
                  @click.stop="emit('submitEdit', item)"
                >
                  发送
                </ElButton>
                <ElButton size="small" @click.stop="emit('cancelEdit')">
                  取消
                </ElButton>
              </div>
            </div>
          </template>

          <!-- 正常显示模式 -->
          <template v-else>
            <!-- 图片列表 -->
            <div v-if="item.images && item.images.length > 0" class="message-content__images">
              <img
                v-for="(image, index) in item.images"
                :key="index"
                :src="image.url"
                :alt="image.name || '图片'"
                class="message-content__image"
                @click.stop="emit('imagePreview', image.url)"
              >
            </div>

            <!-- 文件列表 -->
            <div v-if="item.files && item.files.length > 0" class="message-content__files">
              <div
                v-for="(file, index) in item.files"
                :key="index"
                class="message-content__file-item"
              >
                <ElIcon class="message-content__file-icon">
                  <Document />
                </ElIcon>
                <span class="message-content__file-name">{{ file.name }}</span>
              </div>
            </div>

            <!-- 文本内容 -->
            <div v-if="item.content" class="message-content__text">
              {{ item.content }}
            </div>
          </template>
        </div>
      </template>
    </div>

    <!-- 操作栏（非删除模式） -->
    <div v-if="!isDeleteMode" class="message-wrapper__footer">
      <div
        class="message-wrapper__footer-content"
        :class="{ 'message-wrapper__footer-content--ai': item.role !== 'user', 'message-wrapper__footer-content--user': item.role === 'user' }"
      >
        <!-- 时间和token信息 -->
        <div class="message-wrapper__info">
          <span v-if="item.creationTime" class="message-wrapper__time">
            {{ formatTime(item.creationTime) }}
          </span>
          <span
            v-if="item.role !== 'user' && item?.tokenUsage?.total"
            class="message-wrapper__token"
          >
            {{ item?.tokenUsage?.total }} tokens
          </span>
        </div>

        <!-- 操作按钮组 -->
        <div class="message-wrapper__actions">
          <ElTooltip content="复制" placement="top">
            <ElButton text @click="emit('copy', item)">
              <ElIcon><DocumentCopy /></ElIcon>
            </ElButton>
          </ElTooltip>

          <ElTooltip
            v-if="item.role !== 'user'"
            content="重新生成"
            placement="top"
          >
            <ElButton text :disabled="isSending" @click="emit('regenerate', item)">
              <ElIcon><Refresh /></ElIcon>
            </ElButton>
          </ElTooltip>

          <ElTooltip
            v-if="item.role === 'user' && hasValidId(item)"
            content="编辑"
            placement="top"
          >
            <ElButton
              text
              :disabled="isSending"
              @click="emit('edit', item)"
            >
              <ElIcon><Edit /></ElIcon>
            </ElButton>
          </ElTooltip>

          <ElTooltip content="删除" placement="top">
            <ElButton text @click="emit('delete', item)">
              <ElIcon><Delete /></ElIcon>
            </ElButton>
          </ElTooltip>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
// 消息包装器 - 删除模式时整行有背景
.message-wrapper {
  position: relative;
  width: 100%;
  min-width: 100%;
  padding: 12px 16px;
  border-radius: 8px;
  transition: all 0.2s ease;
  box-sizing: border-box;

  // 删除模式下的样式
  &--delete-mode {
    cursor: pointer;
    background-color: #f5f7fa;
    border: 1px solid transparent;
    margin-bottom: 8px;

    &:hover {
      background-color: #e8f0fe;
      border-color: #c6dafc;
    }
  }

  // 选中状态
  &--selected {
    background-color: #d2e3fc !important;
    border-color: #8ab4f8 !important;
  }

  // 勾选框
  &__checkbox {
    position: absolute;
    left: 12px;
    top: 12px;
    z-index: 2;

    :deep(.el-checkbox) {
      --el-checkbox-input-height: 20px;
      --el-checkbox-input-width: 20px;
    }
  }

  // 内容区域 - 删除模式时有左边距给勾选框
  &__content {
    width: 100%;

    .message-wrapper--delete-mode & {
      padding-left: 36px;
    }
  }

  // 思考链
  &__thinking {
    margin-bottom: 12px;
  }

  // 底部操作栏
  &__footer {
    margin-top: 8px;
  }

  &__footer-content {
    display: flex;
    align-items: center;
    gap: 12px;

    // AI消息：操作栏在左侧
    &--ai {
      justify-content: flex-start;
      flex-direction: row-reverse;

      .message-wrapper__info {
        margin-left: 0;
        margin-right: auto;
      }

      .message-wrapper__actions {
        margin-left: 0;
      }
    }

    // 用户消息：操作栏在右侧
    &--user {
      justify-content: flex-end;

      .message-wrapper__info {
        margin-right: auto;
      }
    }
  }

  &__info {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 12px;
    color: #888;
  }

  &__time,
  &__token {
    display: flex;
    align-items: center;
    color: #888;
  }

  &__token::before {
    content: '';
    width: 3px;
    height: 3px;
    margin-right: 8px;
    background: #bbb;
    border-radius: 50%;
  }

  &__actions {
    display: flex;
    align-items: center;
    gap: 2px;

    :deep(.el-button) {
      width: 24px;
      height: 24px;
      padding: 0;
      font-size: 13px;
      color: #555;
      background: transparent;
      border: none;
      border-radius: 4px;
      transition: all 0.2s ease;

      &:hover {
        color: #409eff;
        background: #f0f7ff;
      }

      &:active {
        background: #e6f2ff;
      }

      &[disabled] {
        color: #bbb;
        background: transparent;
      }

      .el-icon {
        font-size: 13px;
      }
    }
  }
}

// 消息内容
.message-content {
  // AI消息：无气泡背景
  &--ai {
    width: 100%;
    padding: 0;
    background: transparent;
  }

  // 用户消息：有灰色气泡背景
  &--user {
    display: flex;
    flex-direction: column;
    gap: 8px;
    width: fit-content;
    max-width: 80%;
    margin-left: auto;
    padding: 12px 16px;
    background-color: #f5f5f5;
    border-radius: 12px;
  }

  // 编辑模式 - 宽度100%
  &--editing {
    width: 100%;
    max-width: 100%;
    margin-left: 0;
    background-color: transparent;
  }

  &__markdown {
    background-color: transparent;
  }

  &__images {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-bottom: 4px;
  }

  &__image {
    max-width: 200px;
    max-height: 200px;
    border-radius: 8px;
    object-fit: cover;
    cursor: pointer;
    transition: transform 0.2s;

    &:hover {
      transform: scale(1.05);
    }
  }

  &__files {
    display: flex;
    flex-direction: column;
    gap: 6px;
    margin-bottom: 4px;
  }

  &__file-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    background: rgba(0, 0, 0, 0.05);
    border-radius: 6px;
    font-size: 13px;
  }

  &__file-icon {
    font-size: 16px;
    color: #409eff;
  }

  &__file-name {
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  &__text {
    white-space: pre-wrap;
    line-height: 1.6;
    color: #333;
  }

  &__edit-wrapper {
    display: flex;
    flex-direction: column;
    gap: 12px;
    width: 100%;

    :deep(.el-textarea__inner) {
      min-height: 80px !important;
      font-size: 14px;
      line-height: 1.6;
    }
  }

  &__edit-actions {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
  }
}

// 响应式
@media (max-width: 768px) {
  .message-content {
    &__image {
      max-width: 150px;
      max-height: 150px;
    }

    &__file-item {
      padding: 6px 10px;
      font-size: 12px;
    }

    &__file-icon {
      font-size: 14px;
    }
  }

  .message-wrapper {
    &__footer-content {
      flex-wrap: wrap;
    }

    &__info {
      font-size: 11px;
    }

    &__footer-content--user,
    &__footer-content--ai {
      flex-direction: row;

      .message-wrapper__info {
        order: 1;
        margin: 0;
        margin-right: auto;
      }

      .message-wrapper__actions {
        order: 2;
      }
    }
  }
}

@media (max-width: 480px) {
  .message-content {
    &__image {
      max-width: 120px;
      max-height: 120px;
    }

    &--user {
      max-width: 90%;
    }
  }

  .message-wrapper {
    padding: 10px 12px;

    &__checkbox {
      left: 8px;
      top: 8px;
    }

    &--delete-mode &__content {
      padding-left: 28px;
    }

    &__time,
    &__token {
      font-size: 10px;
    }
  }
}
</style>
