import { type TablerIcon } from '@material-hu/icons/tabler';

import { type Comment } from 'src/types/comments';
import {
  type Post,
  type PostRequestActionType,
  type PostRequestContent,
  type PostType,
} from 'src/types/posts';
import { type User } from 'src/types/user';

import { type Icon } from './icons';
import { type CursorPagination, type Pagination } from './services';

export enum GroupPublicationPolicies {
  OPEN = 'OPEN',
  ONLY_ADMINS = 'ONLY_ADMINS',
}

export enum GroupPrivacyPolicies {
  OPEN = 'OPEN',
  CLOSE = 'CLOSE',
  SECRET = 'SECRET',
}

export enum GroupApprovalPolicies {
  NOT_NEEDED = 'NOT_NEEDED',
  ONLY_ADMINS = 'ONLY_ADMINS',
}

export enum GroupMemberRoles {
  ADMIN = 'ADMIN',
  REGULAR = 'REGULAR',
}

export enum GroupPostStates {
  POSTED = 'POSTED',
  SCHEDULED = 'SCHEDULED',
}

export enum GroupMemberSource {
  INDIVIDUAL = 'INDIVIDUAL',
  SEGMENTATION = 'SEGMENTATION',
}

export type GroupMember = {
  createdAt: string;
  groupId: number;
  role: GroupMemberRoles;
  userId: number;
  user: GroupMemberUser | null;
  source: GroupMemberSource;
  isExternal: boolean;
  instanceId: number;
  instanceName: string;
  userCanLeave?: boolean;
};

type GroupMemberUser = {
  email: string | null;
  firstName: string;
  id: number;
  instanceId: number;
  lastName: string;
  profilePicture: string | null;
};

export enum GroupRequestStatus {
  PENDING = 'PENDING',
  ACCEPTED = 'ACCEPTED',
  REJECTED = 'REJECTED',
}

export type GroupRequest = {
  id: number;
  groupId: number;
  userId: number;
  status: GroupRequestStatus;
  createdAt: string;
  updatedAt: string;
  resolutionDatetime: string | null;
  resolutionUser: Pick<
    User,
    'id' | 'firstName' | 'lastName' | 'email' | 'profilePicture'
  > | null;
  resolutionUserId: number | null;
  user: Pick<
    User,
    'id' | 'firstName' | 'lastName' | 'email' | 'profilePicture'
  > & {
    instanceId: number;
  };
};

type ArchivedMetadata = {
  archivedAt: string;
  archivedBy: {
    id: number;
    instanceId: number;
    firstName: string;
    lastName: string;
    email: string;
    profilePicture: string | null;
  };
};

export type Group = {
  id: number;
  instanceId: number;
  members: GroupMember[];
  membersCount: number;
  unreadCount: number;
  bannerUrl: string;
  description: string;
  hasPendingRequests: boolean;
  groupRequest: GroupRequest | null;
  approvalPolicy: GroupApprovalPolicies;
  privacyPolicy: GroupPrivacyPolicies;
  publicationPolicy: GroupPublicationPolicies;
  title: string;
  createdAt: string;
  icon: Icon;
  externalId: string | null;
  pinned: boolean;
  isArchived?: boolean;
  archivedMetadata?: ArchivedMetadata | null;
  isMultiCompany?: boolean;
  membersCanLeave?: boolean;
};

export enum GroupMembersPolicies {
  ALL_USERS = 'ALL_USERS',
  TARGETED_USERS = 'TARGETED_USERS',
}

export type GroupBody = {
  title: string;
  description?: string;
  banner?: {
    file: File | null;
    url: string | null;
  };
  bannerFile?: File | null;
  privacyPolicy: GroupPrivacyPolicies;
  approvalPolicy: GroupApprovalPolicies;
  publicationPolicy: GroupPublicationPolicies;
  icon: Icon & { file?: File };
  membersPolicy?: GroupMembersPolicies;
  memberIds?: number[];
  isMultiCompany?: boolean;
  membersCanLeave?: boolean;
};

export type GroupBodyRequest = Omit<GroupBody, 'banner'> & {
  bannerUrl: string | undefined;
};

export type GroupsNotificationsResponse = {
  groupNotifications: number;
};

export type GroupPost = Omit<Post, 'group' | 'state' | 'type'> & {
  groupId: number;
  state: GroupPostStates;
  type: PostType.GroupType;
};

export type GroupSchedulePost = GroupPost & {
  sendNotification: boolean;
};

export type GroupSchedulePostList = Pagination<GroupSchedulePost[]>;

// called Post Request on api
export type GroupPendingPost = {
  id: number;
  groupId: number;
  updatedAt: string;
  createdAt: string;
  approvalStatus: ApprovalStatus;
  resolutionDatetime: string | null;
  resolutionUser: Pick<
    User,
    | 'createdAt'
    | 'deleted'
    | 'email'
    | 'employeeInternalId'
    | 'firstName'
    | 'hiringDate'
    | 'id'
    | 'isAgent'
    | 'lastName'
    | 'profilePicture'
  > | null;
  resolutionUserId: number | null;
  groupPost: GroupPost;
} & (
  | {
      actionType: PostRequestActionType.CREATION;
      postContent: null;
    }
  | {
      actionType: PostRequestActionType.EDITION;
      postContent: PostRequestContent;
    }
);

export type GroupPendingApprovalPostList = Pagination<GroupPendingPost[]>;

export type GroupPostList = CursorPagination<GroupPost[]>;

export type GroupCommentList = CursorPagination<Comment[]>;

export type GroupReactionUsersResponse = CursorPagination<User[]>;

export enum ApprovalStatus {
  APPROVED = 'APPROVED',
  REJECTED = 'REJECTED',
  PENDING = 'PENDING',
}

export enum PendingPostReviewTypes {
  APPROVE_ONE = 'APPROVE_ONE',
  REJECT_ONE = 'REJECT_ONE',
  APPROVE_SELECTED = 'APPROVE_SELECTED',
  REJECT_SELECTED = 'REJECT_SELECTED',
}

export enum GroupsStepsType {
  GENERAL_ASPECTS = 'GENERAL_ASPECTS',
  MANAGE_MEMBERS = 'MANAGE_MEMBERS',
  INVITATION_MANAGEMENT = 'INVITATION_MANAGEMENT',
  PENDING_POSTS_APPROVAL = 'PENDING_POSTS_APPROVAL',
  FEATURED_MEMBERS = 'FEATURED_MEMBERS',
  GROWTH = 'GROWTH',
  INTERACTION = 'INTERACTION',
}

export enum GroupNotificationConfigurationConfigModes {
  EMAIL = 'EMAIL',
  PUSH = 'PUSH',
  NOTIFICATION_CENTER = 'NOTIFICATION_CENTER',
}

type GroupNotificationConfigurationConfig = {
  enabled: boolean;
  mode: GroupNotificationConfigurationConfigModes;
};

export type GroupNotificationConfiguration = {
  configs: GroupNotificationConfigurationConfig[];
  groupId: number;
  userId: number;
};

export type GroupStatsSummary = {
  currentActivityScore: number;
  currentCommentsCount: number;
  currentPostsCount: number;
  previousActivityScore: number;
  previousCommentsCount: number;
  previousPostsCount: number;
};

export type ActiveMemberUser = {
  id: number;
  firstName: string;
  lastName: string;
  email: string;
  instanceId: number;
  profilePicture: string;
};

export type ActiveMember = {
  activityScore: number;
  commentsCount: number;
  postsCount: number;
  user: ActiveMemberUser;
};

export type ActiveMembersResponse = CursorPagination<ActiveMember[]>;

export const STEP_DIVIDER_ID = 'Divider';

export type GroupComponentStep = {
  id: GroupsStepsType;
  label: string;
  icon: TablerIcon;
  Component: React.FC<{ group?: Group }>;
  showBadge?: boolean;
  disabled?: boolean;
};
export type GroupStep =
  | GroupComponentStep
  | {
      id: typeof STEP_DIVIDER_ID;
      label: string;
    };

export type GroupCompany = {
  instanceLogo: string;
  instanceName: string;
  joinedAt: string;
};
