const Ajv = require('ajv');
const organizationSchema = require('./organization-schema.json');
const queryStringSchema = require('./querystring-schema.json');
const provider = require('./mongodb-provider')('organizations');
// const Logger = require('mongodb').Logger;

// Set debug level
// Logger.setLevel('debug');

let cachedDb = null;

function badRequest(context, errors) {
    return {
        statusCode: 400,
        body: JSON.stringify({
            message: 'Validation failed',
            requestId: context.awsRequestId,
            errors
        })
    };
}

function getCachedDb() {
    return provider.getDatabase(cachedDb)
    .then((db) => {
        cachedDb = db;
        return cachedDb;
    });
}

function handleUnhandledError(callback) {
    return (reason) => {
        console.error(reason);
        return callback(reason, '[500] Internal Server Error');
    };
}

function get(event, context, callback) {
    context.callbackWaitsForEmptyEventLoop = false;

    const qs = event.queryStringParameters || { };
    qs.limit = Number(qs.limit) || 0;
    qs.offset = Number(qs.offset) || 0;

    const ajv = new Ajv();
    ajv.validate(queryStringSchema, qs);
    if (ajv.errors && ajv.errors.length > 0) {
        console.log(ajv.errors);
        const errors = ajv.errors.map((error) => ({
            message: error.message,
            path: error.dataPath
        }));
        return callback(null, badRequest(context, errors));
    }

    return getCachedDb()
    .then((db) => provider.getAll(db, qs.limit, qs.offset, callback))
    .catch(handleUnhandledError(callback));
}

function getById(event, context, callback) {
    if (!event.pathParameters || !event.pathParameters.id) {
        return callback(null, badRequest(context, [{ message: 'missing parameter', path: '/id' }]));
    }
    context.callbackWaitsForEmptyEventLoop = false;

    return getCachedDb()
    .then((db) => provider.getById(db, event.pathParameters.id, callback))
    .catch(handleUnhandledError(callback));
}

function create(event, context, callback) {
    context.callbackWaitsForEmptyEventLoop = false;

    const jsonContents = JSON.parse(event.body);

    const ajv = new Ajv({ allErrors: true, removeAdditional: true });
    ajv.validate(organizationSchema, jsonContents);
    if (ajv.errors && ajv.errors.length > 0) {
        console.log(ajv.errors);
        const errors = ajv.errors.map((error) => ({
            message: error.message,
            path: error.dataPath,
            params: error.params
        }));
        return callback(null, badRequest(context, errors));
    }

    return getCachedDb()
    .then((db) => provider.create(db, jsonContents, callback))
    .catch(handleUnhandledError(callback));
}

module.exports = {
    get,
    getById,
    create
};

@@ -1045,16 +1045,47 @@
 .limit =
+ typeof qs.limit === 'number' ?
  Number(
@@ -1094,20 +1094,27 @@
 .limit) 
-%7C%7C 0
+: undefined
 ;%0A    qs
@@ -1127,30 +1127,95 @@
 t = 
-Number(qs.offset) %7C%7C 0
+typeof qs.limit === 'number' ? Number(qs.limit) : undefined;%0A%0A    console.log(qs.limit)
 ;%0A%0A 
@@ -1237,16 +1237,37 @@
 new Ajv(
+%7B useDefaults: true %7D
 );%0A    a

use default value for limit from schema