import request from '@/utils/request';
import type { Page } from '@/types/common';
import type {
  NotificationChannel,
  NotificationChannelQuery,
  ChannelTypeOption,
  NotificationRequest,
} from './types';

const API_BASE = '/api/v1/notification-channel';

/**
 * 获取所有渠道类型
 */
export const getChannelTypes = () =>
  request.get<ChannelTypeOption[]>(`${API_BASE}/types`);

/**
 * 创建渠道
 */
export const createChannel = (data: NotificationChannel) =>
  request.post<NotificationChannel>(`${API_BASE}`, data);

/**
 * 更新渠道
 */
export const updateChannel = (id: number, data: NotificationChannel) =>
  request.put<NotificationChannel>(`${API_BASE}/${id}`, data);

/**
 * 删除渠道
 */
export const deleteChannel = (id: number) =>
  request.delete<void>(`${API_BASE}/${id}`);

/**
 * 查询单个渠道
 */
export const getChannelById = (id: number) =>
  request.get<NotificationChannel>(`${API_BASE}/${id}`);

/**
 * 查询所有渠道（不分页）
 */
export const getAllChannels = () =>
  request.get<NotificationChannel[]>(`${API_BASE}/list`);

/**
 * 查询所有渠道（带条件，不分页）
 */
export const findAllChannels = (query: NotificationChannelQuery) =>
  request.get<NotificationChannel[]>(`${API_BASE}/list`, { params: query });

/**
 * 分页查询渠道
 */
export const getChannelsPage = (query: NotificationChannelQuery) =>
  request.get<Page<NotificationChannel>>(`${API_BASE}/page`, { params: query });

/**
 * 测试渠道连接
 */
export const testConnection = (id: number) =>
  request.post<boolean>(`${API_BASE}/${id}/test`);

/**
 * 启用渠道
 */
export const enableChannel = (id: number) =>
  request.post<void>(`${API_BASE}/${id}/enable`);

/**
 * 禁用渠道
 */
export const disableChannel = (id: number) =>
  request.post<void>(`${API_BASE}/${id}/disable`);

/**
 * 发送通知消息
 */
export const sendNotification = (request: NotificationRequest) =>
  request.post<void>(`${API_BASE}/send`, request);

