import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type { ShopChatMessage, ShopChatMessageParam } from './model';

/**
 * 分页查询聊天消息表
 */
export async function pageShopChatMessage(params: ShopChatMessageParam) {
  const res = await request.get<ApiResult<PageResult<ShopChatMessage>>>(
    '/shop/shop-chat-message/page',
    params
  );
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 查询聊天消息表列表
 */
export async function listShopChatMessage(params?: ShopChatMessageParam) {
  const res = await request.get<ApiResult<ShopChatMessage[]>>(
    '/shop/shop-chat-message',
    params
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 添加聊天消息表
 */
export async function addShopChatMessage(data: ShopChatMessage) {
  const res = await request.post<ApiResult<unknown>>(
    '/shop/shop-chat-message',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 添加聊天消息表
 */
export async function addShopBatchChatMessage(data: ShopChatMessage[]) {
  const res = await request.post<ApiResult<unknown>>(
    '/shop/shop-chat-message/batch',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 修改聊天消息表
 */
export async function updateShopChatMessage(data: ShopChatMessage) {
  const res = await request.put<ApiResult<unknown>>(
    '/shop/shop-chat-message',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 删除聊天消息表
 */
export async function removeShopChatMessage(id?: number) {
  const res = await request.del<ApiResult<unknown>>(
    '/shop/shop-chat-message/' + id
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 批量删除聊天消息表
 */
export async function removeShopBatchChatMessage(data: (number | undefined)[]) {
  const res = await request.del<ApiResult<unknown>>(
    '/shop/shop-chat-message/batch',
    {
      data
    }
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 根据id查询聊天消息表
 */
export async function getShopChatMessage(id: number) {
  const res = await request.get<ApiResult<ShopChatMessage>>(
    '/shop/shop-chat-message/' + id
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}
