import type { ChatAudioItem, ChatFileItem, ChatImageItem, ChatVideoItem } from '@lobechat/types';

import { audiosPrompts } from './audio';
import { filePrompts } from './file';
import { imagesPrompts } from './image';
import { videosPrompts } from './video';

export type { KnowledgeBaseInfo, PromptKnowledgeOptions } from './knowledgeBase';
export { promptAgentKnowledge } from './knowledgeBase';

export const filesPrompts = ({
  imageList,
  fileList,
  videoList,
  audioList,
  addUrl = true,
  messageId,
}: {
  addUrl?: boolean;
  audioList?: ChatAudioItem[];
  fileList?: ChatFileItem[];
  imageList?: ChatImageItem[];
  messageId?: string;
  videoList?: ChatVideoItem[];
}) => {
  const hasImages = (imageList || []).length > 0;
  const hasFiles = (fileList || []).length > 0;
  const hasVideos = (videoList || []).length > 0;
  const hasAudios = (audioList || []).length > 0;

  if (!hasImages && !hasFiles && !hasVideos && !hasAudios) return '';

  const contentParts = [
    hasImages ? imagesPrompts(imageList!, addUrl, messageId) : '',
    hasFiles ? filePrompts(fileList!, addUrl) : '',
    hasVideos ? videosPrompts(videoList!, addUrl, messageId) : '',
    hasAudios ? audiosPrompts(audioList!, addUrl, messageId) : '',
  ].filter(Boolean);

  const prompt = `<!-- SYSTEM CONTEXT (NOT PART OF USER QUERY) -->
<context.instruction>following part contains context information injected by the system. Please follow these instructions:

1. Always prioritize handling user-visible content.
2. the context is only required when user's queries rely on it.
</context.instruction>
<files_info>
${contentParts.join('\n')}
</files_info>
<!-- END SYSTEM CONTEXT -->`;

  return prompt.trim();
};
