// POSTS
import qs from 'qs';

import api from 'src/config/api';
import { MARKETPLACE_POSTS_LIMIT } from 'src/constants/marketplace';
import { Comment } from 'src/types/comments';
import {
  MarketplacePost,
  MarketplacePostData,
  MarketplaceNotificationsResponse,
  MyListTotalCountResponse,
} from 'src/types/marketplace';
import { ReactionUsersResponse } from 'src/types/reaction';

export const getList = (
  timestamp: string = '',
  limit: number = MARKETPLACE_POSTS_LIMIT,
) =>
  api.get<MarketplacePost[]>('marketplace-posts', {
    params: {
      timestamp: timestamp || undefined,
      limit,
    },
    paramsSerializer: params => qs.stringify(params),
  });

export const getMyList = (
  timestamp: string = '',
  limit: number = MARKETPLACE_POSTS_LIMIT,
) =>
  api.get<MarketplacePost[]>('marketplace-posts/mine', {
    params: {
      timestamp: timestamp || undefined,
      limit,
    },
    paramsSerializer: params => qs.stringify(params),
  });

export const getMyListTotalCount = () =>
  api.get<MyListTotalCountResponse>('marketplace-posts/mine/total-count');

export const getById = (id: number) =>
  api.get<MarketplacePost>(`marketplace-posts/${id}`);

export const createMarketplacePost = (data: MarketplacePostData) =>
  api.post<MarketplacePost>('marketplace-posts', data);

export const updateMarketplacePost = (id: number, data: MarketplacePostData) =>
  api.put<MarketplacePost>(`marketplace-posts/${id}`, data);

export const deleteMarketplacePost = (id: number) =>
  api.delete(`marketplace-posts/${id}`);

// REACTIONS
export const getMarketplacePostReactionUsers = (
  postId: number,
  emoji: string,
  timestamp?: string,
) =>
  api.get<ReactionUsersResponse>(
    `marketplace-posts/${postId}/reactions/users`,
    { params: { emoji, timestamp } },
  );

export const addMarketplacePostReaction = (
  id: number,
  emoji: string,
  unified: string,
) => api.post(`marketplace-posts/${id}/reactions/add`, { emoji, unified });

export const removeMarketplacePostReaction = (id: number, emoji: string) =>
  api.post(`marketplace-posts/${id}/reactions/remove`, { emoji });

// COMMENTS
export const postComment = (id: number, data: any) =>
  api.post<Comment>(`marketplace-posts/${id}/comments`, data);

export const getMarketplacePostCommentsPaginated = (
  id: number,
  page: number,
  limit: number,
) =>
  api.get<Comment[]>(`marketplace-posts/${id}/comments`, {
    params: {
      page,
      limit,
      orderBy: 'CREATED_AT',
      order: 'DESC',
      childCommentsNumber: 2,
    },
  });

export const getChildCommentsPaginated = (
  id: number,
  parentId: number,
  page: number,
  limit: number,
) =>
  api.get<Comment[]>(`marketplace-posts/${id}/comments`, {
    params: {
      page,
      limit,
      parentId,
      orderBy: 'CREATED_AT',
      order: 'DESC',
    },
  });

// NOTIFICATIONS
export const getMarketplaceNotifications = () =>
  api.get<MarketplaceNotificationsResponse>('red-bubbles/marketplace-posts');

export const clearMarketplaceNotifications = () =>
  api.post('red-bubbles/marketplace-posts/clear');
