import api from '@/utils/http'

/**
 * 获取商户列表
 */
export function fetchMerchantList(params?: Partial<Api.Merchant.MerchantListParams>) {
  return api.get<Api.Merchant.MerchantListResponse>({
    url: '/api/admin/v1/merchants',
    params
  })
}

/**
 * 获取商户详情
 */
export function fetchMerchantDetail(merchantId: string, options?: { showErrorMessage?: boolean }) {
  return api.get<Api.Merchant.MerchantDetail>({
    url: `/api/admin/v1/merchants/${merchantId}`,
    showErrorMessage: options?.showErrorMessage
  })
}

/**
 * 更新商户信息
 */
export function fetchUpdateMerchant(
  merchantId: string,
  data: Api.Merchant.UpdateMerchantRequest,
  options?: { showErrorMessage?: boolean }
) {
  return api.put<Api.Merchant.UpdateMerchantResult>({
    url: `/api/admin/v1/merchants/${merchantId}`,
    data,
    showErrorMessage: options?.showErrorMessage
  })
}

/**
 * 获取商户审核历史
 */
export function fetchMerchantAuditHistory(merchantId: string) {
  return api.get<Api.Merchant.MerchantAuditRecord[]>({
    url: `/api/admin/v1/merchants/${merchantId}/audit-history`
  })
}

/**
 * 获取商户变更历史
 */
export function fetchMerchantChangeLogs(merchantId: string, params?: { fieldName?: string }) {
  return api.get<Api.Merchant.MerchantChangeLogRecord[]>({
    url: `/api/admin/v1/merchants/${merchantId}/change-history`,
    params
  })
}

/**
 * 获取待审核商户列表
 */
export function fetchPendingReviewList(params?: Partial<Api.Merchant.MerchantListParams>) {
  return api.get<Api.Merchant.MerchantReviewListResponse>({
    url: '/api/admin/v1/merchants/pending-review',
    params
  })
}

/**
 * 获取审核领取信息
 */
export function fetchMerchantReviewClaim(merchantId: string) {
  return api.get<Api.Merchant.ClaimInfo | null>({
    url: `/api/admin/v1/merchants/${merchantId}/review/claim`
  })
}

/**
 * 领取审核
 */
export function fetchClaimMerchantReview(merchantId: string) {
  return api.post<Api.Merchant.ClaimInfo>({
    url: `/api/admin/v1/merchants/${merchantId}/review/claim`,
    data: {}
  })
}

/**
 * 释放审核
 */
export function fetchReleaseMerchantReview(merchantId: string) {
  return api.del<Api.Merchant.ClaimInfo | null>({
    url: `/api/admin/v1/merchants/${merchantId}/review/claim`
  })
}

/**
 * 执行审核
 */
export function fetchReviewMerchant(merchantId: string, data: Api.Merchant.ReviewMerchantRequest) {
  return api.post<void>({
    url: `/api/admin/v1/merchants/${merchantId}/review`,
    data
  })
}

/**
 * 撤销审核
 */
export function fetchRevokeMerchantReview(
  merchantId: string,
  data: Api.Merchant.RevokeMerchantRequest
) {
  return api.post<void>({
    url: `/api/admin/v1/merchants/${merchantId}/review/revoke`,
    data
  })
}

/**
 * 导出商户 PDF
 */
export function fetchExportMerchantPdf(merchantId: string) {
  return api.get<Blob>({
    url: `/api/admin/v1/merchants/${merchantId}/export-pdf`,
    responseType: 'blob'
  })
}
