#db = 'http://127.0.0.1:5984/utools_portal'
require 'pry'
require 'yaml'
require 'json'
require 'net/http'
require 'uri'
require 'rack'
couch_settings = YAML.load(File.read(File.expand_path('./../../config/couchdb.yml', __FILE__)))
db = couch_settings['production']['database']
host = '127.0.0.1'
port = 5984
translations = JSON.parse(Net::HTTP.get(URI(File.join(
  'http://localhost:5984/', 
  db,
  '_design/couch_i18n::translation',
  '_view/all_documents?reduce=false&include_docs=true'
))))['rows'].map{|t| t['doc']}
translations.each do |t|
  next if t['_id'][0,3] == 't::'
  new_id = "t::#{t['key']}"
  new_json = {
    created_at: t['created_at'],
    updated_at: t['updated_at'],
    translation_value: t['value'],
    translated: t['translated'],
    ruby_class: t['ruby_class']
  }
  #doc_uri = URI(File.join( 'http://localhost:5984/', db, "#{t['_id']}?rev=#{t['_rev']}"))
  doc_uri = URI(File.join( 'http://localhost:5984/', db, Rack::Utils.escape(new_id)))
  create_request = Net::HTTP::Put.new(doc_uri)
  create_request["content-type"] = "application/json"
  create_request.body = new_json.to_json
  response = Net::HTTP.start(host, port) { |http| http.request(create_request) }

  delete_request = Net::HTTP::Delete.new( URI(File.join( 'http://localhost:5984/', db, "#{t['_id']}?rev=#{t['_rev']}")))
  response = Net::HTTP.start(host, port) { |http| http.request(delete_request) }
end
