const Ramekin = require('./ramekin')
const util = require('./lib/util')
const moment = require('moment')
const fs = require('fs') 
const snapshotTestTimeOptions = {
  start: moment('2019-04-22T23:48:05+00:00').subtract(1, 'day').toDate(),
  end: moment('2019-04-22T23:48:05+00:00').toDate()
}
const r = new Ramekin()
const articles = JSON.parse(fs.readFileSync(`${__dirname}/tests/test-articles.json`, 'utf8'))
r.ingestAll(articles)
const searchOptions = r.buildSearchCriteria(snapshotTestTimeOptions)

// find all the common phrases used in respective subject, over the past day
const usedPhrases = r.usedPhrases(searchOptions)
console.log(`There are ${usedPhrases.length} used phrases and ${Object.keys(r.docs).length} docs`)
// duplicated data used later for sorting
let trendPhrases = r.trendUsedPhrases(usedPhrases, searchOptions)
// remove sub phrases (i.e. "Tour de", compared to "Tour de France")
trendPhrases = r.removeSubPhrases(trendPhrases)
const docPhrases = r.constructor.getDocPhrasesFromTrends(trendPhrases)

// rank results - @todo: needs making nicer
trendPhrases.sort((a, b) =>
  b.score === a.score ? b.phrase.length - a.phrase.length : b.score - a.score
)

console.log(trendPhrases.slice(0, 10))

