import Route from "../route.js";
import initDb from "../db.js";
import { randomUUID } from 'node:crypto';
import auth from "../form/auth.js";

let db = await initDb();

// TODO: rewrite
let main = new Route([auth], async function (req, res, input) {
    let { username } = input;
    let id = randomUUID();

    let { targetType, targetId, content } = req.body;
    if (!targetType || !targetId || !content || username == '!nobody')
        return { 'success': false, 'message': 'Some fields are missing' }; // probably should not re-use these strings

    await db.run('INSERT INTO comment (username, targetType, targetId, date, content, id) VALUES (?,?,?,?,?,?)', [
        username,
        targetType,
        targetId,
        +new Date(),
        content,
        id
    ]);

    res.send({ redir: `?` });
});

export default main;