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

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

/** 公告列表项 */
export interface AnnouncementItem {
  id: number
  userAvatar: string
  userName: string
  prizeLevel: string
  prizeName: string
  sort: number
  isEnabled: boolean
  createdAt: string
  updatedAt: string
}

/** 公告列表查询参数 */
export interface AnnouncementListQuery {
  page: number
  pageSize: number
  keyword?: string
}

/** 创建公告请求 */
export interface CreateAnnouncementRequest {
  userAvatar?: string
  userName: string
  prizeLevel: string
  prizeName: string
  sort?: number
  isEnabled?: boolean
}

/** 更新公告请求 */
export interface UpdateAnnouncementRequest {
  userAvatar?: string
  userName?: string
  prizeLevel?: string
  prizeName?: string
  sort?: number
  isEnabled?: boolean
}

// ==================== 公告管理 API ====================

/** 获取公告列表（分页） */
export function getAnnouncementList(params: AnnouncementListQuery): Promise<ApiResponse<PagedResult<AnnouncementItem>>> {
  return request({
    url: '/admin/announcements',
    method: 'get',
    params
  })
}

/** 获取公告详情 */
export function getAnnouncementById(id: number): Promise<ApiResponse<AnnouncementItem>> {
  return request({
    url: `/admin/announcements/${id}`,
    method: 'get'
  })
}

/** 创建公告 */
export function createAnnouncement(data: CreateAnnouncementRequest): Promise<ApiResponse<AnnouncementItem>> {
  return request({
    url: '/admin/announcements',
    method: 'post',
    data
  })
}

/** 更新公告 */
export function updateAnnouncement(id: number, data: UpdateAnnouncementRequest): Promise<ApiResponse<AnnouncementItem>> {
  return request({
    url: `/admin/announcements/${id}`,
    method: 'put',
    data
  })
}

/** 删除公告 */
export function deleteAnnouncement(id: number): Promise<ApiResponse<string>> {
  return request({
    url: `/admin/announcements/${id}`,
    method: 'delete'
  })
}

/** 切换公告启用状态 */
export function toggleAnnouncementStatus(id: number): Promise<ApiResponse<AnnouncementItem>> {
  return request({
    url: `/admin/announcements/${id}/status`,
    method: 'patch'
  })
}
