import { Chat, Message, CreateChatRequest, SendMessageData, PaginatedResponse } from '../../../shared/types' import { apiService } from './api' class ChatService { // Chat management async getChats(): Promise { return await apiService.get('/api/chats') } async getChat(chatId: string): Promise { return await apiService.get(`/api/chats/${chatId}`) } async createChat(data: CreateChatRequest): Promise { return await apiService.post('/api/chats', data) } async updateChat(chatId: string, data: Partial): Promise { return await apiService.put(`/api/chats/${chatId}`, data) } async deleteChat(chatId: string): Promise { await apiService.delete(`/api/chats/${chatId}`) } async leaveChat(chatId: string): Promise { await apiService.post(`/api/chats/${chatId}/leave`) } // Message management async getMessages(chatId: string, page = 1, limit = 50): Promise { const response = await apiService.get>( `/api/chats/${chatId}/messages?page=${page}&limit=${limit}` ) return response.data } async sendMessage(chatId: string, data: Omit): Promise { if (data.attachments && data.attachments.length > 0) { // Upload files first const uploadedFiles = await this.uploadFiles(data.attachments) return await apiService.post(`/api/chats/${chatId}/messages`, { content: data.content, type: data.type, attachments: uploadedFiles, replyTo: data.replyTo, }) } else { return await apiService.post(`/api/chats/${chatId}/messages`, { content: data.content, type: data.type, replyTo: data.replyTo, }) } } async editMessage(messageId: string, content: string): Promise { return await apiService.put(`/api/messages/${messageId}`, { content }) } async deleteMessage(messageId: string): Promise { await apiService.delete(`/api/messages/${messageId}`) } async addReaction(messageId: string, emoji: string): Promise { await apiService.post(`/api/messages/${messageId}/reactions`, { emoji }) } async removeReaction(messageId: string, emoji: string): Promise { await apiService.delete(`/api/messages/${messageId}/reactions/${emoji}`) } // Group management async addMember(chatId: string, userId: string): Promise { await apiService.post(`/api/chats/${chatId}/members`, { userId }) } async removeMember(chatId: string, userId: string): Promise { await apiService.delete(`/api/chats/${chatId}/members/${userId}`) } async updateMemberRole(chatId: string, userId: string, role: string): Promise { await apiService.put(`/api/chats/${chatId}/members/${userId}`, { role }) } async getChatMembers(chatId: string): Promise { return await apiService.get(`/api/chats/${chatId}/members`) } // File upload async uploadFiles(files: File[]): Promise { const uploadPromises = files.map(file => this.uploadFile(file)) return await Promise.all(uploadPromises) } async uploadFile(file: File): Promise { return await apiService.upload('/api/upload', file) } // Search async searchMessages(query: string, chatId?: string): Promise { const params = new URLSearchParams({ q: query }) if (chatId) { params.append('chatId', chatId) } return await apiService.get(`/api/search/messages?${params}`) } async searchChats(query: string): Promise { return await apiService.get(`/api/search/chats?q=${encodeURIComponent(query)}`) } // Chat settings async updateChatSettings(chatId: string, settings: any): Promise { await apiService.put(`/api/chats/${chatId}/settings`, settings) } async getChatSettings(chatId: string): Promise { return await apiService.get(`/api/chats/${chatId}/settings`) } // Notifications async markAsRead(chatId: string): Promise { await apiService.post(`/api/chats/${chatId}/read`) } async muteChat(chatId: string, duration?: number): Promise { await apiService.post(`/api/chats/${chatId}/mute`, { duration }) } async unmuteChat(chatId: string): Promise { await apiService.post(`/api/chats/${chatId}/unmute`) } } export const chatService = new ChatService()