// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { CommunicationIdentifierKind } from "./identifierModels";

/**
 * An Azure Communication chat participant.
 */
export interface ChatParticipant {
  /**
   * The id of the chat participant.
   */
  id: CommunicationIdentifierKind;

  /**
   * Display name for the chat participant.
   */
  displayName: string;

  /**
   * Time from which the chat history is shared with the chat participant.
   * The timestamp is in RFC3339 format: `yyyy-MM-ddTHH:mm:ssZ`.
   */
  shareHistoryTime?: Date;

  /**
   * Metadata of the participant.
   */
  metadata: Record<string, string>;
}

/**
 * Properties of an Azure Communication chat thread.
 */
export interface ChatThreadProperties {
  /**
   * Thread topic.
   */
  topic: string;

  /**
   * Metadata of the thread.
   */
  metadata: Record<string, string>;
}

/** Defines values for AttachmentType. */
export type ChatAttachmentType = "image" | "file" | "unknown";

/** An attachment in a chat message. */
export interface ChatAttachment {
  /** Id of the attachment */
  id: string;
  /** The type of attachment. */
  attachmentType: ChatAttachmentType;
  /** The name of the attachment content. */
  name?: string;
  /** The URL where the attachment can be downloaded */
  url?: string;
  /** The URL where the preview of attachment can be downloaded */
  previewUrl?: string;
}

/**
 * Base class for chat event
 */
export interface BaseChatEvent {
  /**
   * Thread Id of the event.
   */
  threadId: string;

  /**
   * The Id of the event sender.
   */
  sender: CommunicationIdentifierKind;

  /**
   * The display name of the event sender.
   */
  senderDisplayName: string;

  /**
   * The Id of the event recipient.
   */
  recipient: CommunicationIdentifierKind;
}

/**
 * Event for chat message operations
 */
export interface BaseChatMessageEvent extends BaseChatEvent {
  /**
   * The Id of the message. This Id is server generated.
   */
  id: string;

  /**
   * The timestamp when the message arrived at the server. The timestamp is in RFC3339 format:
   * `yyyy-MM-ddTHH:mm:ssZ`.
   */
  createdOn: Date;

  /**
   * Version of the message. This version is an epoch time in a numeric unsigned Int64 format:
   * `1593117207131`
   */
  version: string;

  /**
   * Type of the chat message. Possible types are "Text" and "RichText/Html".
   */
  type: string;
}

/**
 * Event for chat thread operations
 */
export interface BaseChatThreadEvent {
  /**
   * Thread Id of the event.
   */
  threadId: string;

  /**
   * Version of the thread. This version is an epoch time in a numeric unsigned Int64 format:
   * `1593117207131`
   */
  version: string;
}

/**
 * Event for a received chat message.
 * All chat participants receive this event, including the original sender
 */
export interface ChatMessageReceivedEvent extends BaseChatMessageEvent {
  /**
   * Content of the message.
   */
  message: string;

  /**
   * Metadata of the message.
   */
  metadata: Record<string, string>;

  /**
   * Chat message attachments.
   */
  attachments?: ChatAttachment[];
}

/**
 * Event for a edited chat message.
 * All chat participants receive this event, including the original sender
 */
export interface ChatMessageEditedEvent extends ChatMessageReceivedEvent {
  /**
   * The timestamp when the message was edited. The timestamp is in RFC3339 format:
   * `yyyy-MM-ddTHH:mm:ssZ`.
   */
  editedOn: Date;
}

/**
 * Event for a deleted chat message.
 * All chat participants receive this event, including the original sender
 */
export interface ChatMessageDeletedEvent extends BaseChatMessageEvent {
  /**
   * The timestamp when the message was deleted. The timestamp is in RFC3339 format:
   * `yyyy-MM-ddTHH:mm:ssZ`.
   */
  deletedOn: Date;
}

/**
 * Event for a received typing indicator when a chat participant is typing.
 * All chat participants receive this event, including the original sender
 */
export interface TypingIndicatorReceivedEvent extends BaseChatEvent {
  /**
   * Version of the message.
   */
  version: string;

  /**
   * The timestamp when the message arrived at the server. The timestamp is in RFC3339 format:
   * `yyyy-MM-ddTHH:mm:ssZ`.
   */
  receivedOn: Date;
}

/**
 * Event for a received read receipt
 */
export interface ReadReceiptReceivedEvent extends BaseChatEvent {
  /**
   * The id of the last read chat message.
   */
  chatMessageId: string;

  /**
   * The timestamp when the message was read. The timestamp is in RFC3339 format: yyyy-MM-ddTHH:mm:ssZ
   */
  readOn: Date;
}

/**
 * Event for a created chat thread.
 * All chat participants receive this event, including the original sender
 */
export interface ChatThreadCreatedEvent extends BaseChatThreadEvent {
  /**
   * The timestamp when the thread was created. The timestamp is in RFC3339 format:
   * `yyyy-MM-ddTHH:mm:ssZ`.
   */
  createdOn: Date;

  /**
   * The properties of the thread.
   */
  properties: ChatThreadProperties;

  /**
   * The list of participants on the thread.
   */
  participants: ChatParticipant[];

  /**
   * Id of the user that created the chat thread.
   */
  createdBy: ChatParticipant;

  /**
   * Rentention policy
   */
  retentionPolicy: ChatRetentionPolicy;
}

/**
 * Event for an updated chat thread.
 * All chat participants receive this event, including the original sender
 */
export interface ChatThreadPropertiesUpdatedEvent extends BaseChatThreadEvent {
  /**
   * The properties of the thread.
   */
  properties: ChatThreadProperties;

  /**
   * The timestamp when the thread was updated. The timestamp is in RFC3339 format:
   * `yyyy-MM-ddTHH:mm:ssZ`.
   */
  updatedOn: Date;

  /**
   * Id of the user that updated the chat thread.
   */
  updatedBy: ChatParticipant;

  /**
   * Rentention policy
   */
  retentionPolicy: ChatRetentionPolicy;
}

/**
 * Event for an updated chat thread.
 * All chat participants receive this event, including the original sender
 */
export interface ChatThreadDeletedEvent extends BaseChatThreadEvent {
  /**
   * The timestamp when the thread was deleted. The timestamp is in RFC3339 format:
   * `yyyy-MM-ddTHH:mm:ssZ`.
   */
  deletedOn: Date;

  /**
   * Id of the user that deleted the chat thread.
   */
  deletedBy: ChatParticipant;

  /**
   * Reason of the event.
   */
  reason: string;
}

/**
 * Event for participants added to a chat thread.
 * All chat participants receive this event, including the original sender
 */
export interface ParticipantsAddedEvent extends BaseChatThreadEvent {
  /**
   * The timestamp when the member was added. The timestamp is in RFC3339 format:
   * `yyyy-MM-ddTHH:mm:ssZ`.
   */
  addedOn: Date;

  /**
   * The participants added to the thread.
   */
  participantsAdded: ChatParticipant[];

  /**
   * Id of the user that added the chat participants.
   */
  addedBy: ChatParticipant;
}

/**
 * Event for a participant added to a chat thread.
 * All chat participants receive this event, including the original sender
 */
export interface ParticipantsRemovedEvent extends BaseChatThreadEvent {
  /**
   * The timestamp when the member was removed. The timestamp is in RFC3339 format:
   * `yyyy-MM-ddTHH:mm:ssZ`.
   */
  removedOn: Date;

  /**
   * The participants removed from the thread.
   */
  participantsRemoved: ChatParticipant[];

  /**
   * Id of the user that removed the chat participants.
   */
  removedBy: ChatParticipant;
}

/**
 * Defines values for chat event.
 */
export type ChatEventId =
  | "chatMessageReceived"
  | "chatMessageEdited"
  | "chatMessageDeleted"
  | "typingIndicatorReceived"
  | "readReceiptReceived"
  | "chatThreadCreated"
  | "chatThreadDeleted"
  | "chatThreadPropertiesUpdated"
  | "participantsAdded"
  | "participantsRemoved";

/** Thread retention policy based on thread creation date. */
export interface ThreadCreationDateRetentionPolicy {
  /** Polymorphic discriminator, which specifies the different types this object can be */
  kind: "threadCreationDate";
  /** Indicates how many days after the thread creation the thread will be deleted. */
  deleteThreadAfterDays: number;
}

/** No thread retention policy. */
export interface NoneRetentionPolicy {
  /** Polymorphic discriminator, which specifies the different types this object can be */
  kind: "none";
}

/** Data retention policy for auto deletion. */
export declare type ChatRetentionPolicy = ThreadCreationDateRetentionPolicy | NoneRetentionPolicy;

export enum DeleteReason {
  DeletedByPolicy = "deletedByPolicy",
  DeletedByUser = "deletedByUser",
}
