import request from '@/utils/http'

/**
 * 订阅管理 API
 */

/**
 * 获取订阅列表
 * @param params 查询参数
 */
export function getSubscriptionList(params?: Api.Subscription.SubscriptionListParams) {
  return request.get<Api.Subscription.SubscriptionListResponse>({
    url: '/api/admin/v1/subscriptions',
    params
  })
}

/**
 * 获取订阅详情
 * @param id 订阅ID
 */
export function getSubscriptionDetail(id: string) {
  return request.get<Api.Subscription.SubscriptionDetailDto>({
    url: `/api/admin/v1/subscriptions/${id}`
  })
}

/**
 * 更新订阅
 * @param id 订阅ID
 * @param data 更新数据
 */
export function updateSubscription(
  id: string,
  data: Omit<Api.Subscription.UpdateSubscriptionCommand, 'subscriptionId'>
) {
  return request.put<Api.Subscription.SubscriptionDto>({
    url: `/api/admin/v1/subscriptions/${id}`,
    data: {
      ...data,
      subscriptionId: id
    }
  })
}

/**
 * 延期订阅
 * @param id 订阅ID
 * @param data 延期数据
 */
export function extendSubscription(
  id: string,
  data: Omit<Api.Subscription.ExtendSubscriptionCommand, 'subscriptionId'>
) {
  return request.post<Api.Subscription.SubscriptionDto>({
    url: `/api/admin/v1/subscriptions/${id}/extend`,
    data: {
      ...data,
      subscriptionId: id
    }
  })
}

/**
 * 变更套餐
 * @param id 订阅ID
 * @param data 变更数据
 */
export function changeSubscriptionPlan(
  id: string,
  data: Omit<Api.Subscription.ChangePlanCommand, 'subscriptionId'>
) {
  return request.post<Api.Subscription.SubscriptionDto>({
    url: `/api/admin/v1/subscriptions/${id}/change-plan`,
    data: {
      ...data,
      subscriptionId: id
    }
  })
}

/**
 * 变更订阅状态
 * @param id 订阅ID
 * @param data 状态数据
 */
export function updateSubscriptionStatus(
  id: string,
  data: Omit<Api.Subscription.UpdateStatusCommand, 'subscriptionId'>
) {
  return request.post<Api.Subscription.SubscriptionDto>({
    url: `/api/admin/v1/subscriptions/${id}/status`,
    data: {
      ...data,
      subscriptionId: id
    }
  })
}

/**
 * 批量延期订阅
 * @param data 批量延期数据
 */
export function batchExtendSubscriptions(data: {
  subscriptionIds: string[]
  durationDays: number
  notes?: string
}) {
  return request.post<{
    successCount: number
    failureCount: number
    failures: Array<{ subscriptionId: string; reason: string }>
  }>({
    url: '/api/admin/v1/subscriptions/batch-extend',
    data,
    showSuccessMessage: true
  })
}

/**
 * 批量发送提醒
 * @param data 批量提醒数据
 */
export function batchSendReminders(data: { subscriptionIds: string[]; reminderContent: string }) {
  return request.post<{
    successCount: number
    failureCount: number
    failures: Array<{ subscriptionId: string; reason: string }>
  }>({
    url: '/api/admin/v1/subscriptions/batch-remind',
    data,
    showSuccessMessage: true
  })
}
