<template>
    <div class="service-record-item" @click="handleItemClick">
        <div class="record-top">
            <div class="session-id-row">
                <span class="session-id-value field-ellipsis">{{ item.sessionId }}</span>
                <button class="copy-button" type="button" :aria-label="t('复制会话ID')"
                    @click.stop="emit('copy', item.sessionId)">
                    <img class="copy-icon" :src="copyIcon" alt="" />
                </button>
            </div>
            <span class="record-time">{{ item.time }}</span>
        </div>

        <div class="record-meta-row">
            <span class="record-meta-label">{{ t('用户：') }}</span>
            <span class="record-meta-name field-ellipsis">{{ item.userName }}</span>
            <div v-if="displayBlacklistStatus" class="user-status-badge">
                {{ displayBlacklistStatus }}
            </div>
            <span class="record-meta-id field-ellipsis">{{ item.userId }}</span>
        </div>

        <div class="record-meta-row">
            <span class="record-meta-label">{{ t('客服：') }}</span>
            <span class="record-meta-name field-ellipsis">{{ item.staffName }}</span>
            <span class="record-meta-id field-ellipsis">{{ item.staffEmail }}</span>
        </div>
    </div>
</template>

<script setup lang="ts">
import { computed } from 'vue';
import { useI18n } from 'vue-i18n';
import copyIcon from './copy.svg';
import { useRouter } from 'vue-router';
import AIDeskCoreInstance, { SESSION_TYPE } from '@tencentcloud/ai-desk-core';

const router = useRouter();
const { t } = useI18n();

type UserBlacklist = {
    startTime: number;
    endTime: number;
    blockType: number;
    reason: string;
    status: number;
};

const props = defineProps<{
    item: {
        sessionId: string;
        userName?: string;
        userId: string;
        staffName?: string;
        staffEmail: string;
        time: string;
        channelAgentID?: string;
        peerSource?: string;
        timestamp?: number;
        userBlacklist?: UserBlacklist;
    };
}>();

const emit = defineEmits<{
    (e: 'copy', value: string): void;
}>();

const getBlacklistStatusText = (userBlacklist?: UserBlacklist): string => {
    switch (userBlacklist?.status) {
        case 1:
            return t('已拉黑');
        case 2:
            return t('即将拉黑');
        default:
            return '';
    }
};

const ensureFinishedSessionInStore = () => {
    const existedSession = AIDeskCoreInstance.coreStore.getSessionInfo(props.item.sessionId, SESSION_TYPE.IM);
    if (existedSession) {
        return;
    }
    const temporarySession = {
        sessionId: props.item.sessionId,
        userId: props.item.userId,
        avatar: '',
        nickname: props.item.userName || props.item.userId,
        timestamp: props.item.timestamp ?? Date.now(),
        clientData: '',
        peerSource: props.item.peerSource ?? '',
        channelAgentID: props.item.channelAgentID ?? '',
    };
    AIDeskCoreInstance.sessionService.addRecordSession({ ...temporarySession });
};

const displayBlacklistStatus = computed(() => getBlacklistStatusText(props.item.userBlacklist));

const handleItemClick = () => {
    ensureFinishedSessionInStore();
    AIDeskCoreInstance.sessionService.switchSession({ sessionId: props.item.sessionId, sessionType: SESSION_TYPE.IM })
    router.push({
        path: `/mobileDesk/chat/${props.item.sessionId}`,
        query: {
            fromRecord: 'true',
        },
    });
};

</script>

<style scoped>
.service-record-item {
    padding: 16px 0;
    background: #fff;
    border-bottom: 1px solid #f2f3f5;
}

.record-top {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 12px;
}

.session-id-row {
    flex: 1;
    min-width: 0;
    display: flex;
    align-items: center;
    gap: 8px;
}

.session-id-value {
    font-size: 14px;
    line-height: 22px;
    font-weight: 600;
    color: #1d2129;
}

.copy-button {
    width: 24px;
    height: 24px;
    flex-shrink: 0;
    border: none;
    background: transparent;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0;
}

.copy-icon {
    width: 16px;
    height: 16px;
    display: block;
}

.record-time {
    flex-shrink: 0;
    font-size: 12px;
    line-height: 20px;
    color: #86909c;
}

.record-meta-row {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-top: 8px;
    min-width: 0;
}

.record-meta-label {
    flex-shrink: 0;
    font-size: 13px;
    line-height: 20px;
    color: #4e5969;
}

.record-meta-name,
.record-meta-id {
    font-size: 13px;
    line-height: 20px;
    color: #86909c;
}

.record-meta-id {
    margin-left: auto;
    text-align: right;
}

.field-ellipsis {
    min-width: 0;
    max-width: 60%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    display: inline-block;
}

.user-status-badge {
    flex-shrink: 0;
    padding: 2px 8px;
    border-radius: 999px;
    background: #FFE7E5;
    color: #C93439;
    font-size: 12px;
    line-height: 18px;
}
</style>