'use strict'

const router = require('express').Router()
const {BadRequestError} = require('jsonapi-errors/lib/errors')

const { filter } = require('lodash')
const pluralize = require('pluralize')
const Serializer = require('jsonapi-serializer').Serializer
const foreignKey = require('../sql-builder/foreign-key')

const {debugApi} = require('../utils/debug')
const userGroup = require('../models/user-group')

const ApiWrapper = require('./api-wrapper')

class CustomApiWrapper extends ApiWrapper {
  fetchHasMany (relationModelName) {
    const pluralRelationName = pluralize(relationModelName)
    const fkName = foreignKey(this.name)
    const relationModel = this.registry.model(relationModelName)

    const userOptions = (relationModelName === 'user') ? { where: {hide: false}, orderBy: 'name' } : {}

    return Promise.all([ this.model.selectMany(), relationModel.selectMany(userOptions) ])
    .then(([modelRows, relationRows]) => {
      const dataSet = modelRows.map((row) => {
        row[pluralRelationName] = filter(relationRows, [fkName, row.id])
        return row
      })

      const options = { attributes: this.model.sqlBuilder.columnsNames.concat(pluralRelationName) }
      options[pluralRelationName] = { ref: 'id', attributes: relationModel.sqlBuilder.columnsNames }
      const serializer = new Serializer(this.name, options)

      return serializer.serialize(dataSet)
    })
  }
}

const apiWrappedUserGroup = new CustomApiWrapper(userGroup)

function readAll (req, res, next) {
  debugApi('user-group#readAll')

  apiWrappedUserGroup.fetchHasMany('user')
  .then((json) => res.send(json))
  .catch((err) => next(err))
}

function params (req, res, next, id) {
  const idInt = parseInt(id, 10)
  debugApi(`user-group#params id: ${id}`)

  if (isNaN(idInt)) return next(new BadRequestError(`param ${id}`))

  req.id = idInt
  next()
}

function read (req, res, next) {
  debugApi('user-group#read')

  apiWrappedUserGroup.apiFind(req.id)
  .then((data) => res.json(data))
  .catch((err) => next(err))
}

router.param('id', params)

router.route('/')
  .get(readAll)

router.route('/:id')
  .get(read)

module.exports = router

@@ -697,16 +697,77 @@
 lName)%0A%0A
+    const groupOptions = %7B%0A      where: %7Bhide: false%7D%0A    %7D%0A%0A
     cons
@@ -801,17 +801,17 @@
 delName 
-=
+!
 == 'user
@@ -816,10 +816,23 @@
 er')
- ?
+%0A    ? %7B%7D%0A    :
  %7B w
@@ -871,14 +871,231 @@
 ame'
- %7D : %7B
+,%0A      whereIn: %7B%0A        relationFkName: fkName,%0A        parentIdFieldName: this.model.sqlBuilder.idFieldName,%0A        parentTableName: this.model.sqlBuilder.tableName,%0A        parentWhere: groupOptions.where%0A      %7D%0A    
 %7D%0A%0A 
@@ -1140,16 +1140,28 @@
 ectMany(
+groupOptions
 ), relat

rewrite fetchHasMany hack with whereIn option