import { apiShopeeSeller } from '@/api/shopee/seller/init';

/**
 *
 * @param {number[]} star
 * @param {number} limit
 * @param {number} page
 * @returns {Promise<ShopeeListRatingOrder[]>}
 */
export const getRatingListUnReply = async (star = [5, 4], limit = 20, page = 1) => {
  const payloadQuery = {
    rating_star: star.join(','),
    page_number: page,
    page_size: limit,
    cursor: 0,
    from_page_number: 0,
    reply_status: 1,
    language: 'vi',
  };

  if (page > 1) {
    payloadQuery.from_page_number = page - 1;
  }

  try {
    const result = await apiShopeeSeller.get('/api/v3/settings/search_shop_rating_comments_new/', {
      params: payloadQuery,
    });

    return result.data.list || [];
  } catch (error) {
    return [];
  }
};

/**
 *
 * @param {object} payloadFilter
 * @param {number} limit
 * @param {number} page
 * @param {number} cursor
 * @returns {Promise<ShopeeListRatingOrder[]>}
 */
export const getRatingListReply = async (payloadFilter = {}, limit = 20, page = 1, cursor = 0) => {
  const payloadQuery = {
    ...payloadFilter,
    page_number: page,
    page_size: limit,
    from_page_number: 0,
    language: 'vi',
  };

  if (page > 1) {
    payloadQuery.from_page_number = page - 1;
  }
  if (cursor) {
    payloadQuery.cursor = cursor;
  }

  try {
    const result = await apiShopeeSeller.get('/api/v3/settings/search_shop_rating_comments_new/', {
      params: payloadQuery,
    });

    return result.data.list || [];
  } catch (error) {
    return [];
  }
};

export const getRatingMeta = async (star = [5, 4]) => {
  const payloadQuery = {
    rating_star: star.join(','),
    page_number: 1,
    page_size: 1,
    cursor: 0,
    from_page_number: 0,
    reply_status: 1,
    language: 'vi',
  };

  try {
    const result = await apiShopeeSeller.get('/api/v3/settings/search_shop_rating_comments_new/', {
      params: payloadQuery,
    });

    return result.data.counts;
  } catch (error) {
    return null;
  }
};

export const replyRatingId = async (orderId, commentId, text) => {
  const payloadReply = {
    order_id: orderId,
    comment_id: commentId,
    comment: text,
  };

  const result = await apiShopeeSeller.post('/api/v3/settings/reply_shop_rating/', payloadReply);

  return result;
};
