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

import type { ApiResult, PageResult } from '@/api';
import type { CreditJudgmentDebtor, CreditJudgmentDebtorParam } from './model';

/**
 * 分页查询被执行人
 */
export async function pageCreditJudgmentDebtor(
  params: CreditJudgmentDebtorParam
) {
  const res = await request.get<ApiResult<PageResult<CreditJudgmentDebtor>>>(
    '/credit/credit-judgment-debtor/page',
    withCreditUserScope(params)
  );
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 查询被执行人列表
 */
export async function listCreditJudgmentDebtor(
  params?: CreditJudgmentDebtorParam
) {
  const res = await request.get<ApiResult<CreditJudgmentDebtor[]>>(
    '/credit/credit-judgment-debtor',
    withCreditUserScope(params)
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 添加被执行人
 */
export async function addCreditJudgmentDebtor(data: CreditJudgmentDebtor) {
  const res = await request.post<ApiResult<unknown>>(
    '/credit/credit-judgment-debtor',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 修改被执行人
 */
export async function updateCreditJudgmentDebtor(data: CreditJudgmentDebtor) {
  const res = await request.put<ApiResult<unknown>>(
    '/credit/credit-judgment-debtor',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 删除被执行人
 */
export async function removeCreditJudgmentDebtor(id?: number) {
  const res = await request.del<ApiResult<unknown>>(
    '/credit/credit-judgment-debtor/' + id
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 批量删除被执行人
 */
export async function removeBatchCreditJudgmentDebtor(
  data: (number | undefined)[]
) {
  const res = await request.del<ApiResult<unknown>>(
    '/credit/credit-judgment-debtor/batch',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 根据id查询被执行人
 */
export async function getCreditJudgmentDebtor(id: number) {
  const res = await request.get<ApiResult<CreditJudgmentDebtor>>(
    '/credit/credit-judgment-debtor/' + id
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 导入被执行人
 */
export async function importCreditJudgmentDebtor(
  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-judgment-debtor/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 importCreditJudgmentDebtorHistory(
  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-judgment-debtor/import/history',
    formData,
    {
      header: {
        'Content-Type': 'multipart/form-data'
      }
    }
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 修正被执行人主体企业归属（按企业名称匹配回填 companyId）
 *
 * 后端: POST /api/credit/credit-judgment-debtor/company-id/refresh
 * - onlyNull: 默认 true，仅更新 companyId 为空的数据
 * - limit: 可选，限制处理条数
 */
export async function refreshCreditJudgmentDebtorCompanyId(params?: {
  onlyNull?: boolean;
  limit?: number;
}) {
  return refreshCreditCompanyId('credit-judgment-debtor', params);
}
