import {Asset} from './asset';
import {HighlightedSpan} from './highlightedSpan';
import {OtherMembership} from './user';

/**
 * Block types
 * On this URL we have every field of each block type
 * https://www.notion.so/humand-co/Chats-2-backend-interface-1816757f3130801e9c96d3fc0d976d41#1816757f31308075ace7fc17f2882013
 * Consider that the block emitter data is coming on the message object as username and user_id
 */
export enum BlockType {
  ChannelCreated = 'channel_created',
  ChannelJoined = 'channel_joined',
  ChannelLeft = 'channel_left',
  ChannelPictureChanged = 'channel_picture_changed',
  ChannelRenamed = 'channel_renamed',
  ChannelDescriptionChanged = 'channel_description_changed',
  ChannelAdminGranted = 'channel_admin_granted',
  channel_admin_revoked = 'channel_admin_revoked',
  CallInitialized = 'call_initialized',
  CallEnded = 'call_ended',
  MessageForwarded = 'message_forwarded',
  MessageReplied = 'message_replied',
  MessagePinAdded = 'message_pin_added',
}

export const SystemBlockTypes = [
  BlockType.ChannelCreated,
  BlockType.ChannelJoined,
  BlockType.ChannelLeft,
  BlockType.ChannelPictureChanged,
  BlockType.ChannelRenamed,
  BlockType.ChannelDescriptionChanged,
  BlockType.ChannelAdminGranted,
  BlockType.channel_admin_revoked,
  BlockType.CallInitialized,
  BlockType.CallEnded,
  BlockType.MessagePinAdded,
];

export const UnsupportedBlockTypes: BlockType[] = [];

export type BlockBase<T extends BlockType> = {
  type: T;
};

export type BlockChannelCreated = BlockBase<BlockType.ChannelCreated>;

export type BlockChannelJoined = BlockBase<BlockType.ChannelJoined> & {
  usernames: string[];
  user_ids: number[];
};

export type BlockChannelLeft = BlockBase<BlockType.ChannelLeft> & {
  user_id: number;
  username: string;
};

export type BlockChannelPictureChanged =
  BlockBase<BlockType.ChannelPictureChanged>;

export type BlockChannelRenamed = BlockBase<BlockType.ChannelRenamed> & {
  old_name: string;
  new_name: string;
};

export type BlockCallInitialized = BlockBase<BlockType.CallInitialized> & {
  member: {
    id: number;
    employee_internal_id: string;
    first_name: string;
    last_name: string;
    status: string;
    version: number;
  };
};

export type BlockCallEnded = BlockBase<BlockType.CallEnded> & {
  reason: 'ALL_PARTICIPANTS_LEFT' | 'NO_RESPONSE' | 'REJECTED';
};

export type BlockMessageForwarded = BlockBase<BlockType.MessageForwarded> & {
  // needed locally
  original_message_ts?: string;
  original_channel_ts?: string;
};

export type BlockMessageReplied = BlockBase<BlockType.MessageReplied> & {
  text: string;
  message_replied_id: string;
  assets: Asset[];
  blocks: Block[];
  sender_membership?: OtherMembership;
  highlighted_spans?: HighlightedSpan[];
};

export type BlockChannelDescriptionChanged =
  BlockBase<BlockType.ChannelDescriptionChanged> & {
    old_description: string;
    new_description: string;
  };
export type BlockMessagePinAdded = BlockBase<BlockType.MessagePinAdded>;

export type Block =
  | BlockChannelCreated
  | BlockChannelJoined
  | BlockChannelLeft
  | BlockChannelPictureChanged
  | BlockChannelRenamed
  | BlockCallInitialized
  | BlockCallEnded
  | BlockMessageForwarded
  | BlockMessageReplied
  | BlockChannelDescriptionChanged
  | BlockMessagePinAdded;
