import { api } from './api';
import type {
  ScheduledMessageRule,
  ScheduledMessagesListResponse,
  ScheduledMessageDetailResponse,
  RoomsResponse,
  OneTimeMessage,
} from '../types/scheduledMessages.types';

export const scheduledMessagesApi = {
  getAll: () =>
    api.get<ScheduledMessagesListResponse>('/api/scheduled-messages').then(r => r.data),

  getById: (id: string) =>
    api.get<ScheduledMessageDetailResponse>(`/api/scheduled-messages/${id}`).then(r => r.data),

  getRooms: () =>
    api.get<RoomsResponse>('/api/scheduled-messages/rooms').then(r => r.data),

  create: (data: Partial<ScheduledMessageRule>) =>
    api.post<ScheduledMessageDetailResponse>('/api/scheduled-messages', data).then(r => r.data),

  update: (id: string, data: Partial<ScheduledMessageRule>) =>
    api.patch<ScheduledMessageDetailResponse>(`/api/scheduled-messages/${id}`, data).then(r => r.data),

  delete: (id: string) =>
    api.delete(`/api/scheduled-messages/${id}`).then(r => r.data),

  subscribe: (id: string, roomToken: string) =>
    api.post(`/api/scheduled-messages/${id}/subscribe`, { room_token: roomToken }).then(r => r.data),

  unsubscribe: (id: string) =>
    api.delete(`/api/scheduled-messages/${id}/subscribe`).then(r => r.data),

  trigger: (id: string) =>
    api.post(`/api/scheduled-messages/${id}/trigger`).then(r => r.data),

  getMyBotRoom: () =>
    api.get<{ data: { room_token: string } }>('/api/scheduled-messages/my-bot-room').then(r => r.data),

  getOneTimeMessages: () =>
    api.get<{ data: OneTimeMessage[] }>('/api/scheduled-messages/one-time').then(r => r.data),

  createOneTimeMessage: (data: {
    message: string;
    target_room_token: string;
    target_room_name?: string;
    send_at: string;
  }) =>
    api.post<{ data: OneTimeMessage }>('/api/scheduled-messages/one-time', data).then(r => r.data),

  deleteOneTimeMessage: (id: string) =>
    api.delete(`/api/scheduled-messages/one-time/${id}`).then(r => r.data),
};
