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

import type { ApiResult, PageResult } from '@/api';
import type { CreditRiskRelation, CreditRiskRelationParam } from './model';

/**
 * 分页查询风险关系表
 */
export async function pageCreditRiskRelation(params: CreditRiskRelationParam) {
  const res = await request.get<ApiResult<PageResult<CreditRiskRelation>>>(
    '/credit/credit-risk-relation/page',
    { params: withCreditUserScope(params) }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询风险关系表列表
 */
export async function listCreditRiskRelation(params?: CreditRiskRelationParam) {
  const res = await request.get<ApiResult<CreditRiskRelation[]>>(
    '/credit/credit-risk-relation',
    { params: withCreditUserScope(params) }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加风险关系表
 */
export async function addCreditRiskRelation(data: CreditRiskRelation) {
  const res = await request.post<ApiResult<unknown>>(
    '/credit/credit-risk-relation',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改风险关系表
 */
export async function updateCreditRiskRelation(data: CreditRiskRelation) {
  const res = await request.put<ApiResult<unknown>>(
    '/credit/credit-risk-relation',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除风险关系表
 */
export async function removeCreditRiskRelation(id?: number) {
  const res = await request.delete<ApiResult<unknown>>(
    '/credit/credit-risk-relation/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除风险关系表
 */
export async function removeBatchCreditRiskRelation(
  data: (number | undefined)[]
) {
  const res = await request.delete<ApiResult<unknown>>(
    '/credit/credit-risk-relation/batch',
    {
      data
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 根据id查询风险关系表
 */
export async function getCreditRiskRelation(id: number) {
  const res = await request.get<ApiResult<CreditRiskRelation>>(
    '/credit/credit-risk-relation/' + id
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 导入风险关系表
 */
export async function importCreditRiskRelation(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-risk-relation/import',
    formData,
    {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}
