import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api/index';
import type { Website, WebsiteParam } from './model';

/**
 * 获取网站信息
 */
export async function getSiteInfo() {
  const res = await request.get<ApiResult<Website>>(
    '/system/website/getSiteInfo'
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 清除缓存
 */
export async function removeSiteInfoCache(key?: string) {
  const res = await request.del<ApiResult<unknown>>(
    '/system/website/clearSiteInfo/' + key
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 分页查询网站
 */
export async function pageWebsite(params: WebsiteParam) {
  const res = await request.get<ApiResult<PageResult<Website>>>(
    '/system/website/page',
    {
      params
    }
  );
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 查询网站列表
 */
export async function listWebsite(params?: WebsiteParam) {
  const res = await request.get<ApiResult<Website[]>>(
    '/system/website',
    {
      params
    }
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 添加网站
 */
export async function addWebsite(data: Website) {
  const res = await request.post<ApiResult<unknown>>(
    '/system/website',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 修改网站
 */
export async function updateWebsite(data: Website) {
  const res = await request.put<ApiResult<unknown>>(
    '/system/website',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 删除网站
 */
export async function removeWebsite(id?: number) {
  const res = await request.del<ApiResult<unknown>>(
    '/system/website/' + id
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 批量删除网站
 */
export async function removeBatchWebsite(data: (number | undefined)[]) {
  const res = await request.del<ApiResult<unknown>>(
    '/system/website/batch',
    {
      data
    }
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 修改用户状态
 */
export async function updateWebsiteStatus(websiteId?: number, status?: number) {
  const res = await request.put<ApiResult<unknown>>(
    '/system/website/status',
    {
      websiteId,
      status
    }
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 根据id查询网站
 */
export async function getWebsite(id: number) {
  const res = await request.get<ApiResult<Website>>(
    '/system/website/' + id
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 检查IP是否存在
 */
export async function checkExistence(
  field: string,
  value: string,
  id?: number
) {
  const res = await request.get<ApiResult<unknown>>(
    '/system/website/existence',
    {
      params: { field, value, id }
    }
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}
