require "rails_helper"

RSpec.describe ContractDownloadsController, 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 "#create" do
    it "enqueues zip file generation job" do
      create_list(:download, 4, project_id: project.id, name: "#{project.name.parameterize}_appearance-releases")

      expect {
        post :create, params: { project_id: project.id, release_type: "AppearanceRelease" }, format: :js
      }.to have_enqueued_job(GenerateContractsZipJob)
    end

    it "creates a download record with 'not_started' status" do
      create_list(:download, 4, project_id: project.id, name: "#{project.name.parameterize}_appearance-releases")

      expect {
        post :create, params: { project_id: project.id, release_type: "AppearanceRelease" }, format: :js
      }.to change(Download, :count).by(1)

      expect(Download.last.status).to eq('not_started')
    end
    
    context "When there is no existing job" do
      it "shows a notification to user" do
        allow(ProjectsChannel).to receive(:broadcast_download_generation_update).with(be_kind_of(Download), I18n.t("contract_downloads.download.pending", release_type: "Appearance Release"))

        post :create, params: { project_id: project.id, release_type: "AppearanceRelease" }, format: :js

        expect(ProjectsChannel).to have_received(:broadcast_download_generation_update).with(be_kind_of(Download), I18n.t("contract_downloads.download.pending", release_type: "Appearance Release"))
      end
    end

    context "When there are existing jobs" do
      let(:appearance_release_download) { create(:download, project_id: project.id, name: "#{project.name.parameterize}_appearance-releases") }
      let(:acquired_media_release_download) { create(:download, project_id: project.id, name: "#{project.name.parameterize}_acquired-media-releases", release_type: "AcquiredMediaRelease") }

      before do
        allow(Download).to receive_message_chain(:unfinished_desc_order, :offset).and_return([acquired_media_release_download, appearance_release_download])
        allow(ProjectsChannel).to receive(:broadcast_download_generation_update)
      end

      it "shows names of other contracts in the notification, which are in progress" do
        broadcast_message = "<p>Your Music Release contracts are being prepared for download. You will be notified when it's ready.\n</p>\n<p class=\"mt-3\">The following downloads are also in progress:</p> \n<ul>\n      <li>Acquired Media Release contracts (as of less than a minute ago)\n    </li>\n      <li>Appearance Release contracts (as of less than a minute ago)\n    </li>\n</ul>\n"

        post :create, params: { project_id: project.id, release_type: "MusicRelease" }, format: :js

        expect(ProjectsChannel).to have_received(:broadcast_download_generation_update).with(be_kind_of(Download), broadcast_message)
      end
    end
  end
end
