import type { Recordable } from '@vben/types';

import { requestClient } from '#/api/request';

export namespace NotificationApi {
  export interface NotificationItem {
    id: number;
    title: string;
    content?: string;
    notification_page: string;
    start_date: string;
    start_time: string;
    end_date: string;
    end_time: string;
    status: number;
    create_time: string;
    update_time: string;
  }

  export interface NotificationList {
    total: number;
    items: NotificationItem[];
  }

  export interface CreateNotificationRequest {
    title: string;
    content: string;
    notification_page: string;
    start_date: string;
    start_time: string;
    end_date: string;
    end_time: string;
    status: 0 | 1;
  }

  export interface CreateNotificationResponse {
    id: number;
  }

  export interface UpdateNotificationRequest
    extends Partial<CreateNotificationRequest> {
    id: number;
  }

  export interface UpdateNotificationResponse {
    success: boolean;
  }

  export interface DeleteNotificationResponse {
    success: boolean;
  }
}

/**
 * 获取通知列表
 */
async function getNotificationList(params: Recordable<any>) {
  return requestClient.get<NotificationApi.NotificationList>(
    '/notification/list',
    {
      // 某些环境下 GET 列表会被缓存，导致编辑后 query/search 仍看到旧数据。
      // 增加时间戳参数与 no-cache 头，确保每次都拿最新列表。
      params: {
        ...params,
        _ts: Date.now(),
      },
      headers: {
        'Cache-Control': 'no-cache',
        Pragma: 'no-cache',
      },
    },
  );
}

/**
 * 获取通知详情（按 ID）
 */
async function getNotificationDetail(id: number) {
  return requestClient.get<NotificationApi.NotificationItem>(
    `/notification/detail/${id}`,
  );
}

/**
 * 创建通知
 */
async function createNotification(
  data: NotificationApi.CreateNotificationRequest,
) {
  return requestClient.post<NotificationApi.CreateNotificationResponse>(
    '/notification/create',
    data,
  );
}

/**
 * 更新通知
 */
async function updateNotification(
  id: number,
  data: NotificationApi.UpdateNotificationRequest,
) {
  return requestClient.put<NotificationApi.UpdateNotificationResponse>(
    `/notification/update/${id}`,
    data,
  );
}

/**
 * 删除通知
 */
async function deleteNotification(id: number) {
  return requestClient.delete<NotificationApi.DeleteNotificationResponse>(
    `/notification/delete/${id}`,
  );
}

export {
  createNotification,
  deleteNotification,
  getNotificationDetail,
  getNotificationList,
  updateNotification,
};
