const express = require('express');

const schoolApp = express.Router();

const libs = `${process.cwd()}/libs`;

const squel = require('squel');

const query = require(`${libs}/middlewares/query`);

const response = require(`${libs}/middlewares/response`);

const ReqQueryFields = require(`${libs}/middlewares/reqQueryFields`);

let rqf = new ReqQueryFields();
let rqfCount = new ReqQueryFields();

rqf.addField({
    name: 'filter',
    field: false,
    where: true
}).addValue({
    name: 'id',
    table: 'escola',
    tableField: 'id',
    where: {
        relation: '=',
        type: 'integer',
        field: 'id'
    }
}).addValue({
    name: 'city',
    table: 'municipio',
    tableField: 'nome',
    resultField: 'city_name',
    where: {
        relation: '=',
        type: 'integer',
        field: 'municipio_id',
        table: 'escola'
    },
    join: {
        primary: 'id',
        foreign: 'municipio_id',
        foreignTable: 'escola'
    }
}).addValue({
    name: 'state',
    table: 'estado',
    tableField: 'nome',
    resultField: 'state_name',
    where: {
        relation: '=',
        type: 'integer',
        field: 'estado_id',
        table: 'escola'
    },
    join: {
        primary: 'id',
        foreign: 'estado_id',
        foreignTable: 'escola'
    }
}).addValue({
    name: 'year',
    table: 'escola',
    tableField: 'ano_censo',
    resultField: 'year',
    where: {
        relation: '=',
        type: 'integer',
        field: 'ano_censo',
        table: 'escola'
    }
});

rqfCount.addField({
    name: 'filter',
    field: false,
    where: true
}).addField({
    name: 'dims',
    field: true,
    where: false
}).addValue({
    name: 'id',
    table: 'escola',
    tableField: 'id',
    where: {
        relation: '=',
        type: 'integer',
        field: 'id'
    }
}).addValue({
    name: 'city',
    table: 'municipio',
    tableField: 'nome',
    resultField: 'city_name',
    where: {
        relation: '=',
        type: 'integer',
        field: 'municipio_id',
        table: 'escola'
    },
    join: {
        primary: 'id',
        foreign: 'municipio_id',
        foreignTable: 'escola'
    }
}).addValue({
    name: 'state',
    table: 'estado',
    tableField: 'nome',
    resultField: 'state_name',
    where: {
        relation: '=',
        type: 'integer',
        field: 'estado_id',
        table: 'escola'
    },
    join: {
        primary: 'id',
        foreign: 'estado_id',
        foreignTable: 'escola'
    }
}).addValue({
    name: 'region',
    table: 'regiao',
    tableField: 'nome',
    resultField: 'region_name',
    where: {
        relation: '=',
        type: 'integer',
        field: 'id'
    },
    join: {
        primary: 'id',
        foreign: 'regiao_id',
        foreignTable: 'escola'
    }
}).addValue({
    name: 'year',
    table: 'escola',
    tableField: 'ano_censo',
    resultField: 'year',
    where: {
        relation: '=',
        type: 'integer',
        field: 'ano_censo',
        table: 'escola'
    }
}).addValue({
    name: 'adm_dependency',
    table: 'dependencia_adm',
    tableField: 'nome',
    resultField: 'adm_dependency_name',
    where: {
        relation: '=',
        type: 'integer',
        field: 'id'
    },
    join: {
        primary: 'id',
        foreign: 'dependencia_adm_id',
        foreignTable: 'escola'
    }
}).addValue({
    name: 'adm_dependency_detailed',
    table: 'dependencia_adm',
    tableField: 'nome',
    resultField: 'adm_dependency_detailed_name',
    where: {
        relation: '=',
        type: 'integer',
        field: 'id'
    },
    join: {
        primary: 'id',
        foreign: 'dependencia_adm_priv',
        foreignTable: 'escola'
    }
}).addValue({
    name: 'location',
    table: 'escola',
    tableField: 'cod_localizacao',
    resultField: 'cod_localizacao',
    where: {
        relation: '=',
        type: 'integer',
        field: 'cod_localizacao'
    }
}).addValue({
    name: 'cook_room',
    table: 'escola',
    tableField: 'cozinha',
    resultField: 'cook_room_name',
    where: {
        relation: '=',
        type: 'boolean',
        field: 'cozinha'
    }
// tem problemas
}).addValue({
    name: 'government_agreement',
    table: 'escola',
    tableField: 'conveniada_pp',
    resultField: 'conveniada_poder_publico',
    where: {
        relation: '=',
        type: 'boolean',
        field: 'conveniada_pp'
    }
});
schoolApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => {
    console.log(req.filter);
    if(typeof req.filter === 'undefined' || Object.keys(req.filter).length === 0) {
        res.status(400);
        next({
            status: 400,
            message: 'Wrong/No filter specified'
        });
    }
    req.sql.from('escola')
        .field('escola.id')
        .field('escola.ano_censo', 'year')
        .field('escola.nome_escola', 'name')
        .field('escola.estado_id', 'state_id')
        .field('escola.municipio_id', 'city_id');
    next();
}, query, response('school'));

schoolApp.get('/count', rqfCount.parse(), rqfCount.build(), (req, res, next) => {

    req.sql.from('escola')
        .field('COUNT(escola.id)', 'total')
        .field("'Brasil'", 'name')
        .field('escola.ano_censo', 'year')
        .group('escola.ano_censo')
        .order('escola.ano_censo')
        .where('escola.situacao_de_funcionamento = 1 AND escola.ensino_regular = 1');
    next();
}, query, response('school'));

module.exports = schoolApp;