ActiveAdmin.register_page "POSA Card Uploads" do

  menu :label => 'Posa Cards Upload', :parent => 'JamClass'

  page_action :upload_posacards, :method => :post do
    PosaCard.transaction do

      puts params

      file = params[:jam_ruby_posa_card][:csv]
      array_of_arrays = CSV.read(file.tempfile.path)
      array_of_arrays.each do |row|
        if row.length != 5
          raise "UKNONWN CSV FORMAT!  Must be 5 columns"
        end

        code = row[0]
        lesson_package_type = row[1]
        preactivate = row[2].strip.downcase == "true"
        requires_purchase = row[3].downcase== "true"
        is_test = row[4].strip.downcase == "true"

        posa_card = PosaCard.new
        posa_card.code = code
        posa_card.lesson_package_type = LessonPackageType.find(lesson_package_type)
        posa_card.preactivate = preactivate
        posa_card.requires_purchase = requires_purchase
        posa_card.purchased = !requires_purchase
        posa_card.card_type = params[:jam_ruby_posa_card][:card_type]
        posa_card.is_test = is_test


        if posa_card.card_type == PosaCard::JAM_CLASS_4
          posa_card.is_lesson = true
          posa_card.credits = 4
        elsif posa_card.card_type == PosaCard::JAM_CLASS_2
          posa_card.is_lesson = true
          posa_card.credits = 2
        end
        posa_card.origin = file .original_filename
        posa_card.save!
      end

      redirect_to admin_posa_card_uploads_path, :notice => "Created #{array_of_arrays.length} POSA cards!"
    end


  end

=begin
  form :html => {:multipart => true} do |f|
    f.inputs "Details" do
      f.input :version, :hint => "Should match Jenkins build number of artifact"
      f.input :environment, :hint => "Typically just 'public'"
      f.input :product, :as => :select, :collection => JamRuby::ArtifactUpdate::PRODUCTS
    end
    f.inputs "Artifact Upload" do
      f.input :uri, :as => :file, :hint => "Upload the artifact from Jenkins"
    end

    f.actions

  end

# fr manual entry of huge CSVs from rails c command line

 PosaCard.transaction do


      array_of_arrays = CSV.read('codes.csv')
      array_of_arrays.each do |row|
        if row.length != 5
          raise "UKNONWN CSV FORMAT!  Must be 5 columns"
        end

        code = row[0]
        lesson_package_type = row[1]
        preactivate = row[2].strip.downcase == "true"
        requires_purchase = row[3].downcase== "true"
        is_test = row[4].strip.downcase == "true"

        posa_card = PosaCard.new
        posa_card.code = code
        posa_card.lesson_package_type = LessonPackageType.find(lesson_package_type)
        posa_card.preactivate = preactivate
        posa_card.requires_purchase = requires_purchase
        posa_card.purchased = !requires_purchase
        posa_card.card_type = 'jam_class_2'
        posa_card.is_test = is_test


        if posa_card.card_type == PosaCard::JAM_CLASS_4
          posa_card.is_lesson = true
          posa_card.credits = 4
        elsif posa_card.card_type == PosaCard::JAM_CLASS_2
          posa_card.is_lesson = true
          posa_card.credits = 2
        end
        posa_card.origin = 'posa-cards-amazon-test-drive-paid-4-50k.csv'
        posa_card.save!
      end
    end
=end

  content do
    active_admin_form_for PosaCard.new, :url => admin_posa_card_uploads_upload_posacards_path, :builder => ActiveAdmin::FormBuilder do |f|
        f.inputs "Upload POSA Cards" do
          f.input :csv, as: :file, required: true, :label => "A single column CSV that contains ONE type of gift card (5 JamTrack, 10 JamTrack, 4 JamClass etc)"
          f.input :card_type, required:true, as: :select, :collection => JamRuby::PosaCard::CARD_TYPES

        end
        f.actions
      end
    end

end

