import { makeAutoObservable } from 'mobx';
import { List } from './List';
import { getUniqueId } from '../utils/string/getUniqueId';

export interface ChatMessage {
  /**
   * Unique id for the message
   */
  id: string;
  /**
   * The user id who sent the message
   */
  userId: string;
  /**
   * The contents of the message
   * @type {string}
   */
  content: string;
}

/**
 * A basic user model for the chat
 */
interface ChatUser {
  /**
   * Unique id for the user
   */
  id: string;
  /**
   * The name of the user
   */
  name: string;
  /**
   * The role of the user
   */
  role: string;
}

/**
 * A list of chat messages
 */
export type ChatHistory = List<ChatMessage>;

/**
 * A basic chat model
 * @class Chat
 */
export class Chat {
  users: ChatUser[];
  history: ChatHistory;

  constructor(users: ChatUser[], history?: ChatMessage[]) {
    this.users = users;
    this.history = new List<ChatMessage>(history || []);

    makeAutoObservable(this);
  }

  /**
   * Add message to the chat history
   * @param message Partial<ChatMessage>
   */
  addMessage = (message: Partial<ChatMessage>) => {
    this.history.set({
      id: getUniqueId(),
      userId: message.userId || 'user',
      content: message.content || ''
    });
  };
}
