import type {
  AnnouncementLogDto,
  AnnouncementDto,
  AnnouncementCreateInput,
  AnnouncementUpdateInput,
  AnnouncementGetListInput,
  PagedResultDto,
} from './types';
import { del, get, post, put } from '@/utils/request';

// ==================== 前端首页用 ====================

/**
 * 获取系统公告和活动数据
 * 后端接口: GET /api/app/announcement
 * 返回格式: AnnouncementLogDto[]
 */
export function getSystemAnnouncements() {
  return get<AnnouncementLogDto[]>('/announcement').json();
}

// ==================== 后台管理用 ====================

// 获取公告列表
export function getList(params?: AnnouncementGetListInput) {
  const queryParams = new URLSearchParams();
  if (params?.searchKey) {
    queryParams.append('SearchKey', params.searchKey);
  }
  if (params?.skipCount !== undefined) {
    queryParams.append('SkipCount', params.skipCount.toString());
  }
  if (params?.maxResultCount !== undefined) {
    queryParams.append('MaxResultCount', params.maxResultCount.toString());
  }
  if (params?.type !== undefined) {
    queryParams.append('Type', params.type.toString());
  }

  const queryString = queryParams.toString();
  const url = queryString ? `/announcement/list?${queryString}` : '/announcement/list';

  return get<PagedResultDto<AnnouncementDto>>(url).json();
}

// 根据ID获取公告
export function getById(id: string) {
  return get<AnnouncementDto>(`/announcement/${id}`).json();
}

// 创建公告
export function create(data: AnnouncementCreateInput) {
  return post<AnnouncementDto>('/announcement', data).json();
}

// 更新公告
export function update(data: AnnouncementUpdateInput) {
  return put<AnnouncementDto>('/announcement', data).json();
}

// 删除公告
export function deleteById(id: string) {
  return del(`/announcement/${id}`).json();
}

export * from './types';
