class AffiliateCohortsHelper
  def self.percentage(opTop, opBottom)
    "#{(opTop/opBottom * 100).round(1)}%"
  end

  def self.quarter(date)
    case date.month
      when 1, 2, 3 then 0
      when 4, 5, 6 then 1
      when 7, 8, 9 then 2
      when 10, 11, 12 then 3
    end
  end

  def self.payments_for_months(affiliate_partner, year, start_month, end_month)
    JamRuby::AffiliateMonthlyPayment.where(
      "affiliate_partner_id = ? AND month >= ? AND month <= ? AND year = ?",
        affiliate_partner.id, 
        start_month, 
        end_month,
        year
      ).order('month DESC')
  end

  def self.payments_for_quarter(affiliate_partner, year, quarter)
    JamRuby::AffiliateQuarterlyPayment.where(
      "affiliate_partner_id = ? AND quarter = ? AND year = ?",
        affiliate_partner.id, 
        quarter,
        year
      ).order('quarter DESC')
  end

  def self.all_time_payments(affiliate_partner)
    JamRuby::AffiliateQuarterlyPayment.where(
      "affiliate_partner_id = ?", affiliate_partner.id
    )
  end

  def self.current_quarter_payments(affiliate_partner)
    AffiliateCohortsHelper.payments_for_quarter(affiliate_partner, 
      Date.today.year,
      AffiliateCohortsHelper.quarter(Date.today)
    )
  end

  def self.current_quarter_monthly_payments(affiliate_partner)
    AffiliateCohortsHelper.payments_for_months(affiliate_partner, 
      Date.today.beginning_of_quarter.year,
      Date.today.beginning_of_quarter.month,
      Date.today.end_of_quarter.month
    )
  end

  def self.prior_quarter_payments(affiliate_partner)
    prev_quarter_start = (Date.today.beginning_of_quarter - 1.day).beginning_of_quarter
    prev_quarter = AffiliateCohortsHelper.quarter(prev_quarter_start)
    AffiliateCohortsHelper.payments_for_quarter(affiliate_partner, 
      prev_quarter_start.year,
      prev_quarter
    )
  end

  def self.prior_quarter_payable_amount(affiliate_partner)
    total = AffiliateCohortsHelper.prior_quarter_payments(affiliate_partner).inject(0.0){ | sum, payment | 
      sum += payment.due_amount_in_cents }
    paid = AffiliateCohortsHelper.prior_quarter_payments(affiliate_partner).where(paid: false).inject(0.0){ | sum, payment | 
      sum += payment.due_amount_in_cents }
    (total - paid) / 100.0
  end
end

ActiveAdmin.register_page "Affiliate Cohorts" do
  menu parent: 'Reports'

  content :title => "Affiliate Cohorts" do
    table_for AffiliatePartner.includes(:partner_user).all do
      column 'Affiliate Name' do |partner|
        link_to partner.partner_name, admin_affiliate_path(partner)
      end 
      column 'Affiliate ID', :id
      column 'Affiliate Email', Proc.new{ | partner | partner&.partner_user&.email}
      column 'Affiliate Paypal', Proc.new{| partner | partner.paypal_id }
      column 'All Time Users', :referral_user_count
      column 'All Time Subscribers', Proc.new{ | partner | partner.subscribed_user_referrals.size } 
      column 'All Time Subscriber Conversion Rate', Proc.new{ | partner | 
        AffiliateCohortsHelper.percentage(partner.subscribed_user_referrals.size.to_f, partner.referral_user_count.to_f) }
      column 'All Time Revenues', Proc.new{ | partner | 
        number_to_currency(AffiliateCohortsHelper.all_time_payments(partner).inject(0.0){ | sum, payment | sum += payment.due_amount_in_cents } / 100.0) 
      }
      column 'Current Quarter Revenues', Proc.new{ | partner |
        number_to_currency(AffiliateCohortsHelper.current_quarter_payments(partner).inject(0.0){ | sum, payment | sum += payment.due_amount_in_cents } / 100.0 ) 
      }
      column 'Current Quarter Revenues by Month', Proc.new{ | partner |
        AffiliateCohortsHelper.current_quarter_monthly_payments(partner).each do |monthly_payment|
          li "#{Date::MONTHNAMES[monthly_payment.month]} #{monthly_payment.year} - #{number_to_currency(monthly_payment.due_amount_in_cents.to_f / 100.0)}"  
        end
      ''.html_safe  
      }
      column 'Prior Quarter Revenues', Proc.new{ | partner | 
        number_to_currency(AffiliateCohortsHelper.prior_quarter_payments(partner).inject(0.0){ | sum, payment | sum += payment.due_amount_in_cents } / 100.0)
      }
      column 'Prior Quarter Payable', Proc.new{ | partner | 
        number_to_currency(AffiliateCohortsHelper.prior_quarter_payable_amount(partner))
      } 
    end
  end
end