// Produces a good looking HTML for the given newsletter and stories
func Produce(newsletter *newsletters.Newsletter, all_stories []*stories.Story) string {
	var output bytes.Buffer

	var everything all_information
	everything.Title = newsletter.GetTitle()
	everything.NumStories = len(all_stories)

	// Group stories based on source type
	everything.TextStories = make([]*stories.Story, 0, 10)
	for _, story := range all_stories {
		everything.TextStories = append(everything.TextStories, story)
	}

	tmpl, err := template.ParseFiles("./src/munch/producer/email.html")
	if err != nil {
		fmt.Printf("There was an error loading the template file: %s\n", err)
	}
	tmpl.Execute(&output, everything)

	b := output.Bytes()
	len_total := len(b)

	minified, err := htmlmin.Minify(b, &htmlmin.Options{MinifyScripts: true, MinifyStyles: true, UnquoteAttrs: true})

	len_min := len(minified)

	if err != nil {
		fmt.Printf("There was an error in minifying... using the unminified version\n")
		return output.String()
	}
	fmt.Printf("Total saving of %d bytes\n", (len_total - len_min))
	return string(minified)
}
// WARNING: Mutation of newsletter here
func (db *Db) FetchNewsletterSources(newsletter *newsletters.Newsletter) {
	output, err := db.connection.Query("SELECT source_id, source_lastchecked FROM broccoli_newsletters_sources WHERE newsletter_id = ?", newsletter.GetId())
	if err != nil {
		fmt.Printf("There was an error fetching all the source IDs\n%s\n", err)
	}

	for output.Next() {
		var source_id int
		var source_lastchecked int64
		output.Scan(&source_id, &source_lastchecked)
		fmt.Printf("Source last checked = %d\n", source_lastchecked)
		source := db.FetchSource(source_id)
		newsletter.AddSource(source, source_lastchecked)
	}
}
func (db *Db) PersistNewsletter(newsletter *newsletters.Newsletter) {
	stmt, err := db.connection.Prepare("UPDATE broccoli_newsletters SET title=?, pubdate=? WHERE id = ?")
	if err != nil {
		fmt.Printf("There was an error\n")
	}
	result, err := stmt.Exec(newsletter.GetTitle(), newsletter.GetPubDate(), newsletter.GetId())
	rows_affected, err := result.RowsAffected()
	if rows_affected == 0 {
		// Use INSERT
		stmt, err = db.connection.Prepare("INSERT INTO broccoli_newsletters(title, pubdate) VALUES (?, ?)")
		_, err = stmt.Exec(newsletter.GetTitle(), newsletter.GetPubDate())
	}

	// Now, store the newsletter sources
	sources := newsletter.GetSources()
	sources_lastchecked := newsletter.GetSourcesLastChecked()
	newsletter_id := newsletter.GetId()
	for i, src := range sources {
		src_id := (*src).GetId()

		// Use update
		stmt, err = db.connection.Prepare("UPDATE broccoli_newsletters_sources SET newsletter_id=?, source_id=?, source_lastchecked=?")
		result, err = stmt.Exec(newsletter_id, src_id, sources_lastchecked[i])
		rows_affected, err = result.RowsAffected()
		if rows_affected == 0 {
			// Use insert
			stmt, err = db.connection.Prepare("INSERT broccoli_newsletters_sources(newsletter_id, source_id, source_lastchecked) VALUES (?, ?, ?)")
			_, err = stmt.Exec(newsletter.GetId(), newsletter_id, src_id, sources_lastchecked[i])
		}
	}
}