var policy = require('s3-policy');
var Route = require('project-router').Route;

var S3_BUCKET_NAME = process.env.S3_BUCKET_NAME;
var S3_KEY = process.env.S3_KEY;
var S3_SECRET = process.env.S3_SECRET;
var PUBLIC_READ_ACL = 'public-read';

module.exports = Route.extend({

  model: function () {
    return policy({
      secret: S3_SECRET,
      bucket: S3_BUCKET_NAME,
      key: S3_KEY,
      expires: new Date(Date.now() + 60000),
      acl: PUBLIC_READ_ACL,
      conditions: [{success_action_status: '200'}]
    });
  },

  responseData: function (model) {
    var policy = {
      AWSAccessKeyId: S3_KEY,
      acl: PUBLIC_READ_ACL,
      policy: model.policy,
      signature: model.signature,
      success_action_status: 200,
      'Content-Length': ''
    };
    return {
      policy: policy,
      s3BucketName: S3_BUCKET_NAME
    };
  }

});