import request from '@/utils/request'

// 贷款一致性验证API服务

/**
 * 获取通过验证的贷款列表（后端自动验证）
 * @param {Object} query - 查询参数
 * @param {string} query.pageNum - 页码
 * @param {string} query.pageSize - 每页数量
 * @param {string} query.creditNumber - 债权编号
 * @param {string} query.contractNumber - 合同编号
 * @param {string} query.securityCompany - 安保公司
 * @param {string} query.loanStatus - 贷款状态
 * @returns {Promise} API响应
 */
export function getValidatedLoanList(query) {
  return request({
    url: '/credit/loan-consistency/validated-loans',
    method: 'get',
    params: query
  })
}

/**
 * 重新验证指定贷款记录
 * @param {string} loanId - 贷款ID
 * @returns {Promise} API响应
 */
export function revalidateLoan(loanId) {
  return request({
    url: `/credit/loan-consistency/revalidate/${loanId}`,
    method: 'put'
  })
}

/**
 * 触发后台批量验证
 * @returns {Promise} API响应
 */
export function validateAllLoans() {
  return request({
    url: '/credit/loan-consistency/validate-all',
    method: 'post'
  })
}

/**
 * 触发简化版后台批量验证（用于调试）
 * @returns {Promise} API响应
 */
export function validateAllLoansSimple() {
  return request({
    url: '/credit/loan-consistency/validate-all-simple',
    method: 'post'
  })
}

/**
 * 获取验证统计信息
 * @returns {Promise} API响应
 */
export function getValidationStats() {
  return request({
    url: '/credit/loan-consistency/statistics',
    method: 'get'
  })
}

/**
 * 获取贷款验证状态详情
 * @param {string} loanId - 贷款ID
 * @returns {Promise} API响应
 */
export function getLoanValidationStatus(loanId) {
  return request({
    url: `/credit/loan-consistency/validate/${loanId}`,
    method: 'post'
  })
}

/**
 * 清除指定贷款验证缓存
 * @param {string} loanId - 贷款ID
 * @returns {Promise} API响应
 */
export function clearLoanValidationCache(loanId) {
  return request({
    url: `/credit/loan-consistency/cache/${loanId}`,
    method: 'delete'
  })
}

/**
 * 清除所有验证缓存
 * @returns {Promise} API响应
 */
export function clearAllValidationCache() {
  return request({
    url: '/credit/loan-consistency/cache',
    method: 'delete'
  })
}

/**
 * 放款操作
 * @param {Object} disburseData - 放款数据
 * @param {string} disburseData.loanId - 贷款ID
 * @param {string} disburseData.creditId - 债权ID
 * @param {string} disburseData.disbursementAccount - 放款账户
 * @param {string} disburseData.disbursementNote - 放款备注
 * @returns {Promise} API响应
 */
export function disburseLoan(disburseData) {
  return request({
    url: '/api/credit/financing/disburse',
    method: 'post',
    data: disburseData
  })
}

/**
 * 启动后台验证机制
 * @returns {Promise} API响应
 */
export function startBackgroundValidation() {
  return request({
    url: '/credit/loan-consistency/background/start',
    method: 'post'
  })
}

/**
 * 停止后台验证机制
 * @returns {Promise} API响应
 */
export function stopBackgroundValidation() {
  return request({
    url: '/credit/loan-consistency/background/stop',
    method: 'post'
  })
}

/**
 * 获取后台验证状态
 * @returns {Promise} API响应
 */
export function getBackgroundValidationStatus() {
  return request({
    url: '/credit/loan-consistency/background-status',
    method: 'get'
  })
}

/**
 * 获取调试信息
 * @returns {Promise} API响应
 */
export function getDebugInfo() {
  return request({
    url: '/credit/loan-consistency/debug-info',
    method: 'get'
  })
}

/**
 * 获取数据库表统计信息
 * @returns {Promise} API响应
 */
export function getDebugTablesInfo() {
  return request({
    url: '/credit/loan-consistency/debug-tables',
    method: 'get'
  })
}