<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">
                <div class="bubble">
                    <span v-if="props.imageMessage.sender === 'ai'">
                        <img :src="url" alt="AI Image">
                    </span>
                    <span v-else>
                        {{ text }}
                    </span>
                </div>
                <div class="message-time" :class="{ 'user-time': isUser }">
                    {{ props.imageMessage.sentAt }}
                </div>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">

import { ImageMessage } from '@/stores/imageConversation';
import { computed } from 'vue';

const props = defineProps<{
    imageMessage: ImageMessage;
}>();
const text = computed(() => {
    if ("text" in props.imageMessage) {
        return props.imageMessage.text
    }
})
const url = computed(() => {
    if ("url" in props.imageMessage) {
        return props.imageMessage.url
    }
})
const isUser = computed(() => props.imageMessage.sender === 'user');
</script>


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

    display: flex;
    flex-direction: column;
    width: fit-content;
    padding-bottom: 16px;
}

.user-message {
    align-items: flex-end;
    width: 100%;
}

.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: #F5EFE6;
    color: #000;
    border: 1px solid #6366f1;
    border-radius: 10px;
    /* padding-left: 10px; */
}

.user-message .bubble-wrapper {
    padding: 0 20px;
    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;
    }
}
</style>