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

import type { ApiResult, PageResult } from '@/api';
import type {
  CreditJudicialDocument,
  CreditJudicialDocumentParam
} from './model';

/**
 * 分页查询裁判文书司法大数据
 */
export async function pageCreditJudicialDocument(
  params: CreditJudicialDocumentParam
) {
  const res = await request.get<ApiResult<PageResult<CreditJudicialDocument>>>(
    '/credit/credit-judicial-document/page', withCreditUserScope(params));
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 查询裁判文书司法大数据列表
 */
export async function listCreditJudicialDocument(
  params?: CreditJudicialDocumentParam
) {
  const res = await request.get<ApiResult<CreditJudicialDocument[]>>(
    '/credit/credit-judicial-document', withCreditUserScope(params));
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 添加裁判文书司法大数据
 */
export async function addCreditJudicialDocument(data: CreditJudicialDocument) {
  const res = await request.post<ApiResult<unknown>>(
    '/credit/credit-judicial-document',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 修改裁判文书司法大数据
 */
export async function updateCreditJudicialDocument(
  data: CreditJudicialDocument
) {
  const res = await request.put<ApiResult<unknown>>(
    '/credit/credit-judicial-document',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 删除裁判文书司法大数据
 */
export async function removeCreditJudicialDocument(id?: number) {
  const res = await request.del<ApiResult<unknown>>(
    '/credit/credit-judicial-document/' + id
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 批量删除裁判文书司法大数据
 */
export async function removeBatchCreditJudicialDocument(
  data: (number | undefined)[]
) {
  const res = await request.del<ApiResult<unknown>>(
    '/credit/credit-judicial-document/batch',
    data);
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 根据id查询裁判文书司法大数据
 */
export async function getCreditJudicialDocument(id: number) {
  const res = await request.get<ApiResult<CreditJudicialDocument>>(
    '/credit/credit-judicial-document/' + id
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 导入裁判文书司法大数据
 */
export async function importCreditJudicialDocument(
  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-judicial-document/import',
    formData,
    {
      header: {
        'Content-Type': 'multipart/form-data'
      }
    }
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 导入历史裁判文书
 */
export async function importCreditJudicialDocumentHistory(
  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-judicial-document/import/history',
    formData,
    {
      header: {
        'Content-Type': 'multipart/form-data'
      }
    }
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}
