require 'acceptance_helpers'

resource 'Subjects' do
  header 'Accept', 'application/json'
  header 'Content-Type', 'application/json'

  explanation "Subjects to digital resources, like Artes, Biologia."

  before { 12.times { create(:subject); create(:learning_object) } }

  let(:subject) { Subject.all }
  let(:learning_objects) { LearningObject.all }

  get '/v1/subjects' do

    example_request 'Get a list of subjects' do
      # active model serializer may render model associations in different order for collections (array of items), so we're verifing only returned ids
      expect(status).to eq(200)
    end
  end

  post 'v1/:type/:id/subjects' do
    include_context "authenticate_user"

    parameter :id, "The id of the object"
    parameter :type, "Represents the type of the object [LearningObject, Collection]"
    parameter :subjects, "The array of subjects"

    let(:type) {'learning_objects'}
    let(:id) {@learning_object.id}
    let(:subjects) { [subject.first.id ] }
    let(:raw_post) {params.to_json}

    before do
      @learning_object = create(:learning_object, publisher: @user)
    end

    example 'Adding subjects' do
      do_request
      expect(status).to eq(201)
    end
  end

  delete 'v1/:type/:id/subjects' do
    include_context "authenticate_user_admin"

    parameter :id, "The id of the object"
    parameter :type, "Represents the type of the object [LearningObject, Collection]"
    parameter :subjects, "The array of subjects"

    let(:type) {'learning_objects'}
    let(:id) {@learning_object.id}
    let(:subjects) { [subject.first.id ] }
    let(:raw_post) {params.to_json}

    before do
      @learning_object = create(:learning_object, publisher: @user)
      create(:subject_relation, subjectable: @learning_object, subject: subject.first)
    end

    example 'Removing subjects' do
      do_request
      expect(status).to eq(200)
    end
  end
end