import {
  clearChatConversationRoom,
  createChatConversationRoom,
  deleteChatConversationRequest,
  deleteChatConversationRoom,
  fetchChatConversationDetail,
  fetchChatConversations,
  fetchChatRuntimeJobDetail,
  fetchChatRuntimeSnapshot,
  markChatConversationResponsesRead,
  updateChatConversationRoom,
  uploadChatComposerFile,
} from '../../mainChatPanel';
import {
  dismissChatWebPushNotifications,
  markChatNotificationMessagesAsRead,
} from '../../notificationApi';
import type {
  ChatComposerAttachment,
  ChatConversationDetailResponse,
  ChatConversationSummary,
  ChatRuntimeJobDetail,
  ChatRuntimeSnapshot,
} from '../../mainChatPanel/types';

export type ChatGateway = {
  listConversations: () => Promise<ChatConversationSummary[]>;
  getConversationDetail: (
    sessionId: string,
    options?: {
      limit?: number;
      beforeMessageId?: number | null;
    },
  ) => Promise<ChatConversationDetailResponse>;
  createConversation: (args: {
    sessionId: string;
    title: string;
    draftText?: string | null;
    requestBadgeLabel?: string | null;
    codexModel?: string | null;
    chatTypeId?: string | null;
    lastChatTypeId?: string | null;
    generalSectionName?: string | null;
    contextLabel?: string;
    contextDescription?: string;
    notifyOffline?: boolean;
    roomScope?: Record<string, unknown> | null;
  }) => Promise<ChatConversationSummary>;
  updateConversation: (
    sessionId: string,
    payload: Partial<
      Pick<
        ChatConversationSummary,
        | 'title'
        | 'draftText'
        | 'requestBadgeLabel'
        | 'codexModel'
        | 'chatTypeId'
        | 'lastChatTypeId'
        | 'generalSectionName'
        | 'contextLabel'
        | 'contextDescription'
        | 'notifyOffline'
        | 'roomScope'
      >
    >,
  ) => Promise<ChatConversationSummary>;
  clearConversation: (sessionId: string) => Promise<ChatConversationSummary>;
  deleteConversation: (sessionId: string) => Promise<void>;
  deleteConversationRequest: (sessionId: string, requestId: string) => Promise<void>;
  markConversationRead: (sessionId: string) => Promise<void>;
  fetchRuntimeSnapshot: () => Promise<ChatRuntimeSnapshot>;
  fetchRuntimeJobDetail: (requestId: string) => Promise<ChatRuntimeJobDetail>;
  uploadComposerFile: (sessionId: string, file: File) => Promise<ChatComposerAttachment>;
  dismissRoomNotifications: (sessionId: string) => Promise<void>;
  markRoomNotificationMessagesRead: (sessionId: string) => Promise<void>;
};

export const chatGateway: ChatGateway = {
  listConversations: fetchChatConversations,
  getConversationDetail: fetchChatConversationDetail,
  createConversation: createChatConversationRoom,
  updateConversation: updateChatConversationRoom,
  clearConversation: clearChatConversationRoom,
  deleteConversation: async (sessionId) => {
    await deleteChatConversationRoom(sessionId);
  },
  deleteConversationRequest: async (sessionId, requestId) => {
    await deleteChatConversationRequest(sessionId, requestId);
  },
  markConversationRead: async (sessionId) => {
    await markChatConversationResponsesRead(sessionId);
  },
  fetchRuntimeSnapshot: fetchChatRuntimeSnapshot,
  fetchRuntimeJobDetail: fetchChatRuntimeJobDetail,
  uploadComposerFile: uploadChatComposerFile,
  dismissRoomNotifications: async (sessionId) => {
    await dismissChatWebPushNotifications(sessionId);
  },
  markRoomNotificationMessagesRead: async (sessionId) => {
    await markChatNotificationMessagesAsRead(sessionId);
  },
};
