/*!
 * Copyright 2014 Apereo Foundation (AF) Licensed under the
 * Educational Community License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may
 * obtain a copy of the License at
 *
 *     http://opensource.org/licenses/ECL-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

var OAE = require('oae-util/lib/oae');
var OaeUtil = require('oae-util/lib/util');

var FollowingAPI = require('oae-following');

/**
 * @REST getFollowingUserIdFollowers
 *
 * Get the users who are following a user
 *
 * @Server      tenant
 * @Method      GET
 * @Path        /following/{userId}/followers
 * @PathParam   {string}                userId              The id of the user whose followers to get
 * @QueryParam  {number}                [limit]             The maximum number of results to return. Default: 10
 * @QueryParam  {string}                [start]             The following paging token from which to start fetching followers
 * @Return      {FollowResults}                             The followers of the specified user
 */
OAE.tenantRouter.on('get', '/api/following/:userId/followers', function(req, res) {
    var limit = OaeUtil.getNumberParam(req.query.limit, 10, 1, 25);
    FollowingAPI.getFollowers(req.ctx, req.params.userId, req.query.start, limit, function(err, followers, nextToken) {
        if (err) {
            return res.send(err.code, err.msg);
        }

        return res.send(200, {'results': followers, 'nextToken': nextToken});
    });
});

/**
 * @REST getFollowingUserIdFollowing
 *
 * Get the users who are followed by a specific user
 *
 * @Server      tenant
 * @Method      GET
 * @Path        /following/{userId}/following
 * @PathParam   {string}                userId              The id of the user for who to get the followed users
 * @QueryParam  {number}                [limit]             The maximum number of results to return. Default: 10
 * @QueryParam  {string}                [start]             The following paging token from which to start fetching followed users
 * @Return      {FollowResults}                             The users followed by the specified user
 */
OAE.tenantRouter.on('get', '/api/following/:userId/following', function(req, res) {
    var limit = OaeUtil.getNumberParam(req.query.limit, 10, 1, 25);
    FollowingAPI.getFollowing(req.ctx, req.params.userId, req.query.start, limit, function(err, following, nextToken) {
        if (err) {
            return res.send(err.code, err.msg);
        }

        return res.send(200, {'results': following, 'nextToken': nextToken});
    });
});

/**
 * @REST postFollowingUserIdFollow
 *
 * Follow a user
 *
 * @Server      tenant
 * @Method      POST
 * @Path        /following/{userId}/follow
 * @PathParam   {string}                userId              The id of the user to follow
 * @Return      {void}
 */
OAE.tenantRouter.on('post', '/api/following/:userId/follow', function(req, res) {
    FollowingAPI.follow(req.ctx, req.params.userId, function(err, followers) {
        if (err) {
            return res.send(err.code, err.msg);
        }

        return res.send(200);
    });
});

/**
 * @REST postFollowingUserIdUnfollow
 *
 * Unfollow a user
 *
 * @Server      tenant
 * @Method      POST
 * @Path        /following/{userId}/unfollow
 * @PathParam   {string}                userId              The id of the user to unfollow
 * @Return      {void}
 */
OAE.tenantRouter.on('post', '/api/following/:userId/unfollow', function(req, res) {
    FollowingAPI.unfollow(req.ctx, req.params.userId, function(err) {
        if (err) {
            return res.send(err.code, err.msg);
        }

        return res.send(200);
    });
});

@@ -1346,36 +1346,331 @@
  specified user%0A
+ * @HttpResponse                        200                 (Followers available)%0A * @HttpResponse                        400                 You must specify a valid user id%0A * @HttpResponse                        401                 You are not authorized to see this user's list of followers%0A
  */%0A
-
 OAE.tenantRouter
@@ -2741,16 +2741,314 @@
 ed user%0A
+ * @HttpResponse                        200                 (Users available)%0A * @HttpResponse                        400                 You must specify a valid user id'%0A * @HttpResponse                        401                 You are not authorized to view this user's list of followed users%0A
  */%0AOAE.
@@ -3737,32 +3737,432 @@
 urn      %7Bvoid%7D%0A
+ * @HttpResponse                        200                 (Now following user)%0A * @HttpResponse                        400                 You must specify a valid user id of a user to follow%0A * @HttpResponse                        401                 You are not authorized to follow this user%0A * @HttpResponse                        401                 You must be authenticated to follow a user%0A
  */%0AOAE.tenantRo
@@ -4673,24 +4673,24 @@
 to unfollow%0A
-
  * @Return  
@@ -4700,16 +4700,323 @@
  %7Bvoid%7D%0A
+ * @HttpResponse                        200                 (No longer following user)%0A * @HttpResponse                        400                 You must specify a valid user id of a user to unfollow%0A * @HttpResponse                        401                 You must be authenticated to unfollow a user%0A
  */%0AOAE.

document REST responses for following module