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

/**
 * 分页查询项目域名
 */
export async function pageProjectUrl(params: ProjectUrlParam) {
  const res = await request.get<ApiResult<PageResult<ProjectUrl>>>(
    MODULES_API_URL + '/project/project-url/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询项目域名列表
 */
export async function listProjectUrl(params?: ProjectUrlParam) {
  const res = await request.get<ApiResult<ProjectUrl[]>>(
    MODULES_API_URL + '/project/project-url',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加项目域名
 */
export async function addProjectUrl(data: ProjectUrl) {
  const res = await request.post<ApiResult<unknown>>(
    MODULES_API_URL + '/project/project-url',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改项目域名
 */
export async function updateProjectUrl(data: ProjectUrl) {
  const res = await request.put<ApiResult<unknown>>(
    MODULES_API_URL + '/project/project-url',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除项目域名
 */
export async function removeProjectUrl(id?: number) {
  const res = await request.delete<ApiResult<unknown>>(
    MODULES_API_URL + '/project/project-url/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除项目域名
 */
export async function removeBatchProjectUrl(data: (number | undefined)[]) {
  const res = await request.delete<ApiResult<unknown>>(
    MODULES_API_URL + '/project/project-url/batch',
    {
      data
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 根据id查询项目域名
 */
export async function getProjectUrl(id: number) {
  const res = await request.get<ApiResult<ProjectUrl>>(
    MODULES_API_URL + '/project/project-url/' + id
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
