#!/usr/bin/env coffee

# Create a bunch of mocking .json files simulating HN firebase data.

fs = require 'fs'
crypto = require 'crypto'
u = require '../src/utils'

dir = if process.argv[2] then process.argv[2] else 'data/json'

try
  fs.mkdirSync dir, '0755'
catch
  # -p

process.chdir dir

gen_story = (n) ->
  json = JSON.stringify {id: n, kids: [100+n, 100+n, 200+n, 300+n, 500+n], type: 'story'}
  fs.writeFileSync "#{n}.json", json

gen_comment = (n) ->
  json = JSON.stringify {id: n, type: 'comment'}
  fs.writeFileSync "#{n}.json", json

gen_comment_random_body = (n) ->
  json = JSON.stringify {id: n, type: crypto.pseudoRandomBytes(u.randrange 2500, 25000).toString('hex')}
  fs.writeFileSync "#{n}.json", json

gen_comment_invalid_json = (n) ->
  fs.writeFileSync "#{n}.json", "#{n}: invalid json"

gen_poll = (n, parts_num, missing) ->
  parts = (idx for idx in [n+1..n+parts_num])
  for idx in parts
    json = JSON.stringify {id: idx, type: 'pollopt'}
    fs.writeFileSync "#{idx}.json", json

  parts.push 300+n if missing
  json = JSON.stringify {id: n, parts: parts, type: 'poll'}
  fs.writeFileSync "#{n}.json", json


console.log "Output dir: #{dir}"

gen_story idx for idx in [1..10]
gen_comment idx for idx in [101..110]
gen_comment idx for idx in [201..210]
gen_comment_invalid_json idx for idx in [501..510]

gen_poll 20, 2, true            # broken
gen_poll 30, 2, false

#gen_comment_random_body idx for idx in [2000..3000] # notice the gap
#gen_comment_random_body idx for idx in [4000..23000]
