var swagger = require("swagger-node-express");
var url = require("url");
var errors = swagger.errors;
var params = swagger.params;

/* add model includes */

function writeResponse (response, data) {
  response.header('Access-Control-Allow-Origin', "*");
  response.header("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT");
  response.header("Access-Control-Allow-Headers", "Content-Type");
  response.header("Content-Type", "application/json; charset=utf-8");
  response.send(JSON.stringify(data));
}

exports.models = models = require("../models.js");

{{#operations}}
{{#operation}}
exports.{{nickname}} = {
  'spec': {
    "description" : "Operations about pets",
    "path" : "{{path}}",
    "notes" : "{{{notes}}}",
    "summary" : "{{{summary}}}",
    "method": "{{httpMethod}}",
    "params" : [{{#queryParams}}
      params.query("{{paramName}}", "{{description}}", "{{swaggerDataType}}", {{#required}}true{{/required}}{{^required}}false{{/required}}, {{#allowMultiple}}true{{/allowMultiple}}{{^allowMultiple}}false{{/allowMultiple}}, "{{{allowableValues}}}"{{#defaultValue}}, {{{defaultValue}}}{{/defaultValue}}){{#hasMore}},{{/hasMore}}
    {{/queryParams}}].concat([{{#pathParams}}
      params.path("{{paramName}}", "{{description}}"){{#hasMore}},{{/hasMore}}
    {{/pathParams}}]).concat([{{#headerParams}}
      params.header("{{paramName}}", "{{description}}"){{#hasMore}},{{/hasMore}}
    {{/headerParams}}]).concat([{{#bodyParams}}
      params.body("body", "{{swaggerDataType}}", "{{description}}", {{#required}}{{required}}{{/required}}{{^required}}false{{/required}})
    {{/bodyParams}}]),
    {{#returnContainer}}
    "type": "{{returnType}}",
    "items": {
      {{#returnTypeIsPrimitive}}"type": "{{returnContainer}}"{{/returnTypeIsPrimitive}}
      {{^returnTypeIsPrimitive}}"$ref": "{{returnContainer}}"{{/returnTypeIsPrimitive}}
    },
    // container
    {{/returnContainer}}
    {{^returnContainer}}
    "type" : "{{returnType}}",
    {{/returnContainer}}
    "responseMessages" : [errors.invalid('id'), errors.notFound('{{returnType}}')],
    "nickname" : "{{nickname}}"
  },
  'action': function (req,res) {
    {{#requiredParamCount}}
    {{#requiredParams}}
    if (!req.params.{{baseName}}) {
      throw errors.invalid('{{baseName}}');
    }
    {{/requiredParams}}
    {{/requiredParamCount}}
    writeResponse(res, {message: "how about implementing {{nickname}} as a {{httpMethod}} method?"});    
  }
};
{{/operation}}
{{/operations}}