import api from 'src/config/api';
import {
  type Acknowledgement,
  type AcknowledgementRankingParams,
  type ActiveCategory,
  type AvailablePoints,
  type Category,
  type CreateAcknowledgementParams,
  type RankingUser,
} from 'src/pages/dashboard/acknowledgements/types';
import { type Comment, type PostCommentPayload } from 'src/types/comments';
import { type ReactionUsersResponse } from 'src/types/reaction';

import {
  type Exchange,
  type GetProductsParams,
  type ProductsResponse,
} from './Exchanges/types';

export const getAcknowledgementReactionUsers = (
  id: number,
  emoji: string,
  timestamp?: string,
) =>
  api.get<ReactionUsersResponse>(`/acknowledgements/${id}/reactions/users`, {
    params: { emoji, timestamp },
  });

export const addAcknowledgementReaction = (
  id: number,
  emoji: string,
  unified: string,
) =>
  api.post<string>(`/acknowledgements/${id}/reactions/add`, { emoji, unified });

export const removeAcknowledgementReaction = (id: number, emoji: string) =>
  api.post(`/acknowledgements/${id}/reactions/remove`, { emoji });

export const getAcknowledgements = (
  timestamp: string,
  limit = 20,
  categoryId?: number | null,
) => {
  const params: Record<string, number | string> = { limit };
  if (timestamp) params.timestamp = timestamp;
  if (categoryId != null) params.categoryId = categoryId;

  return api.get<Acknowledgement[]>('/acknowledgements', { params });
};

export const getAvailablePoints = () =>
  api.get<AvailablePoints>('/users/points-available');

export const getKudosCategories = () =>
  api.get<Category[]>('/acknowledgement-categories');

export const getActiveKudosCategories = () =>
  api.get<ActiveCategory[]>('/acknowledgement-categories/active');

export const createAcknowledgement = (payload: CreateAcknowledgementParams) =>
  api.post<Acknowledgement>('/acknowledgements', payload);

export const updateAcknowledgement = (
  acknowledgementId: number,
  payload: CreateAcknowledgementParams,
) =>
  api.put<Acknowledgement>(`/acknowledgements/${acknowledgementId}`, payload);

export const deleteAcknowledgement = (acknowledgementId: number) =>
  api.delete(`/acknowledgements/${acknowledgementId}`);

export const getAcknowledgement = (acknowledgementId: number) =>
  api.get<Acknowledgement>(`/acknowledgements/${acknowledgementId}`);

export const getAcknowledgementsRanking = (
  params: AcknowledgementRankingParams,
) => api.get<RankingUser[]>('/acknowledgements/ranking', { params });

export const getProducts = (params: GetProductsParams) =>
  api.get<ProductsResponse>('/recognitions/products', { params });

export const getUserExchanges = (userId: number) =>
  api.get<Exchange[]>(`/users/${userId}/acknowledgement-exchanges`);

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

export const postComment = (id: number, payload: PostCommentPayload) =>
  api.post<Comment>(`acknowledgements/${id}/comments`, payload);

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