import {cookieService} from '../services/cookieService.js';

function login(req, res) {
    res.render('login')
}

function register(req, res) {
    res.render('register');
}

async function dashboard(req, res) {
    const rooms = await cookieService.getRooms(req.user);
    let data = {
        user: req.user,
        rooms: rooms
    };
    res.render('dashboard', data);
}

async function room(req, res) {
    const room = await cookieService.getRoomById(req.query.id);
    let data = {
        user: req.user,
        room: room[0]
    };
    res.render('room', data);
}

async function newRoom(req, res) {
    if (req.body.roomName) {
        const roomId = await cookieService.createRoom(req.body.roomName);
        await cookieService.joinRoom(req.user.id, roomId);
        res.redirect('/dashboard');
    }
    else {
        res.render('new-room', {user: req.user});
    }
}

async function joinRoom(req, res) {
    if (req.body.roomId) {
        await cookieService.joinRoom(req.user.id, req.body.roomId);
    }
    res.redirect('/dashboard');
}

async function updateRoom(req, res) {

}

export default {login, register, dashboard, newRoom, joinRoom, room, updateRoom}