import { apiClient } from '@/shared/api/client'
import type { ApiResponse } from '@/shared/types/types'
import type { Announcement, PaginatedResult } from '@/features/announcement/api/announcements'

export interface CreateAnnouncementRequest {
  title: string
  content: string
  category: 'notice' | 'tutorial' | 'rule' | 'faq'
  priority?: number
  is_pinned?: boolean
  is_important?: boolean
}

export interface UpdateAnnouncementRequest {
  title?: string
  content?: string
  category?: 'notice' | 'tutorial' | 'rule' | 'faq'
  priority?: number
  is_pinned?: boolean
  is_important?: boolean
}

export async function fetchAdminAnnouncements(params: {
  status?: string
  category?: string
  page?: number
  page_size?: number
}): Promise<PaginatedResult<Announcement>> {
  const { data } = await apiClient.get<ApiResponse<PaginatedResult<Announcement>>>(
    '/admin/announcements',
    { params }
  )
  return data.data
}

export async function fetchAdminAnnouncementDetail(id: number): Promise<Announcement> {
  const { data } = await apiClient.get<ApiResponse<Announcement>>(`/admin/announcements/${id}`)
  return data.data
}

export async function createAnnouncement(req: CreateAnnouncementRequest): Promise<Announcement> {
  const { data } = await apiClient.post<ApiResponse<Announcement>>('/admin/announcements', req)
  return data.data
}

export async function updateAnnouncement(
  id: number,
  req: UpdateAnnouncementRequest
): Promise<Announcement> {
  const { data } = await apiClient.put<ApiResponse<Announcement>>(`/admin/announcements/${id}`, req)
  return data.data
}

export async function publishAnnouncement(id: number): Promise<void> {
  await apiClient.post(`/admin/announcements/${id}/publish`)
}

export async function archiveAnnouncement(id: number): Promise<void> {
  await apiClient.post(`/admin/announcements/${id}/archive`)
}

export async function unarchiveAnnouncement(id: number): Promise<void> {
  await apiClient.post(`/admin/announcements/${id}/unarchive`)
}

export async function deleteAnnouncement(id: number): Promise<void> {
  await apiClient.delete(`/admin/announcements/${id}`)
}
