import request from '@/utils/request';
import { withCreditUserScope } from '@/api/credit/utils/data-scope';

import type { ApiResult, PageResult } from '@/api';
import type {
  CreditSuspectedRelationship,
  CreditSuspectedRelationshipParam
} from './model';

/**
 * 分页查询疑似关系
 */
export async function pageCreditSuspectedRelationship(
  params: CreditSuspectedRelationshipParam
) {
  const res = await request.get<
    ApiResult<PageResult<CreditSuspectedRelationship>>
  >('/credit/credit-suspected-relationship/page', withCreditUserScope(params));
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 查询疑似关系列表
 */
export async function listCreditSuspectedRelationship(
  params?: CreditSuspectedRelationshipParam
) {
  const res = await request.get<ApiResult<CreditSuspectedRelationship[]>>(
    '/credit/credit-suspected-relationship', withCreditUserScope(params));
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 添加疑似关系
 */
export async function addCreditSuspectedRelationship(
  data: CreditSuspectedRelationship
) {
  const res = await request.post<ApiResult<unknown>>(
    '/credit/credit-suspected-relationship',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 修改疑似关系
 */
export async function updateCreditSuspectedRelationship(
  data: CreditSuspectedRelationship
) {
  const res = await request.put<ApiResult<unknown>>(
    '/credit/credit-suspected-relationship',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 删除疑似关系
 */
export async function removeCreditSuspectedRelationship(id?: number) {
  const res = await request.del<ApiResult<unknown>>(
    '/credit/credit-suspected-relationship/' + id
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 批量删除疑似关系
 */
export async function removeBatchCreditSuspectedRelationship(
  data: (number | undefined)[]
) {
  const res = await request.del<ApiResult<unknown>>(
    '/credit/credit-suspected-relationship/batch',
    data);
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 根据id查询疑似关系
 */
export async function getCreditSuspectedRelationship(id: number) {
  const res = await request.get<ApiResult<CreditSuspectedRelationship>>(
    '/credit/credit-suspected-relationship/' + id
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 导入疑似关系
 */
export async function importCreditSuspectedRelationship(
  file: File,
  companyId?: number
) {
  const formData = new FormData();
  formData.append('file', file);
  if (companyId != null) {
    formData.append('companyId', String(companyId));
  }
  const res = await request.post<ApiResult<unknown>>(
    '/credit/credit-suspected-relationship/import',
    formData,
    {
      header: {
        'Content-Type': 'multipart/form-data'
      }
    }
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}
