/*
Copyright (C) 2016 Centro de Computacao Cientifica e Software Livre
Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR

This file is part of simcaq-node.

simcaq-node is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

simcaq-node is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with simcaq-node.  If not, see <https://www.gnu.org/licenses/>.
*/

const express = require('express');

const simcaqDivisionOfResponsibilityApp = express.Router();

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

const squel = require('squel');

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

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

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

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

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

const cache = require('apicache').options({ debug: config.debug, statusCodes: {include: [200]} }).middleware;

let rqf = new ReqQueryFields();

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

simcaqDivisionOfResponsibilityApp.use(cache('15 day'));

rqf.addField({
    name: 'filter',
    field: false,
    where: true
}).addField({
    name: 'dims',
    field: true,
    where: false
}).addValue({
    name: 'year',
    table: 'simcaq_divisao_de_responsabilidade',
    tableField: 'ano_censo',
    where: {
        relation: '=',
        type: 'integer',
        field: 'ano_censo'
    }
}).addValue({
    name: 'city',
    table: 'municipio',
    tableField: ['nome', 'id'],
    resultField: ['city_name', 'city_id'],
    where: {
        relation: '=',
        type: 'integer',
        field: 'id'
    },
    join: {
        primary: 'id',
        foreign: 'municipio_id',
        foreignTable: 'simcaq_divisao_de_responsabilidade'
    }
}, 'dims').addValue({
    name: 'state',
    table: 'estado',
    tableField: ['nome', 'id'],
    resultField: ['state_name', 'state_id'],
    where: {
        relation: '=',
        type: 'integer',
        field: 'id'
    },
    join: {
        primary: 'id',
        foreign: 'estado_id',
        foreignTable: 'simcaq_divisao_de_responsabilidade'
    }
}, 'dims').addValue({
    name: 'school',
    table: 'escola_agregada',
    tableField: 'id',
    where: {
        relation: '=',
        type: 'integer',
        field: 'id'
    },
    join: {
        primary: 'id',
        foreign: 'escola_id',
        foreignTable: 'simcaq_divisao_de_responsabilidade'
    }
}, 'dims').addValue({
    name: 'education_level_short_id',
    table: 'simcaq_divisao_de_responsabilidade',
    tableField: 'etapa',
    where: {
        relation: '=',
        type: 'integer',
        field: 'etapa'
    }
}).addValue({
    name: 'adm_dependency_public_id',
    table: 'simcaq_divisao_de_responsabilidade',
    tableField: 'rede',
    where: {
        relation: '=',
        type: 'integer',
        field: 'rede'
    }
});

simcaqDivisionOfResponsibilityApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => {
    req.sql.from('simcaq_divisao_de_responsabilidade')
        .field('simcaq_divisao_de_responsabilidade.ano_censo', 'year')
        .field('SUM(simcaq_divisao_de_responsabilidade.total_matriculas)', 'enrollments')
        .field('simcaq_divisao_de_responsabilidade.etapa', 'education_level_short_id')
        .field('simcaq_divisao_de_responsabilidade.rede', 'adm_dependency_public_id')
        .group('simcaq_divisao_de_responsabilidade.ano_censo')
        .group('simcaq_divisao_de_responsabilidade.etapa')
        .group('simcaq_divisao_de_responsabilidade.rede');
    next();
}, query, id2str.transform(), response('divisionOfResponsibility'));

module.exports = simcaqDivisionOfResponsibilityApp;