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

import type { ApiResult, PageResult } from '@/api';
import type { CreditCourtSession, CreditCourtSessionParam } from './model';

/**
 * 分页查询开庭公告司法大数据
 */
export async function pageCreditCourtSession(params: CreditCourtSessionParam) {
  const res = await request.get<ApiResult<PageResult<CreditCourtSession>>>(
    '/credit/credit-court-session/page',
    { params: withCreditUserScope(params) }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询开庭公告司法大数据列表
 */
export async function listCreditCourtSession(params?: CreditCourtSessionParam) {
  const res = await request.get<ApiResult<CreditCourtSession[]>>(
    '/credit/credit-court-session',
    { 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 addCreditCourtSession(data: CreditCourtSession) {
  const res = await request.post<ApiResult<unknown>>(
    '/credit/credit-court-session',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改开庭公告司法大数据
 */
export async function updateCreditCourtSession(data: CreditCourtSession) {
  const res = await request.put<ApiResult<unknown>>(
    '/credit/credit-court-session',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除开庭公告司法大数据
 */
export async function removeCreditCourtSession(id?: number) {
  const res = await request.delete<ApiResult<unknown>>(
    '/credit/credit-court-session/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除开庭公告司法大数据
 */
export async function removeBatchCreditCourtSession(
  data: (number | undefined)[]
) {
  const res = await request.delete<ApiResult<unknown>>(
    '/credit/credit-court-session/batch',
    {
      data
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 根据id查询开庭公告司法大数据
 */
export async function getCreditCourtSession(id: number) {
  const res = await request.get<ApiResult<CreditCourtSession>>(
    '/credit/credit-court-session/' + id
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 导入开庭公告司法大数据
 */
export async function importCreditCourtSession(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-court-session/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));
}

/**
 * 导入历史开庭公告司法大数据
 */
export async function importCreditCourtSessionHistory(
  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-court-session/import/history',
    formData,
    {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}
