require 'rails_helper'

RSpec.describe Public::BroadcastsController, type: :controller do
  render_views

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

  before do
    allow(MuxLiveStream).to receive(:new).and_return(double(id: "id", key: "key", playback_id: "playback_id"))
  end

  describe "#show" do
    let(:broadcast) { create(:broadcast, project: project, name: "Broadcast") }

    it "responds successfully" do
      get :show, params: { token: broadcast.token }

      expect(response).to be_successful
      expect(assigns(:broadcast)).to eq(broadcast)
    end

    it "renders zoom meeting button" do
      get :show, params: { token: broadcast.token }

      expect(response.body).to have_link("Video Conference", href: broadcast_zoom_meeting_url(broadcast.token))
    end

    it "doesn't render share url" do
      get :show, params: { token: broadcast.token }

      expect(response.body).not_to have_button "Copy Share URL"
      expect(response.body).not_to have_xpath "//input[@value='#{broadcast_url(broadcast.token)}']"
    end

    it "assigns required variables" do
      get :show, params: { token: broadcast.token }

      expect(assigns(:conference_url)).to eq broadcast_zoom_meeting_url(broadcast.token)
      expect(assigns(:broadcast)).to eq broadcast
    end

    context "when there are no multi-view broadcasts" do
      it "renders the view dropdown with just the current broadcast" do
        get :show, params: { token: broadcast.token }

        expect(response.body).to have_content "Switch View"
        expect(response.body).to have_selector(".dropdown-menu .dropdown-item.active", text: "Broadcast")
      end
    end

    context "when there are multi-view broadcasts" do
      it "renders the view dropdown with all the broadcasts" do
        other_broadcast = create(:broadcast, project: project, name: "Some Other Broadcast")

        get :show, params: { token: broadcast.token, multi_view_tokens: [broadcast.token, other_broadcast.token] }

        expect(response.body).to have_content "Switch View"
        expect(response.body).to have_selector(".dropdown-menu .dropdown-item.active", text: "Broadcast")
        expect(response.body).to have_selector(".dropdown-menu a.dropdown-item", text: "Some Other Broadcast")
      end
    end

    context "when there are no recordings for the current broadcast" do
      it "renders the view dropdown with a message" do
        get :show, params: { token: broadcast.token }

        expect(response.body).to have_content "Switch View"
        expect(response.body).to have_selector(".dropdown-menu .dropdown-item", text: "Recordings will appear here")
      end
    end

    context "when there are recordings available" do
      it "renders the view dropdown with the recordings" do
        recording = create(:broadcast_recording, broadcast: broadcast)

        get :show, params: { token: broadcast.token }

        expect(response.body).to have_content "Switch View"
        expect(response.body).to have_selector(".dropdown-menu a.dropdown-item", text: recording.download_file_name)
      end
    end
  end

  describe "#update" do
    let!(:broadcast) { create(:broadcast, :with_stream, skip_create_callback: true, project: project ) }
  
    it "uploads files to broadcast" do
      patch :update, params: { token: broadcast.token, broadcast: file_params }, xhr: true

      expect(broadcast.files.count).to eq(1)
      expect(broadcast.files.first.filename).to eq("contract.pdf")
    end

    it "sends an update to the broadcasts channel" do
      allow(BroadcastsChannel).to receive(:broadcast_file_upload_updates)

      patch :update, params: { token: broadcast.token, broadcast: file_params }, xhr: true

      expect(BroadcastsChannel).to have_received(:broadcast_file_upload_updates)
    end
  end

  after do
    # Set the callback again or it will affect other test cases where the callback is required
    Broadcast.set_callback(:create, :after, :create_mux_live_stream)
  end

  private

  def file_params
    path = Rails.root.join("spec", "fixtures", "files", "contract.pdf")
    contract_file = Rack::Test::UploadedFile.new(path, "application/pdf")

    { files: [contract_file] }
  end
end
