import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type { Notification, NotificationParam, UnreadCountResult } from './model';

const BASE = '/app/notification';

/** 分页查询通知列表 */
export async function pageNotification(params: NotificationParam) {
  const res = await request.get<ApiResult<PageResult<Notification>>>(BASE + '/page', { params });
  if (res.code === 0) return res.data;
  return Promise.reject(new Error(res.message));
}

/** 查询最近通知 */
export async function listRecentNotification(params?: { type?: string; limit?: number }) {
  const res = await request.get<ApiResult<Notification[]>>(BASE + '/recent', { params });
  if (res.code === 0 && res.data) return res.data;
  return Promise.reject(new Error(res.message));
}

/** 获取未读数量统计 */
export async function getUnreadCount() {
  const res = await request.get<ApiResult<UnreadCountResult>>(BASE + '/unread-count');
  if (res.code === 0 && res.data) return res.data;
  return Promise.reject(new Error(res.message));
}

/** 标记单条通知为已读 */
export async function markNotificationRead(id: number) {
  const res = await request.put<ApiResult<unknown>>(BASE + `/read/${id}`);
  if (res.code === 0) return res.message;
  return Promise.reject(new Error(res.message));
}

/** 标记全部已读 */
export async function markAllNotificationRead(data?: { type?: string }) {
  const res = await request.put<ApiResult<unknown>>(BASE + '/read-all', data);
  if (res.code === 0) return res.message;
  return Promise.reject(new Error(res.message));
}

/** 删除单条通知 */
export async function removeNotification(id: number) {
  const res = await request.del<ApiResult<unknown>>(BASE + '/' + id);
  if (res.code === 0) return res.message;
  return Promise.reject(new Error(res.message));
}
