const express = require('express');

const classApp = express.Router();

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

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

const squel = require('squel');

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

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

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

let rqfCount = new ReqQueryFields();
// Complete range of the enrollments dataset.
// Returns a tuple of start and ending years of the complete enrollments dataset.
classApp.get('/year_range', (req, res, next) => {
    req.sql.from('turma')
    .field('MIN(turma.ano_censo)', 'start_year')
    .field('MAX(turma.ano_censo)', 'end_year');
    next();
}, query, response('range'));

classApp.get('/location', (req, res, next) => {
    req.sql = squel.select()
    .field('id')
    .field('descricao', 'name')
    .from('localizacao');
    next();
}, query, response('location'));

// Returns all adm dependencies
classApp.get('/adm_dependency', (req, res, next) => {
    req.sql.from('dependencia_adm')
    .field('id')
    .field('nome', 'name')
    .where('id <= 4');
    next();
}, query, response('adm_dependency'));

classApp.get('/adm_dependency_detailed', (req, res, next) => {
    req.sql.from('dependencia_adm')
    .field('id', 'id')
    .field('nome', 'name');
    next();
}, query, response('adm_dependency_detailed'));

// Returns all periods avaible
classApp.get('/period', (req, res, next) => {
    req.sql.from('turma_turno')
    .field('id')
    .field('nome', 'name');
    next();
}, query, response('period'));

// Returns all educational levels avaible
classApp.get('/education_level', (req, res, next) => {
    req.sql.from('etapas_mod_ensino_segmento')
    .field('id')
    .field('nome', 'name');
    next();
}, query, response('education_level'));

rqfCount.addField({
    name: 'filter',
    field: false,
    where: true
}).addField({
    name: 'dims',
    field: true,
    where: false
}).addValue({
    name: 'city',
    table: 'municipio',
    tableField: 'nome',
    resultField: 'city_name',
    where: {
        relation: '=',
        type: 'integer',
        field: 'municipio_id',
        table: 'turma'
    },
    join: {
        primary: 'id',
        foreign: 'municipio_id',
        foreignTable: 'turma'
    }
}).addValue({
    name: 'state',
    table: 'estado',
    tableField: 'nome',
    resultField: 'state_name',
    where: {
        relation: '=',
        type: 'integer',
        field: 'estado_id',
        table: 'turma'
    },
    join: {
        primary: 'id',
        foreign: 'estado_id',
        foreignTable: 'turma'
    }
}).addValue({
    name: 'region',
    table: 'regiao',
    tableField: 'nome',
    resultField: 'region_name',
    where: {
        relation: '=',
        type: 'integer',
        field: 'id'
    },
    join: {
        primary: 'id',
        foreign: 'regiao_id',
        foreignTable: 'turma'
    }
}).addValue({
    name: 'min_year',
    table: 'turma',
    tableField: 'ano_censo',
    resultField: 'year',
    where: {
        relation: '>=',
        type: 'integer',
        field: 'ano_censo'
    }
}).addValue({
    name: 'max_year',
    table: 'turma',
    tableField: 'ano_censo',
    resultField: 'year',
    where: {
        relation: '<=',
        type: 'integer',
        field: 'ano_censo'
    }
}).addValue({
    name:'adm_dependency',
    table: 'dependencia_adm',
    tableField: 'id',
    resultField: 'adm_dependency_name',
    where: {
        relation: '=',
        type: 'integer',
        field: 'id'
    },
    join: {
        primary: 'id',
        foreign: 'dependencia_adm_id',
        foreignTable: 'turma'
    }
}).addValue({
    name: 'location',
    table: 'localizacao',
    tableField: 'descricao',
    resultField: 'location_name',
    where: {
        relation: '=',
        type: 'integer',
        field: 'id'
    },
    join: {
        primary: 'id',
        foreign: 'localizacao_id',
        foreignTable: 'turma'
    }
}).addValue({
  name:'education_level',
  table: 'etapas_mod_ensino_segmento ',
  tableField: 'id',
  resultField: 'education_level_name',
  where: {
    relation: '=',
    type: 'integer',
    field: 'id'
  },
  join: {
    primary: 'id',
    foreign: 'etapas_mod_ensino_segmento_id',
    foreignTable: 'turma'
  }
}).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: 'turma'
    }
}).addValue({
  name:'period',
  table: 'turma_turno',
  tableField: 'id',
  resultField: 'period_name',
  where: {
      relation: '=',
      type: 'integer',
      field: 'id'
  },
  join: {
      primary: 'id',
      foreign: 'turno',
      foreignTable: 'turma'
  }
}).addValue({
    name: 'school',
    table: 'escola',
    tableField: 'nome_escola',
    resultField: 'school_name',
    where: {
        relation: '=',
        type: 'integer',
        field: 'id'
    },
    join: {
        primary: ['id', 'ano_censo'],
        foreign: ['escola_id', 'ano_censo'],
        foreignTable: 'turma'
    }
});


classApp.get('/', rqfCount.parse(), rqfCount.build(), (req, res, next) => {
  log.debug(req.sql.toParam());
   req.sql.field('COUNT(turma.id)', 'total')
   .field("'Brasil'", 'name')
   .field('turma.ano_censo', 'year')
   .from('turma')
   .group('turma.ano_censo')
   .order('turma.ano_censo')
   .where('turma.tipo_turma_id = 0 OR turma.tipo_turma_id = 1 OR turma.tipo_turma_id = 2 OR turma.tipo_turma_id = 3');
   next();
}, query, response('class'));

module.exports = classApp;