import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type { HouseInfo, HouseInfoParam } from './model';
import { MODULES_API_URL } from '@/config/setting';

/**
 * 分页查询房源信息表
 */
export async function pageHouseInfo(params: HouseInfoParam) {
  const res = await request.get<ApiResult<PageResult<HouseInfo>>>(
    MODULES_API_URL + '/house/house-info/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询房源信息表列表
 */
export async function listHouseInfo(params?: HouseInfoParam) {
  const res = await request.get<ApiResult<HouseInfo[]>>(
    MODULES_API_URL + '/house/house-info',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加房源信息表
 */
export async function addHouseInfo(data: HouseInfo) {
  const res = await request.post<ApiResult<unknown>>(
    MODULES_API_URL + '/house/house-info',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改房源信息表
 */
export async function updateHouseInfo(data: HouseInfo) {
  const res = await request.put<ApiResult<unknown>>(
    MODULES_API_URL + '/house/house-info',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除房源信息表
 */
export async function removeHouseInfo(id?: number) {
  const res = await request.delete<ApiResult<unknown>>(
    MODULES_API_URL + '/house/house-info/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除房源信息表
 */
export async function removeBatchHouseInfo(data: (number | undefined)[]) {
  const res = await request.delete<ApiResult<unknown>>(
    MODULES_API_URL + '/house/house-info/batch',
    {
      data
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 根据id查询房源信息表
 */
export async function getHouseInfo(id: number) {
  const res = await request.get<ApiResult<HouseInfo>>(
    MODULES_API_URL + '/house/house-info/' + id
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量修改房源信息
 */
export async function updateBatchHouseInfo(data: any) {
  const res = await request.put<ApiResult<unknown>>('/house/house-info/batch', data);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}
