import { request } from '.'

// 定义促销相关类型
export interface Promotion {
  id: number
  title: string
  description: string
  type: string
  discount_value: number
  start_date: string
  end_date: string
  status: string
  created_at: string
  updated_at: string
}

export interface PromotionQueryParams {
  page?: number
  limit?: number
  title?: string
  type?: string
  status?: string
  start_date?: string
  end_date?: string
}

export interface PromotionCreateData {
  title: string
  description: string
  type: string
  discount_value: number
  start_date: string
  end_date: string
  status?: string
}

export interface PromotionUpdateData {
  title?: string
  description?: string
  type?: string
  discount_value?: number
  start_date?: string
  end_date?: string
  status?: string
}

// 获取促销列表
export const getPromotions = (params?: PromotionQueryParams) =>
  request.get<{ success: boolean; code: number; message: string; data: { promotions: Promotion[]; pagination: any } }>('/promotions', { params })

// 获取促销详情
export const getPromotion = (id: number) =>
  request.get<{ success: boolean; code: number; message: string; data: { promotion: Promotion } }>(`/promotions/${id}`)

// 创建促销
export const createPromotion = (data: PromotionCreateData) =>
  request.post<{ success: boolean; code: number; message: string; data: { promotion: Promotion } }>('/promotions', data)

// 更新促销
export const updatePromotion = (id: number, data: PromotionUpdateData) =>
  request.put<{ success: boolean; code: number; message: string; data: { promotion: Promotion } }>(`/promotions/${id}`, data)

// 删除促销
export const deletePromotion = (id: number) =>
  request.delete<{ success: boolean; code: number; message: string }>(`/promotions/${id}`)

// 更新促销状态
export const updatePromotionStatus = (id: number, status: string) =>
  request.put<{ success: boolean; code: number; message: string }>(`/promotions/${id}/status`, { status })

export default {
  getPromotions,
  getPromotion,
  createPromotion,
  updatePromotion,
  deletePromotion,
  updatePromotionStatus
}

// 推广活动状态
export type PromotionStatus = 'active' | 'upcoming' | 'ended' | 'paused'

// 奖励类型
export type RewardType = 'cash' | 'points' | 'coupon'

// 奖励状态
export type RewardStatus = 'pending' | 'issued' | 'failed'

// 推广活动数据结构
export interface PromotionActivity {
  id: number
  name: string
  description: string
  reward_type: RewardType
  reward_amount: number
  status: PromotionStatus
  start_time: string
  end_time: string
  max_participants: number
  current_participants: number
  created_at: string
  updated_at: string
}

// 奖励记录
export interface RewardRecord {
  id: number
  user_id: number
  user_name: string
  user_phone: string
  activity_id: number
  activity_name: string
  reward_type: RewardType
  reward_amount: number
  status: RewardStatus
  issued_at: string
  created_at: string
}

// 推广活动查询参数
export interface PromotionQueryParams {
  page?: number
  pageSize?: number
  name?: string
  status?: PromotionStatus
  activityTime?: [string, string]
}

// 奖励记录查询参数
export interface RewardQueryParams {
  page?: number
  pageSize?: number
  user?: string
  reward_type?: RewardType
  status?: RewardStatus
}

// 统计数据
export interface PromotionStatistics {
  total_activities: number
  active_activities: number
  total_rewards: number
  issued_rewards: number
  total_amount: number
}

// API响应结构
export interface ApiResponse<T> {
  success: boolean
  code: number
  message: string
  data: T
  pagination?: {
    current: number
    pageSize: number
    total: number
    totalPages: number
  }
}

// 获取推广活动列表
export const getPromotionActivities = async (params?: PromotionQueryParams): Promise<ApiResponse<PromotionActivity[]>> => {
  return request.get<ApiResponse<PromotionActivity[]>>('/promotion/activities', { params })
}

// 获取推广活动详情
export const getPromotionActivity = async (id: number): Promise<ApiResponse<PromotionActivity>> => {
  return request.get<ApiResponse<PromotionActivity>>(`/promotion/activities/${id}`)
}

// 创建推广活动
export const createPromotionActivity = async (activityData: Partial<PromotionActivity>): Promise<ApiResponse<PromotionActivity>> => {
  return request.post<ApiResponse<PromotionActivity>>('/promotion/activities', activityData)
}

// 更新推广活动
export const updatePromotionActivity = async (id: number, activityData: Partial<PromotionActivity>): Promise<ApiResponse<PromotionActivity>> => {
  return request.put<ApiResponse<PromotionActivity>>(`/promotion/activities/${id}`, activityData)
}

// 删除推广活动
export const deletePromotionActivity = async (id: number): Promise<ApiResponse<void>> => {
  return request.delete<ApiResponse<void>>(`/promotion/activities/${id}`)
}

// 暂停推广活动
export const pausePromotionActivity = async (id: number): Promise<ApiResponse<void>> => {
  return request.post<ApiResponse<void>>(`/promotion/activities/${id}/pause`)
}

// 恢复推广活动
export const resumePromotionActivity = async (id: number): Promise<ApiResponse<void>> => {
  return request.post<ApiResponse<void>>(`/promotion/activities/${id}/resume`)
}

// 获取奖励记录列表
export const getRewardRecords = async (params?: RewardQueryParams): Promise<ApiResponse<RewardRecord[]>> => {
  return request.get<ApiResponse<RewardRecord[]>>('/promotion/rewards', { params })
}

// 发放奖励
export const issueReward = async (id: number): Promise<ApiResponse<void>> => {
  return request.post<ApiResponse<void>>(`/promotion/rewards/${id}/issue`)
}

// 获取推广统计数据
export const getPromotionStatistics = async (): Promise<ApiResponse<PromotionStatistics>> => {
  return request.get<ApiResponse<PromotionStatistics>>('/promotion/statistics')
}