import { request } from '@/utils/request'
import type { PageParams, PageResult } from '@/types/api'

/**
 * 系统公告 API
 *
 * 后端契约：com.snack.server.module.notice.controller.NoticeAdminController
 *   GET    /api/admin/notice/page                    → Page<NoticeVO>
 *   GET    /api/admin/notice/{id}                    → NoticeVO
 *   POST   /api/admin/notice                         → Long
 *   PUT    /api/admin/notice                         → Void
 *   DELETE /api/admin/notice/{id}                    → Void
 *   PUT    /api/admin/notice/{id}/status/{status}    → Void
 *   PUT    /api/admin/notice/{id}/top/{isTop}        → Void
 */

export interface NoticeItem {
  id: number
  title: string
  content: string
  type: 0 | 1 | 2
  isTop: 0 | 1
  status: 0 | 1
  startTime: string
  endTime: string
  viewCount: number
  publisherId: number
  createTime: string
  updateTime: string
}

export interface NoticeSavePayload {
  id?: number
  title: string
  content: string
  type: 0 | 1 | 2
  isTop?: 0 | 1
  status?: 0 | 1
  startTime?: string
  endTime?: string
}

export interface NoticePageQuery extends PageParams {
  keyword?: string
  type?: 0 | 1 | 2
  status?: 0 | 1
}

export function getNoticePageApi(params: NoticePageQuery) {
  return request<PageResult<NoticeItem>>({
    url: '/api/admin/notice/page',
    method: 'GET',
    params
  })
}

export function getNoticeDetailApi(id: number) {
  return request<NoticeItem>({
    url: `/api/admin/notice/${id}`,
    method: 'GET'
  })
}

export function createNoticeApi(payload: NoticeSavePayload) {
  return request<number>({
    url: '/api/admin/notice',
    method: 'POST',
    data: payload
  })
}

export function updateNoticeApi(payload: NoticeSavePayload) {
  return request<void>({
    url: '/api/admin/notice',
    method: 'PUT',
    data: payload
  })
}

export function deleteNoticeApi(id: number) {
  return request<void>({
    url: `/api/admin/notice/${id}`,
    method: 'DELETE'
  })
}

export function updateNoticeStatusApi(id: number, status: 0 | 1) {
  return request<void>({
    url: `/api/admin/notice/${id}/status/${status}`,
    method: 'PUT'
  })
}

export function updateNoticeTopApi(id: number, isTop: 0 | 1) {
  return request<void>({
    url: `/api/admin/notice/${id}/top/${isTop}`,
    method: 'PUT'
  })
}
