<template>
  <div class="message" :class="{ 'user-message': isUser }">
    <div class="message-content">
      <div class="avatar" v-if="isUser">
        <img src="@/assets/dear.png" alt="AI Avatar" class="avatar-img" />
      </div>
      <div class="avatar" v-else>
        <img src="@/assets/user.jpeg" alt="User Avatar" class="avatar-img" />
      </div>
      <div class="bubble-wrapper" :class="{'user-bubble-wrapper':isUser}" >
        <div class="bubble">
          <MarkdownRenderer v-if="isUser" :content="message.text"  />
          <div v-html="message.text" v-else></div>
          <!-- <div v-html="message.text" v-else>{{ message.text }}</div> -->
        </div>
        <div class="message-time" :class="{ 'user-time': isUser }">
          {{ message.sentAt }}
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue';
import MarkdownRenderer from './MarkdownRenderer.vue'

const props = defineProps<{
  message: {
    text: string,
    sender: 'user' | 'ai',
    sentAt: string
  }
}>()
const isUser = computed(() => props.message.sender === 'user')
</script>

<style scoped>
.message {
  position: relative;
  margin: 8px 0;
  padding: 5px;
  animation: fadeIn 0.3s ease;

  display: flex;
  flex-direction: column;
  width: 95%;
  padding-bottom: 16px;
}

.user-message {
  align-items: flex-end;

}

.message-content {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 5px;
  flex-direction: row;
  width: 100%;
  /* 默认头像在左，消息在右 */
}

.user-message .message-content {
  flex-direction: row-reverse;
  /* 用户消息时头像在右，消息在左 */
}

.avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: transparent;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  overflow: hidden;
}

.avatar-img {
  width: 36px;
  height: 36px;
  object-fit: cover;
}

.bubble-wrapper {
  width: 95%;
  flex: 1;
  min-width: 0;
  background-color: #fff;
  color: #000;
  border: 1px solid #6366f1;
  border-radius: 10px;
  padding-left: 10px;
}
.user-bubble-wrapper {
  background: #F5EFE6;

}
.user-message .bubble-wrapper {
  flex: unset;
}

.bubble {
  /* padding: 14px 18px; */
  position: relative;
  transition: all 0.3s ease;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Helvetica Neue', sans-serif;
  font-size: 16px;
  line-height: 1.7;
  letter-spacing: 0.3px;
  word-spacing: 1px;
  color: #2c3e50;
}

.user-message .bubble {
  color: #6366f1;
  font-weight: 400;
  letter-spacing: 0.4px;
}

.message-time {
  position: absolute;
  font-size: 0.75rem;
  color: #666;
  margin-top: 4px;
  text-align: left;
  left: 50px;
}

.user-time {
  text-align: right;
  right: 55px;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(10px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

:deep(.v-theme--dark) {
  .avatar {
    background: #2d3748;
  }

  .bubble {
    color: #e2e8f0;
  }

  .user-message .bubble {
    color: #7c3aed;
  }

  .message-time {
    color: #a0aec0;
  }
}

:deep(.markdown-body) {
  background: transparent !important;
  color: inherit;
}

:deep(.markdown-body ol) {
  padding-left: 1.2em;
  margin: 0;
  list-style-position: outside;
}

:deep(.markdown-body ol li) {
  margin: 4px 0;
  padding-left: 0.3em;
}

:deep(.markdown-body pre) {
  background-color: rgba(0, 0, 0, 0.05);
}

.user-message :deep(.markdown-body) {
  color: white;
}

.user-message :deep(.markdown-body pre) {
  background-color: rgba(255, 255, 255, 0.1);
}

.user-message :deep(.markdown-body code) {
  background-color: rgba(255, 255, 255, 0.2);
}

.user-message :deep(.markdown-body blockquote) {
  border-left-color: rgba(255, 255, 255, 0.3);
  color: rgba(255, 255, 255, 0.8);
}
</style>
