class Stripe::ProcessingService < ApplicationService
  def initialize(school, amount)
    @school = school
    @amount = amount
    @final_amount = amount
    @processing_fee_amount = 0
    apply_processing_fee
  end

  def sycamore_processing_fee(cents = true)
    percent = 1 # Default to 1%

    if @school.parents_pay_cc_fee
      stripe_processing_fee = (@final_amount * 0.029 + 30).to_i
      fee = @final_amount - @amount - stripe_processing_fee
      fee = 0 if fee < 0
      return cents ? fee : fee / 100.0.to_f
    end

    fee = (@final_amount * (percent / 100.0)).ceil
    return fee if cents

    fee / 100.0.to_f
  end

  def total_processing_fee_amount(cents = true)
    return @processing_fee_amount if cents

    @processing_fee_amount / 100.0.to_f
  end

  def final_amount(cents = true)
    return @final_amount if cents
    @final_amount / 100.0.to_f
  end

  private
    def apply_processing_fee
      if @school.parents_pay_cc_fee
        @final_amount += (@amount * 0.039).ceil + 30 # 2.9% + 1%
      end

      @processing_fee_amount = @final_amount - @amount
    end
end
