/*
 * Routing for API
 * Created by: Michael Pastushkov <michael@pastushkov.com>
 */

'use strict';
const utils = require('../utils/utils');

const root_path = '/api';

const GET = 'get';
const POST = 'post';
const PATCH = 'patch';
const PUT = 'put';
const DELETE = 'delete';

function init(app) {

    // Main routing table: [ method, path, module, function = 'run' ] 
    let routing = [
        // Misc
        [POST, '/stream', './KotGPT'],
        [GET,  '/stop', './KotGPT', 'stop'],
        [GET,  '/clear', './KotGPT', 'clear'],
        // Misc
        [GET, '/info', '../common/info'],
        [GET, '/logcat', '../utils/logcat'],
    ];

    for (const route of routing) {
        let mod = require(route[2]);
        let func = route[3] || 'run';
        app[route[0]] (root_path + route[1], mod[func]);
    }

    // const dal = require('../utils/dal');
    // for (let o of Object.values(dal)) {
    //     app.get(`/db/${o.table}/:id?`, o.get);
    //     app.post(`/db/${o.table}`, o.post);
    //     app.patch(`/db/${o.table}`, o.patch);
    //     app.delete(`/db/${o.table}/:id`, o.del);
    // }

    const info = require('../common/info');
    const server = require('../../server');
 
    module.exports.root_path = root_path;
    module.exports.routing = routing;
    info.init(server, module);
}

module.exports.init = init;
