import {Router} from 'express';
import createCRUD from './utils/createCRUD';
import {createMap} from './utils/createMap';
import {getSpecification, getSchemaDefinition} from '../../swaggerFromSpec';
const swagger = getSpecification();

const groupMap = {
  body: 'contents',
  profile_id: 'owner_id',
  media: 'attributes.media'
};

const postMap = {
  body: 'contents',
  profile_id: 'owner_id',
  group_id: 'entity_ref',
  media: 'attributes.media'
};

const commentMap = {
  body: 'contents',
  profile_id: 'owner_id',
  post_id: 'entity_ref',
  media: 'attributes.media'
};

const reviewMap = {
  body: 'contents',
  profile_id: 'owner_id',
  reference: 'entity_ref',
  rating: 'attributes.rating',
  media: 'attributes.media'
};

function getSchema(type, map) {
  return createMap(getSchemaDefinition(swagger, type), map);
}

export const schemas = {
  group: getSchema('Group', groupMap),
  post: getSchema('Post', postMap),
  comment: getSchema('Comment', commentMap),
  review: getSchema('Review', reviewMap)
};

/**
 * Returns group router.
 *
 * @returns {Object}
 */
export function group() {
  const router = createCRUD('entity', 'group', Router(), groupMap, getSchemaDefinition(swagger, 'Group'));

  router.get('/:groupId/posts', (req, res) => {
    const provider = req.app.get('serviceProvider');
    const context = req.context;
    context.crud = true;

    const query = Object.assign(
      {},
      req.params,
      req.body,
      req.query,
      {selector: {
        type: 'post',
        entity_ref: req.params.groupId
      }},
      {
        _meta: {
          type: 'post',
          elvisType: 'entity',
          schemaMap: createMap(getSchemaDefinition(swagger, 'Post'), postMap),
          schema: getSchemaDefinition(swagger, 'Post')
        }
      }
    );

    provider.execute('listEntities', query, context).then(result => res.json(result));

  });

  return router;
}

/**
 * Returns post router.
 *
 * @returns {Object}
 */
export function post() {
  return createCRUD('entity', 'post', Router(), postMap, getSchemaDefinition(swagger, 'Post'));
}

/**
 * Returns comment router.
 *
 * @returns {Object}
 */
export function comment() {
  return createCRUD('entity', 'comment', Router(), commentMap, getSchemaDefinition(swagger, 'Comment'));
}

/**
 * Returns comment router.
 *
 * @returns {Object}
 */
export function review() {
  return createCRUD('entity', 'review', Router(), reviewMap, getSchemaDefinition(swagger, 'Review'));
}

@@ -81,19 +81,22 @@
 ort 
-%7BcreateMap%7D
+getRelatedList
  fro
@@ -110,148 +110,86 @@
 ils/
-createMap';%0Aimport %7BgetSpecification, getSchemaDefinition%7D from '../../swaggerFromSpec';%0Aconst swagger = getSpecification();%0A%0Acons
+getRelatedList';%0Aimport %7BcreateMap%7D from './utils/createMap';%0Aimpor
 t 
+%7B
 groupMap
  = %7B
@@ -188,490 +188,197 @@
 pMap
- = %7B%0A  body: 'contents',%0A  profile_id: 'owner_id',%0A  media: 'attributes.media'%0A%7D;%0A%0Aconst postMap = %7B%0A  body: 'contents',%0A  profile_id: 'owner_id',%0A  group_id: 'entity_ref',%0A  media: 'attributes.media'%0A%7D;%0A%0Aconst commentMap = %7B%0A  body: 'contents',%0A  profile_id: 'owner_id',%0A  post_id: 'entity_ref',%0A  media: 'attributes.media'%0A%7D;%0A%0Aconst reviewMap = %7B%0A  body: 'contents',%0A  profile_id: 'owner_id',%0A  reference: 'entity_ref',%0A  rating: 'attributes.rating',%0A  media: 'attributes.media'%0A%7D
+, postMap, commentMap, reviewMap, likeMap, followMap, flagMap%7D from './maps';%0Aimport %7BgetSpecification, getSchemaDefinition%7D from '../../swaggerFromSpec';%0Aconst swagger = getSpecification()
 ;%0A%0Af
@@ -745,16 +745,122 @@
 oup() %7B%0A
+  const likeMap = %7B%0A    id: 'id',%0A    reference: 'attributes.reference',%0A    profile_id: 'owner_id'%0A  %7D;%0A%0A
   const 
@@ -975,22 +975,17 @@
 .get('/:
-groupI
+i
 d/posts'
@@ -990,496 +990,303 @@
 s', 
-(req, res) =%3E %7B%0A    const provider = req.app.get('serviceProvider');%0A    const context = req.context;%0A    context.crud = true;%0A%0A    const query = Object.assign(%0A      %7B%7D,%0A      req.params,%0A      req.body,%0A      req.query,%0A      %7Bselector: %7B%0A        type: 'post',%0A        entity_ref: req.params.groupId%0A      %7D%7D,%0A      %7B%0A        _meta: %7B%0A          type: 'post',%0A          elvisType: 'entity',%0A          schemaMap: createMap(getSchemaDefinition(swagger, 'Post'), postMap),%0A          schema:
+getRelatedList('entity', 'entity', 'post', postMap, getSchemaDefinition(swagger, 'Post')));%0A  router.get('/:id/likes', getRelatedList('entity', 'action', 'like', likeMap, getSchemaDefinition(swagger, 'Like')));%0A  router.get('/:id/follows', getRelatedList('entity', 'action', 'follow', followMap,
  get
@@ -1316,132 +1316,136 @@
 r, '
-Post')%0A        %7D%0A      %7D%0A    
+Follow'))
 );%0A
-%0A
   
-  provider.execute('listEntities', query, context).then(result =%3E res.json(result));%0A%0A  %7D
+router.get('/:id/flags', getRelatedList('entity', 'action', 'flag', flagMap, getSchemaDefinition(swagger, 'Flag'))
 );%0A%0A

add related endpoint on groups