import { type SortableAttachment } from 'src/types/attachments';
import { type Tree } from 'src/types/breadcrumbs';
import { type Icon } from 'src/types/icons';
import { type UserSegmentation } from 'src/types/segmentation';
import { type Pagination } from 'src/types/services';

export enum LibraryErrors {
  DELETE_WITH_CHILDREN = 'CANNOT_DELETE_KNOWLEDGE_LIBRARY_WITH_CHILDREN_ON_ROOT_LEVEL',
}

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

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

export type Library = {
  id: number;
  title: string;
  icon: Icon;
  status: LibraryStatus | string;
  parent: Library;
};

export type ArticleChild = {
  id: number;
  status: LibraryStatus;
  icon: Icon;
  title: string;
};

export type Article = {
  id: number;
  title: string;
  body: string;
  coverPicture: string;
  icon: Icon;
  attachments: SortableAttachment[];
  children: Article[];
  segmentation: any[];
  userSegmentation: UserSegmentation[];
  status: LibraryStatus;
  parentId: number;
  parent: ParentLibrary;
  position: number;
  notify: boolean;
  notificationType: LibraryNotificationType;
  createdAt: string;
  updatedAt: string;
  isEditor: boolean;
};

export type ParentLibrary = Pick<Article, 'id' | 'title' | 'icon' | 'status'>;

export type ArticleLeaf = Pick<Article, 'id' | 'title' | 'icon'>;

export type ArticleTree = Tree<ArticleLeaf>;

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

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

// Values
export type LibraryValues = Pick<Article, 'title' | 'icon' | 'body'> & {
  id: '' | number;
  attachments: SortableAttachment[];
  coverPicture: {
    url: string | null;
    file: File | null;
  };
  showCoverPicture: boolean;
};

// Data
export type LibraryDataAttachment = Omit<SortableAttachment, 'id'> & {
  id?: number;
  position: number;
};

export type LibraryData = Omit<
  LibraryValues,
  'showCoverPicture' | 'coverPicture' | 'attachments'
> & {
  coverPictureUrl?: string;
  removeCoverPicture?: boolean;
  replaceAttachments: boolean;
  attachments: LibraryDataAttachment[];
};

// Response
export type LibrariesResponse = Pagination<Library[]>;

// Socket
export type KnowledgeLibraryDeletedSocket = {
  id: number;
};
