require 'csv'

class JamTrackController < ApplicationController

  respond_to :html

  def dump_released
    headers['Content-Disposition'] = "attachment; filename=\"released-jam-tracks.csv\""
    headers['Content-Type'] ||= 'text/csv'

    @jam_tracks = JamTrack.where(status: 'Production')
    render "jam_track/dump_released", :layout => nil
  end

  def dump_top_selling

    if params[:code] != Rails.application.config.data_dump_code
      render :text => "", :status => 404
      return
    end

    @jam_tracks = JamTrack.select([:id, :original_artist, :name]).find_by_sql("select count(jam_tracks.id) as count , jam_tracks.id, original_artist, name, (jmep_text is not null)  has_tap_in from jam_tracks inner join jam_track_rights on (jam_track_rights.jam_track_id = jam_tracks.id AND jam_track_rights.is_test_purchase = false) group by jam_tracks.id order by count(jam_tracks.id) desc ")

    headers['Content-Disposition'] = "attachment; filename=\"top-selling-jam-tracks.csv\""
    headers['Content-Type'] ||= 'text/csv'

    render "jam_track/dump_top_selling", :layout => nil

  end
end