const Room = require('../models/room');
const { sendToPlayersData } = require('../socket/emits');

const getRoom = async roomId => {
    return await Room.findOne({ _id: roomId }).exec();
};

const getRooms = async () => {
    return await Room.find().exec();
};

const updateRoom = async room => {
    return await Room.findOneAndUpdate({ _id: room._id }, room).exec();
};

const getJoinableRoom = async () => {
    return await Room.findOne({ full: false, started: false }).exec();
};

const createNewRoom = async data => {
    const room = new Room(data);
    await room.save();
    return room;
};

const deleteRoom = async roomId => {
    return await Room.findByIdAndDelete(roomId).exec();
};

Room.watch().on('change', async data => {
    // Ignore delete operations
    if (data.operationType === 'delete') {
        console.log('🗑️ Room deleted, skipping sendToPlayersData');
        return;
    }
    
    const room = await getRoom(data.documentKey._id);
    if (room) {
        sendToPlayersData(room);
    }
});

module.exports = { getRoom, getRooms, updateRoom, getJoinableRoom, createNewRoom, deleteRoom };
