import { type SortableAttachment } from '@material-hu/types/attachments';
import { type IconInterface } from '@material-hu/types/icons';

import { type AudienceQuery } from '@material-hu/components/composed-components/audience/types';

import { type Attachment } from 'src/types/attachments';
import { type Position } from 'src/types/position';
import { type UserSegmentation } from 'src/types/segmentation';
import { type User } from 'src/types/user';

export enum ArticleStatus {
  ENABLED = 'enabled',
  DISABLED = 'disabled',
}

export enum ArticleNotificationType {
  PUSH = 'PUSH',
  EMAIL = 'EMAIL',
  BOTH = 'BOTH',
}

export enum ArticleErrors {
  DELETE_WITH_CHILDREN = 'CANNOT_DELETE_KNOWLEDGE_LIBRARY_WITH_CHILDREN_ON_ROOT_LEVEL',
}

export type Article = {
  id: number;
  body: string | null;
  user: User | null;
  lastModifiedUser: User | null;
  createdAt: Date | string | null;
  updatedAt: Date | string | null;
  icon: IconInterface;
  title: string;
  status: ArticleStatus;
  parentId: number | null;
  position: number | null;
  public: boolean | null;
  coverPicture: string | null;
  attachments: Attachment[] | null;
  notify: boolean | null;
  notificationType: ArticleNotificationType | null;
  textContent: string | null;
  parent: Article | null;
  children: Article[] | null;
  segmentation: UserSegmentation[] | null;
  userSegmentation: UserSegmentation[] | null;
  isEditor?: boolean | null;
  audiencesQuery: AudienceQuery | null;
};

export type ArticleTree = {
  id: number;
  title: string;
  status: ArticleStatus;
  position: number | null;
  parentId: number | null;
  children: ArticleTree[] | null;
  icon: IconInterface;
  isEditor?: boolean | null;
};

export type CreateArticlePayload = {
  title: string;
  parentId: number | null;
  icon: IconInterface | null;
};

export type BreadcrumbItem = {
  id?: number | null;
  title: string;
};

export type ArticleSearchListItem = Pick<
  Article,
  'id' | 'title' | 'icon' | 'status'
> & {
  textContent: string;
  parentTree: BreadcrumbItem[];
  highlights: {
    title: string[];
    textContent: string[];
  };
};

export type ArticleAttachmentPayload = Pick<
  SortableAttachment,
  'url' | 'name' | 'type' | 'size' | 'bytes'
> &
  Position & {
    id?: number;
  };

export type ArticleUpdatePayload = {
  icon?: IconInterface;
  title: string;
  body?: string;
  coverPictureUrl?: string;
  removeCoverPicture?: boolean;
  attachments?: ArticleAttachmentPayload[];
  replaceAttachments?: boolean;
  sendNotification?: boolean;
  notificationType?: ArticleNotificationType | null;
};

export type NotificationsValues = {
  sendNotification: boolean;
  notificationType: ArticleNotificationType | null;
};

export type ArticlePositionPayload = {
  orders: Array<{
    parentId: number | null;
    orderedIds: number[];
  }>;
};

export type KnowledgeLibrariesWelcomeConfig = {
  title: string;
  description: string;
  imageUrl: string | null;
};

export type DuplicateArticlePayload = {
  title: string;
  parentId: number | null;
};

export type UpdateArticleStatusPayload = {
  status: ArticleStatus;
  sendNotification: boolean;
  notificationType: ArticleNotificationType | null;
};

export type LibraryNavigationState = {
  search?: string;
};
