require 'roo'

class Sheet
  def self.parse(adjustments_file)
    Sheet.new(adjustments_file).run
  end

  def initialize(adjustments_file)
    workbook = Roo::Spreadsheet.open(adjustments_file)
    @rows = workbook.sheet(0)

    @row_template = {account_id: 'Account ID (Required)', contract_id: 'Contract ID (Required)',
                     upc: 'UPC (Optional)', amount: 'Amount (Required)', currency: 'Currency (Required)',
                     activity_year: 'Activity year (Required)', activity_month: 'Activity month (Required)',
                     apply_year: 'Apply to Statement year (Required)', apply_month: 'Apply to Statement month (Required)',
                     adjustment_type: 'Adjustment Type (Required)',
                     comment: 'Comment (Optional)', distribution_type: 'Distribution Type (Optional)'}
  end

  def run
    parsed_rows = []
    @rows.each( @row_template ) do | row |
      row[:account_id]     = row[:account_id].to_i     unless row[:account_id].nil?
      row[:contract_id]    = row[:contract_id].to_i    unless row[:contract_id].nil?
      row[:activity_month] = row[:activity_month].to_i unless row[:activity_month].nil?
      row[:activity_year]  = row[:activity_year].to_i  unless row[:activity_year].nil?
      row[:apply_month]    = row[:apply_month].to_i    unless row[:apply_month].nil?
      row[:apply_year]     = row[:apply_year].to_i     unless row[:apply_year].nil?
      row[:reasons]        = []

      parsed_rows << row
    end

    parsed_rows
  end
end


