import { request, type ApiResponse, type PagedResult } from '@/utils/request'

// ==================== 广告跳转类型枚举 ====================

/** 广告跳转类型枚举 */
export enum AdvertJumpType {
  None = 0,        // 不跳转
  Coupon = 1,      // 优惠券
  YiFanShang = 2,  // 一番赏
  WuXianShang = 3, // 无限赏
  LianJiShang = 4, // 连击赏
  CustomUrl = 5    // 自定义URL
}

/** 广告跳转类型标签映射 */
export const AdvertJumpTypeLabels: Record<number, string> = {
  [AdvertJumpType.None]: '不跳转',
  [AdvertJumpType.Coupon]: '优惠券',
  [AdvertJumpType.YiFanShang]: '一番赏',
  [AdvertJumpType.WuXianShang]: '无限赏',
  [AdvertJumpType.LianJiShang]: '连击赏',
  [AdvertJumpType.CustomUrl]: '自定义URL',
}

// ==================== 广告相关类型定义 ====================

/** 广告列表查询参数 */
export interface AdvertListRequest {
  page: number
  pageSize: number
  typeId?: number
}

/** 广告响应 */
export interface AdvertResponse {
  id: number
  typeId: number
  typeName: string
  imageUrl: string
  sort: number
  jumpType: number
  jumpTypeName: string
  couponId?: number
  goodsId?: number
  urlLink?: string
  createdAt: string
  updatedAt?: string
}

/** 创建广告请求 */
export interface AdvertCreateRequest {
  typeId: number
  imageUrl: string
  sort: number
  jumpType: number
  couponId?: number
  goodsId?: number
  urlLink?: string
}

/** 更新广告请求 */
export interface AdvertUpdateRequest extends AdvertCreateRequest {}

// ==================== 广告类型相关类型定义 ====================

/** 广告类型响应 */
export interface AdvertTypeResponse {
  id: number
  name: string
  sort: number
  advertCount: number
}

/** 创建广告类型请求 */
export interface AdvertTypeCreateRequest {
  name: string
  sort: number
}

/** 更新广告类型请求 */
export interface AdvertTypeUpdateRequest extends AdvertTypeCreateRequest {}

// ==================== API 基础路径 ====================

const ADVERT_BASE_URL = '/admin/business/advert'

// ==================== 广告 API ====================

/**
 * 获取广告列表
 * @param params 查询参数
 * @returns 分页广告列表
 */
export function getAdverts(params: AdvertListRequest): Promise<ApiResponse<PagedResult<AdvertResponse>>> {
  return request({
    url: ADVERT_BASE_URL,
    method: 'get',
    params
  })
}

/**
 * 获取广告详情
 * @param id 广告ID
 * @returns 广告详情
 */
export function getAdvertById(id: number): Promise<ApiResponse<AdvertResponse>> {
  return request({
    url: `${ADVERT_BASE_URL}/${id}`,
    method: 'get'
  })
}

/**
 * 创建广告
 * @param data 创建请求数据
 * @returns 新广告ID
 */
export function createAdvert(data: AdvertCreateRequest): Promise<ApiResponse<{ id: number }>> {
  return request({
    url: ADVERT_BASE_URL,
    method: 'post',
    data
  })
}

/**
 * 更新广告
 * @param id 广告ID
 * @param data 更新请求数据
 * @returns 操作结果
 */
export function updateAdvert(id: number, data: AdvertUpdateRequest): Promise<ApiResponse<string>> {
  return request({
    url: `${ADVERT_BASE_URL}/${id}`,
    method: 'put',
    data
  })
}

/**
 * 删除广告
 * @param id 广告ID
 * @returns 操作结果
 */
export function deleteAdvert(id: number): Promise<ApiResponse<string>> {
  return request({
    url: `${ADVERT_BASE_URL}/${id}`,
    method: 'delete'
  })
}

// ==================== 广告类型 API ====================

/**
 * 获取广告类型列表
 * @returns 广告类型列表
 */
export function getAdvertTypes(): Promise<ApiResponse<AdvertTypeResponse[]>> {
  return request({
    url: `${ADVERT_BASE_URL}/types`,
    method: 'get'
  })
}

/**
 * 创建广告类型
 * @param data 创建请求数据
 * @returns 新类型ID
 */
export function createAdvertType(data: AdvertTypeCreateRequest): Promise<ApiResponse<{ id: number }>> {
  return request({
    url: `${ADVERT_BASE_URL}/types`,
    method: 'post',
    data
  })
}

/**
 * 更新广告类型
 * @param id 类型ID
 * @param data 更新请求数据
 * @returns 操作结果
 */
export function updateAdvertType(id: number, data: AdvertTypeUpdateRequest): Promise<ApiResponse<string>> {
  return request({
    url: `${ADVERT_BASE_URL}/types/${id}`,
    method: 'put',
    data
  })
}

/**
 * 删除广告类型
 * @param id 类型ID
 * @returns 操作结果
 */
export function deleteAdvertType(id: number): Promise<ApiResponse<string>> {
  return request({
    url: `${ADVERT_BASE_URL}/types/${id}`,
    method: 'delete'
  })
}
