require 'rails_helper'

RSpec.describe BroadcastRecordingsController, type: :controller do
  render_views

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

  before do
    sign_in user
    stub_mux_live_stream
  end

  describe "#edit" do
    let(:broadcast) { create(:broadcast, project: project, name: "New Broadcast") }
    let(:recording) { create(:broadcast_recording, broadcast: broadcast) }

    before do
      stub_mux_live_stream
    end

    it "assigns project, broadcast, broadcast recording" do
      get :edit, params: { project_id: project, broadcast_id: broadcast, id: recording }, xhr: true

      expect(assigns(:project)).to have_attributes({
        id: project.id,
        name: project.name,
        account_id: project.account_id
      })

      expect(assigns(:broadcast)).to have_attributes({
        id: broadcast.id,
        name: broadcast.name,
        project_id: project.id
      })

      expect(assigns(:recording)).to have_attributes({
        id: recording.id,
        broadcast_id: broadcast.id,
        file_name: "high.mp4"
      })
    end
  end

  describe "#update" do
    let(:broadcast) { create(:broadcast, project: project, name: "New Broadcast") }
    let(:recording) { create(:broadcast_recording, broadcast: broadcast) }
    let(:recordings) { create_list(:broadcast_recording, 5, :with_random_asset_uid, broadcast: broadcast) }
    let(:starred_recordings) { create_list(:broadcast_recording, 5, :with_random_asset_uid, broadcast: broadcast, starred: true) }

    before do
      stub_mux_live_stream
    end

    it "updates the recording's name and description" do
      expect(recording.name).to eq(recording.download_file_name)
      expect(recording.description).to eq("No description provided for this recording.")

      patch :update, params: { project_id: project, broadcast_id: broadcast, id: recording, broadcast_recording: { name: "Just for fun", description: "I had fun while making this stream." } }, xhr: true
      recording.reload

      expect(recording.name).to eq("Just for fun")
      expect(recording.description).to eq("I had fun while making this stream.")
    end
  end

  describe "#destroy" do
    let(:broadcast) { create(:broadcast, project: project, name: "New Broadcast") }
    let(:recording) { create(:broadcast_recording, broadcast: broadcast) }

    it "hides the broadcast recording" do
      expect(recording.hidden).to be false

      post :destroy, params: { project_id: project, broadcast_id: broadcast, id: recording }, xhr: true
      
      expect(recording.reload.hidden).to be true
    end
  end
end
