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

import type { ApiResult, PageResult } from '@/api';
import type { CreditCompetitor, CreditCompetitorParam } from './model';

/**
 * 分页查询竞争对手
 */
export async function pageCreditCompetitor(params: CreditCompetitorParam) {
  const res = await request.get<ApiResult<PageResult<CreditCompetitor>>>(
    '/credit/credit-competitor/page',
    { params: withCreditUserScope(params) }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询竞争对手列表
 */
export async function listCreditCompetitor(params?: CreditCompetitorParam) {
  const res = await request.get<ApiResult<CreditCompetitor[]>>(
    '/credit/credit-competitor',
    { 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 addCreditCompetitor(data: CreditCompetitor) {
  const res = await request.post<ApiResult<unknown>>(
    '/credit/credit-competitor',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改竞争对手
 */
export async function updateCreditCompetitor(data: CreditCompetitor) {
  const res = await request.put<ApiResult<unknown>>(
    '/credit/credit-competitor',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

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

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

/**
 * 根据id查询竞争对手
 */
export async function getCreditCompetitor(id: number) {
  const res = await request.get<ApiResult<CreditCompetitor>>(
    '/credit/credit-competitor/' + id
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 导入竞争对手
 */
export async function importCreditCompetitor(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-competitor/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));
}
