import React from 'react';
import { MessagesProps } from './MessagesTypes';
import { ChatMessage } from './ChatMessages/ChatMessage';
import { LogMessage } from '../Log/LogMessage/LogMessage';
import { AutoScroll } from '../AutoScroll';
import styles from './Messages.module.css';


const Messages: React.FC<MessagesProps> = ({
  messages,
  className = '',
  variant = 'chat',
  showDocuments = true,
  showMetadata = true,
  showProgress = true,
  renderMessage,
  renderDocument,
  emptyMessage = 'No messages yet',
  onFileDelete,
  onFileRemove,
  onFileView,
  onFileDownload,
  deletingFiles,
  previewingFiles,
  removingFiles,
  downloadingFiles,
  workflowId
}) => {
  if (!messages || messages.length === 0) {
    return (
      <div className={`${styles.messagesContainer} ${styles.emptyContainer} ${className}`}>
        <div className={styles.emptyState}>{emptyMessage}</div>
      </div>
    );
  }

  return (
    <AutoScroll
      className={className}
      scrollDependency={messages.length}
    >
      <div className={styles.messagesContainer}>
        {messages.map((message, index) => {
        // Use custom render function if provided
        if (renderMessage) {
          return (
            <div key={message.id || index} className={styles.messageWrapper}>
              {renderMessage(message, index)}
            </div>
          );
        }

        // Render based on variant
        if (variant === 'log') {
          return (
            <LogMessage
              key={message.id || index}
              message={message}
              showDocuments={showDocuments}
              showMetadata={showMetadata}
              showProgress={showProgress}
              renderDocument={renderDocument}
              onFileDelete={onFileDelete}
              onFileRemove={onFileRemove}
              onFileView={onFileView}
              onFileDownload={onFileDownload}
              deletingFiles={deletingFiles}
              previewingFiles={previewingFiles}
              removingFiles={removingFiles}
              downloadingFiles={downloadingFiles}
              workflowId={workflowId}
            />
          );
        }

        // Default to chat variant
        return (
          <ChatMessage
            key={message.id || index}
            message={message}
            showDocuments={showDocuments}
            renderDocument={renderDocument}
            onFileDelete={onFileDelete}
            onFileRemove={onFileRemove}
            onFileView={onFileView}
            onFileDownload={onFileDownload}
            deletingFiles={deletingFiles}
            previewingFiles={previewingFiles}
            removingFiles={removingFiles}
            downloadingFiles={downloadingFiles}
            workflowId={workflowId}
          />
        );
      })}
      </div>
    </AutoScroll>
  );
};

export default Messages;

