import chatService from '../services/chatService.js'; class ChatController { constructor() { this.getMessages = this.getMessages.bind(this); this.findRandomChatMatch = this.findRandomChatMatch.bind(this); this.registerUser = this.registerUser.bind(this); this.sendMessage = this.sendMessage.bind(this); this.stopChat = this.stopChat.bind(this); this.removeUser = this.removeUser.bind(this); this.initOneToOne = this.initOneToOne.bind(this); this.sendOneToOneMessage = this.sendOneToOneMessage.bind(this); this.getOneToOneMessageHistory = this.getOneToOneMessageHistory.bind(this); } async getMessages(req, res) { const { to, from } = req.body; try { const messages = await chatService.getMessages(to, from); res.status(200).json(messages); } catch (error) { res.status(500).json({ error: error.message }); } } async findRandomChatMatch(req, res) { const { genders, age, id } = req.body; try { const match = await chatService.findMatch(genders, age, id); if (match) { res.status(200).json({ status: 'matched', user: match }); } else { res.status(200).json({ status: 'waiting' }); } } catch (error) { res.status(500).json({ error: error.message }); } } async registerUser(req, res) { const { gender, age } = req.body; try { const userId = await chatService.registerUser(gender, age); res.status(200).json({ id: userId }); } catch (error) { res.status(500).json({ error: error.message }); } } async sendMessage(req, res) { const { from, to, text } = req.body; try { const message = await chatService.addMessage(from, to, text); res.status(200).json(message); } catch (error) { res.status(500).json({ error: error.message }); } } async removeUser(req, res) { const { id } = req.body; try { await chatService.removeUser(id); res.sendStatus(200); } catch (error) { res.status(500).json({ error: error.message }); } } async stopChat(req, res) { const { id } = req.body; try { await chatService.endChat(id); res.sendStatus(200); } catch (error) { res.status(500).json({ error: error.message }); } } async initOneToOne(req, res) { const { userid: hashedUserId } = req.headers; const { partnerHashId } = req.body; try { await chatService.initOneToOne(hashedUserId, partnerHashId); res.status(200).json({ message: 'One-to-one chat initialization is pending implementation.' }); } catch (error) { res.status(500).json({ error: error.message }); } } async sendOneToOneMessage(req, res) { const { user1HashId, user2HashId, message } = req.body; try { await chatService.sendOneToOneMessage(user1HashId, user2HashId, message); res.status(200).json({ status: 'message sent' }); } catch (error) { res.status(500).json({ error: error.message }); } } async getOneToOneMessageHistory(req, res) { const { user1HashId, user2HashId } = req.query; try { const history = await chatService.getOneToOneMessageHistory(user1HashId, user2HashId); res.status(200).json({ history }); } catch (error) { res.status(500).json({ error: error.message }); } } } export default ChatController;