require "rails_helper"

RSpec.describe DownloadsController, type: :controller do
  render_views

  let(:current_user) { create(:user) }
  let(:project) { create(:project, :discovery_client, account: current_user.primary_account) }

  before do
    sign_in current_user
  end

  describe "#index" do
    it "responds successfully" do
      get :index, params: { project_id: project }

      expect(response).to be_successful
    end

    context "when there are no reports" do
      it "renders an empty message" do
        get :index, params: { project_id: project }

        expect(response.body).to have_content("Downloads will appear here")
      end
    end

    context "when there are many records" do
      it "paginates the table" do
        create_list(:download, 20, project: project, name: "#{project.name.parameterize}_appearance-releases")

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

    describe "for xhr request" do
      before do
        downloads = [
          create(:download_release_type_appearance, project: project),
          create(:download_release_type_music, project: project)
        ]
      end

      it "shows all downloads if search field is empty" do
        get :index, params: { project_id: project }, xhr: true

        expect(response.body).to have_content("Natgeo Contract")
        expect(response.body).to have_content("Discovery Contract")
      end

      it "shows only downloads with file name or type matching search query" do
        get :index, params: { project_id: project, query: "Natgeo" }, xhr: true

        expect(response.body).not_to have_content("Discovery Contract")
        expect(response.body).to have_content("Natgeo Contract")
        
        get :index, params: { project_id: project, query: "Music" }, xhr: true

        expect(response.body).to have_content("Music Release")
        expect(response.body).not_to have_content("Appearance Release")
      end

      it "removes pagination in case of limited search results" do
        create_list(:download_release_type_music, 20, project: project)

        get :index, params: { project_id: project, query: "Appearance" }, xhr: true
        expect(response.body).to have_no_link("1", href: project_downloads_path(project, page: 1))
      end
    end
  end

  describe "#destroy" do
    let!(:download) { create(:download, project: project) }
    
    it "responds with redirect" do
      delete :destroy, params: { project_id: project.id, id: download.id }

      expect(response).to be_redirect
      expect(response).to redirect_to(project_downloads_path(project))
      expect(flash.alert).not_to be_nil
    end

    it "destroys the record" do
      expect {
        delete :destroy, params: { project_id: project.id, id: download.id }
      }.to change(Download, :count).by(-1)
    end
  end
end
