module DataHelpers
  class UploadManualAdjustment < DataHelpers::TemplateBasedUpload
    class << self
      def create_manual_adjustment
        clear_records
        create_adjustment :valid_manual_adjustment
        @records.first
      end

      def create_spreadsheet(valid:)
        fail "no template found!" unless @template_path
        clear_records
        if valid
          create_adjustment :valid_bulk_1_adjustment
          create_adjustment :valid_bulk_2_adjustment
        else
          create_adjustment :invalid_bulk_adjustment
        end

        populate_template
      end

      private

      def create_adjustment(yml_key)
        create_record yml_key
        # can be replaced by ERBing the yml
        add_field_to_last_record("comment", "QA Automation #{Time.stamp}")
        add_field_to_last_record("apply to year", Time.now.year)
        add_field_to_last_record("apply to month", Time.now.month)
      end

      def add_field_to_last_record(key, value)
        return unless @records.last[key].nil?
        @records.last[key] = value
      end
    end
  end
end
