#!/usr/bin/env ruby

@global_count = 0

def create_notifications(count, msgtype)
  return unless count > 0
  Notification.transaction do
    notification_ids = Array.new
    puts "Creating #{count} notifications of type #{msgtype}..."
    1.upto count do |i|
      n = Notification.create :msg_type => msgtype, :generated => (Time.now + @options.offset), :sender => "test"
      notification_ids << n.id
      @global_count += 1
      puts "#{@global_count}/#{@options.count}"
    end
    param_sql_list = notification_ids.map do |n_id| 
      msgtype.parameter_ids.map {|p_id| "(#{n_id},#{p_id},'test')"}.join(",")
    end

    puts "Creating notification parameters..."
    param_sql = "insert into notification_parameters (notification_id,parameter_id,value) values " + param_sql_list.join(",")
    ActiveRecord::Base.connection.execute param_sql
  end
end

def create_messages count, msg_type, subscription
  return unless count > 0
  notification = Notification.first
  person = Person.first
  StarshipMessage.transaction do
    1.upto count do |i|
      StarshipMessage.create :notification_id => notification.id, :sender => "starship", :person_id => person.id,
        :subject => "[obs del] Package AOLserver-4.5.1 from home:Ibraima deleted",
        :body => "Package AOLserver-4.5.1 from project home:Ibraima was deleted ",
        :msg_type_id => msg_type.id, :msg_state_id => 1, :subscription_id => subscription.id, :created => DateTime.now
      @global_count += 1
      puts "#{@global_count}/#{@options.count}"
    end
  end
end


require 'optparse'
require 'ostruct'

@options = OpenStruct.new
@options.count = 200000
@options.env = "development"
@options.offset = 0

OptionParser.new do |opts|
  opts.banner = "Usage: script/fill_db [options]"

  opts.on("-c", "--count N", "number of entries to generate") do |count|
    puts "count:" +count
    @options.count = count.to_i
  end

  opts.on("-e", "--env ENV", "rails environment to use") do |env|
    @options.env = env
  end

  opts.on( "--offset OFF", "generated offset in days") do |offset|
    puts "offset: "+offset
    @options.offset = offset.to_i*60*60*24
  end

  opts.on( "--messages", "generate starship messages") do
    puts "generating starship messages"
    @options.messages = true
  end

  opts.on_tail("-h", "--help", "show this message") do
    puts opts
    exit
  end
end.parse!

puts "Initializing #{@options.env} environment..."
RAILS_ENV = @options.env
require File.dirname(__FILE__) + '/../config/boot'
require RAILS_ROOT+"/config/environment"
RAILS_DEFAULT_LOGGER.silence do

  mt = MsgType.find_by_msgtype "OBS_SRCSRV_REQUEST_CREATE"

  # create notifications in blocks of 1000
  # active record chokes if the notification parameter insert gets too long

  block_length = 1000
  unless @options.messages
    1.upto(@options.count / block_length) do
      create_notifications(block_length,mt)
    end
    create_notifications(@options.count % block_length, mt)
  else
    # create subscription
    sub = Subscription.create( :msg_type_id => mt.id, :person_id => Person.first.id )
    create_notifications(1, mt)
    1.upto(@options.count / block_length) do
      create_messages( block_length, mt, sub )
    end
    create_messages( @options.count % block_length, mt, sub )
    puts "Created messages for subscription #{sub.id}: /feeds/#{sub.id}"
  end

  puts "Finished."
end
