import { type PaginationParams } from '@material-hu/hooks/useHuPagination';

import {
  type AudienceExpressionCountRequest,
  type AudienceExpressionCountResponse,
  type AudienceExpressionSearchRequest,
  type AudienceExpressionSearchResponse,
} from '@material-hu/components/composed-components/audience/types';

import api from 'src/config/api';
import { type ImageIconOption } from 'src/types/icons';
import { compressFile } from 'src/utils/files';
import { type PaginatedResponse } from 'src/utils/pagination';

import {
  type Article,
  type ArticlePositionPayload,
  type ArticleSearchListItem,
  type ArticleTree,
  type ArticleUpdatePayload,
  type CreateArticlePayload,
  type DuplicateArticlePayload,
  type KnowledgeLibrariesWelcomeConfig,
  type UpdateArticleStatusPayload,
} from './types';

export const getArticle = (articleId: number) =>
  api.get<Article>(`knowledge-libraries/${articleId}`);

export const getArticleTree = (articleId: number) =>
  api.get<ArticleTree>(`knowledge-libraries/${articleId}/tree`);

export const updateArticle = (articleId: number, data: ArticleUpdatePayload) =>
  api.patch<Article>(`knowledge-libraries/${articleId}`, data);

export const deleteArticle = (articleId: number) =>
  api.delete(`knowledge-libraries/${articleId}`);

export const getArticles = () =>
  api.get<PaginatedResponse<Article>>(`knowledge-libraries`);

export const getArticlesSearch = (params: Partial<PaginationParams>) =>
  api.get<PaginatedResponse<ArticleSearchListItem>>(
    'knowledge-libraries/search',
    { params },
  );

export const getArticleChildrenTree = (articleId: number) =>
  api.get<ArticleTree>(`knowledge-libraries/${articleId}/children-tree`);

export const updateArticlesPositions = (payload: ArticlePositionPayload) =>
  api.put<Article>(`knowledge-libraries/bulk-order`, payload);

export const createArticle = (payload: CreateArticlePayload) =>
  api.post<Article>('knowledge-libraries/v2', payload);

export const searchAudienceByExpression = (
  params: AudienceExpressionSearchRequest,
) =>
  api.post<AudienceExpressionSearchResponse>(
    'audiences/search/segmentation-expression',
    params,
  );

export const countAudienceByExpression = (
  params: AudienceExpressionCountRequest,
) =>
  api.post<AudienceExpressionCountResponse>(
    'audiences/count/segmentation-expression',
    params,
  );

export const getWelcomeConfig = () =>
  api.get<KnowledgeLibrariesWelcomeConfig>(
    'knowledge-libraries/welcome-config',
  );
export const getArticleIcons = (articleId: number) =>
  api.get<ImageIconOption[]>(`knowledge-libraries/${articleId}/icons`);

export const uploadArticleIcon = async (articleId: number, file: File) => {
  const compressedFile = await compressFile(file);
  const formData = new FormData();
  formData.append('file', compressedFile);
  return api.post(`knowledge-libraries/${articleId}/icons`, formData);
};

export const duplicateArticle = (
  articleId: number,
  payload: DuplicateArticlePayload,
) => api.post<Article>(`knowledge-libraries/${articleId}/duplicate`, payload);

export const updateArticleStatus = (
  articleId: number,
  payload: UpdateArticleStatusPayload,
) => api.patch(`knowledge-libraries/${articleId}/status`, payload);
