require "rails_helper"

describe ReleaseTemplateImportsController do
  render_views

  let(:current_user) { create(:user, :account_manager) }
  let(:project) { create(:project, members: current_user, account: current_user.primary_account) }
  let(:project_one) { create(:project, name: "Avengers", members: [current_user], account: current_user.primary_account) }
  let!(:project_one_template) { create(:contract_template, name: "First Contract Template", fee: 50, release_type: "appearance", project: project_one) }
  let(:project_two) { create(:project, name: "Justice League", members: [], account: current_user.primary_account) }
  let!(:project_two_template) { create(:contract_template, name: "Second Contract Template", fee: 50, release_type: "talent", project: project_two) }

  before do
    sign_in(current_user)
  end

  describe "#new" do

    it "responds ok" do
      get :new, params: { project_id: project }

      expect(response).to be_successful
    end

    it "has a search field form" do
      get :new, params: { project_id: project }

      expect(response.body).to have_button("search-button")
    end

    it "paginates the templates" do
      create_list(:contract_template, 20, project: project_one)

      get :new, params: { project_id: project }
      expect(response.body).to have_link("2", href: new_project_release_template_imports_path(project, page: 2))
    end

    it "doesn't render release templates from current project" do
      template = create(:contract_template, name: "Current Project Template", fee: 100, release_type: "appearance", project: project)

      get :new, params: { project_id: project }
      expect(response.body).not_to have_content("Current Project Template")
    end

    it "filters the templates by a query param" do
      get :new, params: { project_id: project, query: "Second" }, xhr: true

      expect(response.body).not_to have_content("First Contract Template")
      expect(response.body).to have_content("Second Contract Template")
    end

    context "for account manager" do
      it "renders all the available release templates from other projects" do
        get :new, params: { project_id: project }

        expect(response.body).to have_content("Avengers")
        expect(response.body).to have_content("First Contract Template")
        expect(response.body).to have_content("Appearance")

        expect(response.body).to have_content("Justice League")
        expect(response.body).to have_content("Second Contract Template")
        expect(response.body).to have_content("Talent")

        expect(response.body).to have_button("Import Selected Templates")
      end
    end

    context "for manager" do
      let(:manager) { create(:user, :manager, accounts: [current_user.primary_account]) }

      it "renders the release templates only from projects that he's member of" do
        project_one.project_memberships.create(user: manager)
        project.project_memberships.create(user: manager)
        sign_in(manager)

        get :new, params: { project_id: project }

        expect(response.body).to have_content("Avengers")
        expect(response.body).to have_content("First Contract Template")
        expect(response.body).to have_content("Appearance")

        expect(response.body).not_to have_content("Justice League")

        expect(response.body).to have_button("Import Selected Templates")
      end
    end

    context "for associate" do
      let(:current_user) { create(:user, :associate) }

      it "raises authorization error" do
        expect {
          get :new, params: { project_id: project }
        }.to raise_error(Pundit::NotAuthorizedError)
      end
    end
  end

  describe "#create" do
    let(:template_ids) { [project_one_template.id, project_two_template.id] }

    it "redirects" do
      post :create, params: { project_id: project, template_ids: template_ids }
      expect(response).to redirect_to(project_contract_templates_path(project))
    end

    it "copies selected templates" do
      expect {
        post :create, params: { project_id: project, template_ids: template_ids }
      }.to change(ContractTemplate, :count).by(2)
    end

    context "when current user is an associate" do
      let(:current_user) { create(:user, :associate) }

      it "raises authorization error" do
        expect {
          post :create, params: { project_id: project, template_ids: template_ids }
        }.to raise_error(Pundit::NotAuthorizedError)
      end
    end
  end
end
