import request from './request'

/**
 * 邮件记录相关 API
 */

/**
 * 获取邮件列表（分页）
 * @param {Object} params - 查询参数
 * @param {number} [params.latestId] - 最后一条记录的ID，用于游标分页
 * @returns {Promise<{success: boolean, data: Array, total: number, lastId: number}>}
 */
export const getEmailList = (params = {}) => {
  return request({
    url: '/EmailMessage/GetList',
    method: 'get',
    params
  })
}

/**
 * 根据ID获取邮件详情
 * @param {number} id - 邮件ID
 * @returns {Promise<{success: boolean, data: Object}>}
 */
export const getEmailDetail = (id) => {
  return request({
    url: `/EmailMessage/GetById/${id}`,
    method: 'get'
  })
}

/**
 * 删除邮件
 * @param {number} id - 邮件ID
 * @returns {Promise<{success: boolean}>}
 */
export const deleteEmail = (id) => {
  return request({
    url: `/EmailMessage/DeleteById`,
    method: 'post',
    params: { id }
  })
}

/**
 * 重新分析邮件并刷新交易记录
 * @param {number} id - 邮件ID
 * @returns {Promise<{success: boolean}>}
 */
export const refreshTransactionRecords = (id) => {
  return request({
    url: `/EmailMessage/RefreshTransactionRecords`,
    method: 'post',
    params: { id }
  })
}

/**
 * 立即同步邮件
 * @returns {Promise<{success: boolean, message: string}>}
 */
export const syncEmails = () => {
  return request({
    url: `/EmailMessage/SyncEmails`,
    method: 'post'
  })
}

/**
 * 获取邮件关联的交易记录列表
 * @param {number} emailId - 邮件ID
 * @returns {Promise<{success: boolean, data: Array}>}
 */
export const getEmailTransactions = (emailId) => {
  return request({
    url: `/TransactionRecord/GetByEmailId/${emailId}`,
    method: 'get'
  })
}
