import {
  ConversationLatest,
  ConversationLatestFromServer,
  Message,
} from './message';
import {ConversationProperties} from './conversationProperties';
import {HuDataMember, OtherMembership} from './user';
import {ReadMembership} from './membership';

interface BaseConversation {
  id: string;
  created: number;
  is_im: boolean;
  is_private: boolean;
  num_members: number;
  /**
   * Other user id. Only defined in 1:1 conversations
   */
  user?: string;
  /**
   * Only defined in 1:1 conversations
   */
  is_user_deleted?: boolean;
  priority: number;
  /**
   * ULID of the last message read by the user
   */
  last_read: Nullable<string>;
  unread_count: number;
  /**
   * User id of the creator of the conversation
   */
  creator: string;
  is_channel?: boolean;
  /**
   * Group name
   */
  name?: string;
  /**
   * Group description
   */
  purpose?: {
    value: string;
  };
}

/**
 * Local conversation interface
 */
export interface Conversation
  extends Omit<BaseConversation, 'last_read' | 'unread_count'> {
  picture_url?: string;
  other_user?: HuDataMember; // We use other_user in our local model
  version: number;
  message_count: number;
  //We won't save the read membership into the DB, the map from it will be null
  readMembership: Nullable<ReadMembership>;
  latest: Nullable<ConversationLatest>;
  mentioned?: boolean;
  isConversationWithCurrentUser?: boolean;
  messages?: Message[];
  /**
   * Flag used to know if the conversation is active for current user
   */
  is_active?: boolean;
  /**
   * If the conversation is muted or not
   */
  properties?: ConversationProperties;
}

/**
 * Server conversation interface
 */
export interface ConversationFromServer extends BaseConversation {
  hu_data: {
    created_at: number;
    other_membership?: OtherMembership;
    membership: ReadMembership;
    version: number;
    message_count: number;
    latest_msg_version: number;
    latest_msg_position: number;
    picture_url?: string;
    mentioned?: boolean;
  };
  latest: ConversationLatestFromServer;
  properties?: ConversationProperties;
  marked_unread_at?: number;
}
