ActiveAdmin.register_page "CampaignSpend" do
  menu :parent => 'JamClass'

  page_action :create_spend, :method => :post do

    campaign = params[:jam_ruby_campaign_spend][:campaign]
    year = params[:jam_ruby_campaign_spend][:year]
    month = params[:jam_ruby_campaign_spend][:month]
    spend = params[:jam_ruby_campaign_spend][:spend]

    if campaign.blank?
      redirect_to admin_campaignspend_path, :notice => "No campaign defined! Nothing done."
      return
    elsif spend.blank?
      redirect_to admin_campaignspend_path, :notice => "No spend defined! Nothing done."
      return
    elsif year.blank? || month.blank?
      spend = spend.to_f
      # get all cohorts for a given campaign
      campaign_cohorts = JamClassReport.where(campaign: campaign).where("cohort IS NOT NULL")
      year_months = []
      campaign_cohorts.each do |cohort|
        year_month = {year: cohort.cohort.year, month: cohort.cohort.month}
        year_months << year_month
      end

      if campaign_cohorts.length > 0
        per_month = spend / campaign_cohorts.length
        year_months.each do |year_month|
          campaign_spend = CampaignSpend.where(campaign: campaign).where(year: year_month[:year]).where(month: year_month[:month]).first

          if campaign_spend.nil?
            campaign_spend = CampaignSpend.new
          end
          campaign_spend.campaign = campaign
          campaign_spend.month = year_month[:month]
          campaign_spend.year = year_month[:year]
          campaign_spend.spend = per_month

          campaign_spend.save!

        end
      else
        redirect_to admin_campaignspend_path, :notice => "No data found for campaign: #{campaign}"
        return
      end


      redirect_to admin_campaignspend_path, :notice => "Campaign #{campaign} updated with a per month value of $#{per_month} (#{year_months.length} months worth of data found)"
    else
      campaign_spend = CampaignSpend.where(campaign: campaign).where(year: year).where(month: month).first

      if campaign_spend.nil?
        campaign_spend = CampaignSpend.new
      end
      campaign_spend.campaign = campaign
      campaign_spend.month = month
      campaign_spend.year = year
      campaign_spend.spend = spend

      campaign_spend.save!

      redirect_to admin_campaignspend_path, :notice => "Campaign spend updated: #{campaign}:#{year}-#{month} = $#{spend}"
    end

  end


  content do

    para do
      link_to "JamClass Report", admin_jamclassreports_path
    end
    para do
      semantic_form_for CampaignSpend.new, :url => admin_campaignspend_create_spend_path, :builder => ActiveAdmin::FormBuilder do |f|
        f.inputs "Campaign Spend" do
          f.input :spend, :required => true, hint: "If you leave year or month blank, the system will divide up the specified spend amount here across all months seen for this campaign."
          f.input :campaign, :as => :select, hint: "If this appears empty or incomplete, visit the JamClass Report page (link above) and come back.", :required => true, :collection => JamClassReport.select('campaign').group('campaign').map(&:campaign)
          f.input :year, :as => :select, :hint => "Year of campaign spend (optional)", :collection => [Date.today.year, Date.today.year - 1]
          f.input :month, :as => :select, :hint => "Month of campaign (optional)", :collection => (1..12).map { |m| [Date::MONTHNAMES[m], m] }
        end
        f.actions
      end
    end

  end
end
