const express = require('express')
const json2csv = require('json-2-csv').json2csv
const csv2json = require('json-2-csv').csv2json
const moment = require('moment')
const config = require('lib/config')
const urlBuilder = require('lib/url-builder')
const middlewares = require('../middlewares')
const api = require('../db-api')

const app = module.exports = express()

const titles = [
  'Topic ID',
  'Topic Title',
  'Topic URL'
]

function fullUrl (topicId, forumName) {
  return config.protocol + '://' + config.host + urlBuilder
    .for('site.topic', {
      id: topicId,
      forum: forumName
    })
}

function escapeTxt (text) {
  if (!text) return ''
  return text.replace(/"/g, '\'').replace(/\r/g, '').replace(/\n/g, '')
}

app.get('/topics.csv',
middlewares.users.restrict,
middlewares.forums.findByName,
middlewares.topics.findAllFromForum,
middlewares.forums.privileges.canChangeTopics,
function getCsv (req, res, next) {
  const infoTopics = [ titles.concat(
    req.forum.topicsAttrs.map(attr => attr.title)
  )]
  const attrsNames = req.forum.topicsAttrs
    .map(attr => attr.name)

  req.topics.forEach((topic) => {
    if (topic.attrs === undefined){
      topic.attrs = {}
    }
    infoTopics.push([
      topic.id,
      `"${escapeTxt(topic.mediaTitle)}"`,
      fullUrl(topic.id, req.forum.name),
      ].concat(attrsNames.map(name => topic.attrs[name] || '')))
  })

  json2csv(infoTopics, function (err, csv) {
    if (err)  {
      throw new Error('topics.csv: array to csv error')
    }
    res.status(200)
    res.set({
      'Content-Type': 'text/csv; charset=utf-8',
      'Content-Disposition': 'attachment; filename=' + req.forum.name.replace(/\s/g, '-') + '.csv'
    })
    res.write(csv)
    res.end()
  }, { prependHeader: false })
})

app.post('/topics.csv',
middlewares.users.restrict,
function postCsv (req, res) {  
  const body = req.body.csv
  console.log(req.body.csv)
  csv2json(body, function(err, json) {
    if (err) {
      //res.status (500) 
      //return status 500
      throw new Error('csv to json error')
    }
    console.log(json)
    res.status(200)
    res.end()
  })
})
@@ -24,24 +24,55 @@
 ('express')%0A
+const debug = require('debug')%0A
 const json2c
@@ -159,41 +159,8 @@
 son%0A
-const moment = require('moment')%0A
 cons
@@ -318,16 +318,63 @@
 -api')%0A%0A
+const log = debug('democracyos:api:topic:csv')%0A
 const ap
@@ -1011,21 +1011,16 @@
 .concat(
-%0A    
 req.foru
@@ -1033,28 +1033,30 @@
 csAttrs.map(
+(
 attr
+)
  =%3E attr.tit
@@ -1062,12 +1062,10 @@
 tle)
-%0A  
 )
+ 
 %5D%0A  
@@ -1114,20 +1114,22 @@
    .map(
+(
 attr
+)
  =%3E attr
@@ -1204,16 +1204,17 @@
 defined)
+ 
 %7B%0A      
@@ -1355,20 +1355,17 @@
 um.name)
-,%0A  
+%0A
     %5D.co
@@ -1384,20 +1384,22 @@
 mes.map(
+(
 name
+)
  =%3E topi
@@ -1484,17 +1484,16 @@
 if (err)
- 
  %7B%0A     
@@ -1497,56 +1497,81 @@
     
-throw new Error('topics.csv: array to csv error'
+log('get csv: array to csv error', err)%0A      return res.status(500).end(
 )%0A  
@@ -1912,18 +1912,16 @@
 , res) %7B
-  
 %0A  const
@@ -1945,36 +1945,8 @@
 csv%0A
-  console.log(req.body.csv)%0A
   cs
@@ -1966,16 +1966,17 @@
 function
+ 
 (err, js
@@ -2006,95 +2006,81 @@
     
-//res.status (500) 
+log('get csv: array to csv error', err)
 %0A      
-//
 return 
+res.
 status
- 500%0A      throw new Error('csv to json error'
+(500).end(
 )%0A  
@@ -2142,12 +2142,13 @@
 nd()%0A  %7D)%0A%7D)
+%0A

update js style