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

import type { ApiResult, PageResult } from '@/api';
import type { CreditPatent, CreditPatentParam } from './model';

/**
 * 分页查询专利
 */
export async function pageCreditPatent(params: CreditPatentParam) {
  const res = await request.get<ApiResult<PageResult<CreditPatent>>>(
    '/credit/credit-patent/page',
    { params: withCreditUserScope(params) }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询专利列表
 */
export async function listCreditPatent(params?: CreditPatentParam) {
  const res = await request.get<ApiResult<CreditPatent[]>>(
    '/credit/credit-patent',
    { 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 addCreditPatent(data: CreditPatent) {
  const res = await request.post<ApiResult<unknown>>(
    '/credit/credit-patent',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改专利
 */
export async function updateCreditPatent(data: CreditPatent) {
  const res = await request.put<ApiResult<unknown>>(
    '/credit/credit-patent',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除专利
 */
export async function removeCreditPatent(id?: number) {
  const res = await request.delete<ApiResult<unknown>>(
    '/credit/credit-patent/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除专利
 */
export async function removeBatchCreditPatent(data: (number | undefined)[]) {
  const res = await request.delete<ApiResult<unknown>>(
    '/credit/credit-patent/batch',
    {
      data
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 根据id查询专利
 */
export async function getCreditPatent(id: number) {
  const res = await request.get<ApiResult<CreditPatent>>(
    '/credit/credit-patent/' + id
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 导入专利
 */
export async function importCreditPatent(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-patent/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));
}
