package com.coding4people.mosquitoreport.api.controllers;

import javax.inject.Inject;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.ws.rs.Consumes;
import javax.ws.rs.ForbiddenException;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import com.coding4people.mosquitoreport.api.auth.AuthenticationService;
import com.coding4people.mosquitoreport.api.models.Email;
import com.coding4people.mosquitoreport.api.repositories.EmailRepository;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

@Path("/auth/email")
public class AuthEmailController {
    @Inject
    EmailRepository emailRepository;
    
    @Inject
    AuthenticationService authenticationService;

    /**
     * @api {post} /auth/email Authenticate an user using his email
     * @apiName GetUser
     * @apiGroup User
     * 
     * @apiParam {String} email
     * @apiParam {String} password
     * 
     * @apiSuccessExample Success-Response:
     *     HTTP/1.1 200 OK
     *     {
     *       "token": "..."
     *     }
     */
    @POST
    @Consumes("application/json")
    @Produces("application/json;charset=UTF-8")
    public ObjectNode auth(@Valid AuthInput input) {
        Email email = emailRepository.load(input.getEmail());
        
        if (email == null || !Email.checkPassword(input.getPassword(), email.getPassword())) {
            throw new ForbiddenException("Invalid email or password");
        }
        
        return new ObjectMapper().createObjectNode().put("token", authenticationService.generateToken(email.getUserguid()));
    }

    public static class AuthInput {
        @NotNull
        private String email;
        
        @NotNull
        private String password;

        public String getEmail() {
            return email;
        }

        public void setEmail(String email) {
            this.email = email;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }
    }
}

@@ -895,33 +895,8 @@
 il%0D%0A
-     * @apiName GetUser%0D%0A
     
@@ -992,16 +992,174 @@
 ssword%0D%0A
+     *%0D%0A     * @apiParamExample %7Bjson%7D Request-Example:%0D%0A     *     %7B%0D%0A     *      %22email%22: %22test@test.org%22,%0D%0A     *      %22password%22: %22123456%22%0D%0A     *     %7D%0D%0A
      * %0D
@@ -1197,16 +1197,15 @@
 ess-
-Response
+Headers
 :%0D%0A 
@@ -1231,16 +1231,69 @@
 200 OK%0D%0A
+     *%0D%0A     * @apiSuccessExample Success-Response:%0D%0A
      *  

Add example docs into AuthEmailController