/**
 * 通知管理服务
 * 负责通知数据的API调用
 */

import apiClient from '@/infrastructure/api/client';
import { API_ENDPOINTS } from '@/infrastructure/api/endpoints';

/** 通知类型 */
export type NotificationType = 'SYSTEM' | 'ACTIVITY' | 'REWARD' | 'UPGRADE' | 'ANNOUNCEMENT';

/** 通知优先级 */
export type NotificationPriority = 'LOW' | 'NORMAL' | 'HIGH' | 'URGENT';

/** 目标用户类型 */
export type TargetType = 'ALL' | 'BY_TAG' | 'SPECIFIC';

/** 目标配置 */
export interface TargetConfig {
  type: TargetType;
  tagIds?: string[];
  accountSequences?: string[];
}

/** 通知项 */
export interface NotificationItem {
  id: string;
  title: string;
  content: string;
  type: NotificationType;
  priority: NotificationPriority;
  targetType: TargetType;
  targetConfig: TargetConfig | null;
  imageUrl: string | null;
  linkUrl: string | null;
  isEnabled: boolean;
  requiresForceRead: boolean;
  publishedAt: string | null;
  expiresAt: string | null;
  createdAt: string;
}

/** 创建通知请求 */
export interface CreateNotificationRequest {
  title: string;
  content: string;
  type: NotificationType;
  priority?: NotificationPriority;
  targetType?: TargetType;
  targetConfig?: {
    tagIds?: string[];
    accountSequences?: string[];
  };
  imageUrl?: string;
  linkUrl?: string;
  requiresForceRead?: boolean;
  publishedAt?: string;
  expiresAt?: string;
}

/** 更新通知请求 */
export interface UpdateNotificationRequest {
  title?: string;
  content?: string;
  type?: NotificationType;
  priority?: NotificationPriority;
  targetType?: TargetType;
  targetConfig?: {
    tagIds?: string[];
    accountSequences?: string[];
  };
  imageUrl?: string | null;
  linkUrl?: string | null;
  isEnabled?: boolean;
  requiresForceRead?: boolean;
  publishedAt?: string | null;
  expiresAt?: string | null;
}

/** 查询通知列表参数 */
export interface ListNotificationsParams {
  type?: NotificationType;
  limit?: number;
  offset?: number;
}

/** 通知类型选项 */
export const NOTIFICATION_TYPE_OPTIONS = [
  { value: 'SYSTEM', label: '系统通知', color: 'blue' },
  { value: 'ACTIVITY', label: '活动通知', color: 'green' },
  { value: 'REWARD', label: '收益通知', color: 'yellow' },
  { value: 'UPGRADE', label: '升级通知', color: 'purple' },
  { value: 'ANNOUNCEMENT', label: '公告', color: 'orange' },
] as const;

/** 通知优先级选项 */
export const NOTIFICATION_PRIORITY_OPTIONS = [
  { value: 'LOW', label: '低', color: 'gray' },
  { value: 'NORMAL', label: '普通', color: 'blue' },
  { value: 'HIGH', label: '高', color: 'orange' },
  { value: 'URGENT', label: '紧急', color: 'red' },
] as const;

/** 目标用户类型选项 */
export const TARGET_TYPE_OPTIONS = [
  { value: 'ALL', label: '全部用户', description: '发送给所有用户' },
  { value: 'BY_TAG', label: '按标签筛选', description: '发送给有指定标签的用户' },
  { value: 'SPECIFIC', label: '指定用户', description: '发送给特定用户列表' },
] as const;

/**
 * 通知管理服务
 */
export const notificationService = {
  /**
   * 获取通知列表
   */
  async getNotifications(params: ListNotificationsParams = {}): Promise<NotificationItem[]> {
    return apiClient.get(API_ENDPOINTS.NOTIFICATIONS.LIST, { params });
  },

  /**
   * 获取通知详情
   */
  async getNotification(id: string): Promise<NotificationItem> {
    return apiClient.get(API_ENDPOINTS.NOTIFICATIONS.DETAIL(id));
  },

  /**
   * 创建通知
   */
  async createNotification(data: CreateNotificationRequest): Promise<NotificationItem> {
    return apiClient.post(API_ENDPOINTS.NOTIFICATIONS.CREATE, data);
  },

  /**
   * 更新通知
   */
  async updateNotification(id: string, data: UpdateNotificationRequest): Promise<NotificationItem> {
    return apiClient.put(API_ENDPOINTS.NOTIFICATIONS.UPDATE(id), data);
  },

  /**
   * 删除通知
   */
  async deleteNotification(id: string): Promise<void> {
    return apiClient.delete(API_ENDPOINTS.NOTIFICATIONS.DELETE(id));
  },

  /**
   * 切换通知启用状态
   */
  async toggleNotification(id: string, isEnabled: boolean): Promise<NotificationItem> {
    return apiClient.put(API_ENDPOINTS.NOTIFICATIONS.UPDATE(id), { isEnabled });
  },
};

export default notificationService;
