import request from '@/utils/request'

// 债权管理API服务

/**
 * 获取债权列表
 * @param {Object} query - 查询参数
 * @param {string} query.pageNum - 页码
 * @param {string} query.pageSize - 每页数量
 * @param {string} query.status - 债权状态
 * @param {string} query.contractId - 关联合同ID
 * @param {string} query.securityCompanyId - 安保公司ID
 * @returns {Promise} API响应
 */
export function listCredits(query) {
  return request({
    url: '/api/credit/credits',
    method: 'get',
    params: query
  })
}

/**
 * 获取债权详情
 * @param {string} id - 债权ID
 * @returns {Promise} API响应
 */
export function getCredit(id) {
  return request({
    url: `/api/credit/credits/${id}`,
    method: 'get'
  })
}

/**
 * 生成债权凭证
 * @param {Object} data - 债权数据
 * @param {string} data.contractId - 关联合同ID
 * @param {number} data.amount - 债权金额
 * @param {string} data.issueDate - 发行日期
 * @param {string} data.maturityDate - 到期日期
 * @param {string} data.description - 债权描述
 * @returns {Promise} API响应
 */
export function generateCredit(data) {
  return request({
    url: '/api/credit/credits',
    method: 'post',
    data: data
  })
}

/**
 * 更新债权信息
 * @param {Object} data - 债权数据
 * @returns {Promise} API响应
 */
export function updateCredit(data) {
  return request({
    url: '/api/credit/credits',
    method: 'put',
    data: data
  })
}

/**
 * 删除债权
 * @param {string} id - 债权ID
 * @returns {Promise} API响应
 */
export function delCredit(id) {
  return request({
    url: `/api/credit/credits/${id}`,
    method: 'delete'
  })
}

/**
 * 验证债权有效性（后端自动验证）
 * @param {string} id - 债权ID
 * @returns {Promise} API响应
 */
export function validateCredit(id) {
  return request({
    url: `/api/credit/credits/${id}/validate`,
    method: 'post'
  })
}



/**
 * 获取债权数字签名
 * @param {string} id - 债权ID
 * @returns {Promise} API响应
 */
export function getCreditSignature(id) {
  return request({
    url: `/api/credit/credits/${id}/signature`,
    method: 'get'
  })
}

/**
 * 转让债权
 * @param {string} id - 债权ID
 * @param {Object} data - 转让数据
 * @param {string} data.transfereeId - 受让方ID
 * @param {number} data.transferAmount - 转让金额
 * @param {string} data.transferNote - 转让备注
 * @returns {Promise} API响应
 */
export function transferCredit(id, data) {
  return request({
    url: `/api/credit/credits/${id}/transfer`,
    method: 'put',
    data: data
  })
}

/**
 * 获取债权转让历史
 * @param {string} id - 债权ID
 * @returns {Promise} API响应
 */
export function getCreditTransferHistory(id) {
  return request({
    url: `/api/credit/credits/${id}/transfer-history`,
    method: 'get'
  })
}

/**
 * 获取债权统计信息
 * @returns {Promise} API响应
 */
export function getCreditStats() {
  return request({
    url: '/api/credit/credits/stats',
    method: 'get'
  })
}

/**
 * 导出债权列表
 * @param {Object} query - 查询参数
 * @returns {Promise} API响应
 */
export function exportCredit(query) {
  return request({
    url: '/api/credit/credits/export',
    method: 'get',
    params: query,
    responseType: 'blob'
  })
}

/**
 * 获取债权区块链信息
 * @param {string} id - 债权ID
 * @returns {Promise} API响应
 */
export function getCreditBlockchainInfo(id) {
  return request({
    url: `/api/credit/credits/${id}/blockchain`,
    method: 'get'
  })
}

/**
 * 导出债权凭证
 * @param {string} id - 债权ID
 * @returns {Promise} API响应
 */
export function exportCreditCertificate(id) {
  return request({
    url: `/api/credit/credits/${id}/certificate`,
    method: 'get',
    responseType: 'blob'
  })
}