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

import type { ApiResult, PageResult } from '@/api';
import type {
  CreditCourtAnnouncement,
  CreditCourtAnnouncementParam
} from './model';

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

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

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

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

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

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

/**
 * 导入法院公告司法大数据
 */
export async function importCreditCourtAnnouncement(
  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-announcement/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 importCreditCourtAnnouncementHistory(
  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-announcement/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));
}
