import { computed } from 'vue';
import { useI18n } from 'vue-i18n';
import type { IMessageModel } from '@tencentcloud/ai-desk-core';

interface HitTask {
    task_name?: string
}

interface HitFaqItem {
    question?: string
}

interface HitDocItem {
    name?: string
}

export interface CloudCustomData {
    messageType?: string
    hit_task?: HitTask | null
    hit_faq?: HitFaqItem[] | null
    hit_docs?: HitDocItem[] | null
}

const messageTypeWithTooltip = new Set([
    'standardTaskFlowBranchOption',
    'standardTaskFlowInformationCollection',
    'standardTaskFlowReplyMessage',
    'aiTaskFlowInformationCollection',
    'aiTaskFlowEndReply',
    'faq',
    'chitchat',
    'rag',
]);

const taskFlowMessageTypes = new Set([
    'standardTaskFlowBranchOption',
    'standardTaskFlowInformationCollection',
    'standardTaskFlowReplyMessage',
    'aiTaskFlowInformationCollection',
    'aiTaskFlowEndReply',
]);

export const useMessageSource = (getMessage: () => IMessageModel | undefined) => {
    const { t } = useI18n();

    const messageTypeLabelMap = computed<Record<string, string>>(() => ({
        standardTaskFlowBranchOption: t('任务流'),
        standardTaskFlowInformationCollection: t('任务流'),
        standardTaskFlowReplyMessage: t('任务流'),
        aiTaskFlowInformationCollection: t('任务流'),
        aiTaskFlowEndReply: t('任务流'),
        faq: t('问答库'),
        chitchat: t('问答库'),
        rag: t('文档问答'),
        aiReply: t('AI回复'),
        fallback: t('兜底话术'),
    }));

    const parsedCloudCustomData = computed<CloudCustomData | null>(() => {
        const raw = getMessage()?.cloudCustomData;
        if (typeof raw !== 'string' || !raw) {
            return null;
        }

        try {
            return JSON.parse(raw) as CloudCustomData;
        } catch {
            return null;
        }
    });

    const messageSourceLabel = computed(() => {
        const data = parsedCloudCustomData.value;

        if (!data || typeof data !== 'object') {
            return '';
        }

        const messageType = data.messageType;
        if (!messageType) {
            return '';
        }

        if (messageType === 'chitchat' && (data.hit_faq === null || data.hit_faq === undefined)) {
            return '';
        }

        return messageTypeLabelMap.value[messageType] || '';
    });

    const tooltipTitle = computed(() => {
        const data = parsedCloudCustomData.value;
        const label = messageSourceLabel.value;

        if (!data || !label) {
            return '';
        }

        const messageType = data.messageType;
        if (!messageType || !messageTypeWithTooltip.has(messageType)) {
            return '';
        }

        let tooltipLines: string[] = [];

        if (taskFlowMessageTypes.has(messageType)) {
            tooltipLines = data.hit_task?.task_name ? [data.hit_task.task_name] : [];
        }

        if (messageType === 'faq' || messageType === 'chitchat') {
            tooltipLines = Array.isArray(data.hit_faq)
                ? data.hit_faq
                    .map((item) => item?.question)
                    .filter((question): question is string => Boolean(question))
                : [];
        }

        if (messageType === 'rag') {
            tooltipLines = Array.isArray(data.hit_docs)
                ? data.hit_docs
                    .map((item) => item?.name)
                    .filter((name): name is string => Boolean(name))
                : [];
        }

        return tooltipLines.length > 0 ? `${label}-${tooltipLines.join('、')}` : label;
    });

    return {
        messageSourceLabel,
        tooltipTitle,
    };
};
