require 'rails_helper'
require 'controllers/shared_normal_user_controller_actions_spec'
require 'controllers/shared_admin_user_controller_actions_spec'

# This spec was generated by rspec-rails when you ran the scaffold generator.
# It demonstrates how one might use RSpec to specify the controller code that
# was generated by Rails when you ran the scaffold generator.
#
# It assumes that the implementation code is generated by the rails scaffold
# generator.  If you are using any extension libraries to generate different
# controller code, this generated spec may or may not pass.
#
# It only uses APIs available in rails and/or rspec-rails.  There are a number
# of tools you can use to make these specs even more expressive, but we're
# sticking to rails and rspec-rails APIs to keep things simple and stable.
#
# Compared to earlier versions of this generator, there is very limited use of
# stubs and message expectations in this spec.  Stubs are only used when there
# is no simpler way to get a handle on the object needed for the example.
# Message expectations are only used when there is no simpler way to specify
# that an instance is receiving a specific message.

RSpec.describe GameDetailsController, type: :controller do
  # This should return the minimal set of attributes required to create a valid
  # Game. As you add validations to Game, be sure to
  # adjust the attributes here as well.
  let(:game) { FactoryBot.create :game }
  let(:player) { FactoryBot.create :player }
  let(:valid_attributes) do
    {
      player_id: player.id,
      game_id: game.id,
      team_id: FactoryBot.create(:team).id,
      hero_id: FactoryBot.create(:hero).id,
      solo_kills: 2,
      assists: 5,
      deaths: 3,
      time_spent_dead: 50,
      team_colour: 'blue',
      win: true
    }
  end

  let(:invalid_attributes) do
    {
      player_id: player.id,
      game_id: game.id,
      team_id: FactoryBot.create(:team).id,
      hero_id: FactoryBot.create(:hero).id,
      solo_kills: 2,
      assists: 5,
      deaths: 3,
      time_spent_dead: 50,
      team_colour: 'blue',
      win: nil
    }
  end

  let(:new_attributes) do
    {
      player_id: FactoryBot.create(:player).id,
      game_id: game.id,
      team_id: FactoryBot.create(:team).id,
      hero_id: FactoryBot.create(:hero).id,
      solo_kills: 2,
      assists: 5,
      deaths: 3,
      time_spent_dead: 50,
      team_colour: 'red',
      win: true
    }
  end

  def assert_update_successful(game_detail)
    expect(game_detail.team_colour).to eq 'red'
  end

  context 'a normal user' do
    login_user

    # This should return the minimal set of values that should be in the session
    # in order to pass any filters (e.g. authentication) defined in
    # GamesController. Be sure to keep this updated too.
    let(:valid_session) { {} }

    def check_access_denied
      expect(response).to redirect_to root_path
      expect(flash[:alert]).to eq "You don't have permission to take this action."
    end

    describe 'GET #new' do
      it 'access denied' do
        get :new, params: { game_id: game.id }, session: valid_session
        check_access_denied
      end
    end

    describe 'GET #edit' do
      it 'access denied' do
        game_detail = GameDetail.create! valid_attributes
        get :edit, params: { id: game_detail.to_param }, session: valid_session
        check_access_denied
      end
    end

    describe 'POST #create' do
      it 'access denied' do
        post :create, params: { game_id: game.id, detail: valid_attributes }, session: valid_session
        check_access_denied
      end
    end

    describe 'PUT #update' do
      it 'access denied' do
        game_detail = GameDetail.create! valid_attributes
        put :update, params: { id: game_detail.to_param, detail: valid_attributes }, session: valid_session
        check_access_denied
      end
    end

    describe 'DELETE #destroy' do
      it 'access denied' do
        game_detail = GameDetail.create! valid_attributes
        delete :destroy, params: { id: game_detail.to_param }, session: valid_session
        check_access_denied
      end
    end
  end

  context 'an admin user' do
    login_admin

    # This should return the minimal set of values that should be in the session
    # in order to pass any filters (e.g. authentication) defined in
    # GamesController. Be sure to keep this updated too.
    let(:valid_session) { {} }

    describe 'GET #new' do
      it 'assigns a new game_detail as @game_detail' do
        get :new, params: { game_id: game.id }, session: valid_session
        expect(assigns(:game_detail)).to be_a_new(GameDetail)
      end
    end

    describe 'GET #edit' do
      it 'assigns the requested game_detail as @game_detail' do
        game_detail = GameDetail.create! valid_attributes
        get :edit, params: { id: game_detail.to_param }, session: valid_session
        expect(assigns(:game_detail)).to eq(game_detail)
      end
    end

    describe 'POST #create' do
      context 'with valid params' do
        it 'creates a new GameDetail' do
          expect do
            post :create, params: { game_id: game.id, detail: valid_attributes }, session: valid_session
          end.to change(GameDetail, :count).by(1)
        end

        it 'assigns a newly created game_detail as @game_detail' do
          post :create, params: { game_id: game.id, detail: valid_attributes }, session: valid_session
          expect(assigns(:game_detail)).to be_a(GameDetail)
          expect(assigns(:game_detail)).to be_persisted
        end

        it 'redirects to the created game_detail' do
          post :create, params: { game_id: game.id, detail: valid_attributes }, session: valid_session
          expect(response).to redirect_to(detail_url(GameDetail.last))
        end
      end

      context 'with invalid params' do
        it 'assigns a newly created but unsaved game_detail as @game_detail' do
          post :create, params: { game_id: game.id, detail: invalid_attributes }, session: valid_session
          expect(assigns(:game_detail)).to be_a_new(GameDetail)
        end

        it "re-renders the 'new' template" do
          post :create, params: { game_id: game.id, detail: invalid_attributes }, session: valid_session
          expect(response).to render_template('new')
        end
      end
    end

    describe 'PUT #update' do
      context 'with valid params' do
        it 'updates the requested game_detail' do
          game_detail = GameDetail.create! valid_attributes
          put :update, params: { id: game_detail.to_param, detail: new_attributes }, session: valid_session
          game_detail.reload
          assert_update_successful game_detail
        end

        it 'assigns the requested game_detail as @game_detail' do
          game_detail = GameDetail.create! valid_attributes
          put :update, params: { id: game_detail.to_param, detail: valid_attributes }, session: valid_session
          expect(assigns(:game_detail)).to eq(game_detail)
        end

        it 'redirects to the game' do
          game_detail = GameDetail.create! valid_attributes
          put :update, params: { id: game_detail.to_param, detail: valid_attributes }, session: valid_session
          expect(response).to redirect_to(game_detail.game)
        end
      end

      context 'with invalid params' do
        it 'assigns the game_detail as @game_detail' do
          game_detail = GameDetail.create! valid_attributes
          put :update, params: { id: game_detail.to_param, detail: invalid_attributes }, session: valid_session
          expect(assigns(:game_detail)).to eq(game_detail)
        end

        it "re-renders the 'edit' template" do
          game_detail = GameDetail.create! valid_attributes
          put :update, params: { id: game_detail.to_param, detail: invalid_attributes }, session: valid_session
          expect(response).to render_template('edit')
        end
      end
    end

    describe 'DELETE #destroy' do
      it 'destroys the requested game_detail' do
        game_detail = GameDetail.create! valid_attributes
        expect do
          delete :destroy, params: { id: game_detail.to_param }, session: valid_session
        end.to change(GameDetail, :count).by(-1)
      end

      it 'redirects to the game_details list' do
        game_detail = GameDetail.create! valid_attributes
        delete :destroy, params: { id: game_detail.to_param }, session: valid_session
        expect(response).to redirect_to(game_details_url(game))
      end
    end
  end
end
