require "rails_helper"

RSpec.describe VideoAnalysesController do
  let(:admin) { create(:user, :admin) }
  let(:project) { create(:project, account: admin.accounts.first) }
  let(:video) { create(:video, project: project) }

  before do
    allow(AnalyzeVideoJob).to receive(:perform_later)
    allow(BrayniacAI::EdlParse).to receive(:create)
    sign_in admin
  end

  render_views

  describe "#create" do
    it "redirects to video analyses" do
      post :create, params: { video_id: video, use_route: video_video_analyses_path(video) }

      expect(response).to redirect_to(video_video_analyses_path(video))
    end

    it "reanalyzes the video" do
      expect(AnalyzeVideoJob).to receive(:perform_later).with(video, reanalysis: true)

      post :create, params: { video_id: video, use_route: video_video_analyses_path(video) }
    end

    it "reanalyzes the audio" do
      expect(AnalyzeAudioJob).to receive(:perform_later).with(video)

      post :create, params: { video_id: video, use_route: video_video_analyses_path(video) }
    end
  end

  describe "#show" do
    it "sets video_release_confirmations, talent_releases, appearance_releases, location_releases, acquired_media_releases, music_releases, material_releases, graphics_elements_data, audio_confirmations" do
      talent_releases = [create(:talent_release)]
      appearance_releases = [create(:appearance_release)]
      location_releases = [create(:location_release)]
      acquired_media_releases = [create(:acquired_media_release)]
      music_releases = [create(:music_release)]
      material_releases = [create(:material_release)]

      project = create(:project,
        name: "All the releases",
        account: admin.accounts.first,
        talent_releases: talent_releases,
        appearance_releases: appearance_releases,
        location_releases: location_releases,
        acquired_media_releases: acquired_media_releases,
        music_releases: music_releases,
        material_releases: material_releases,
      )
      video = create(:video,
        project: project,
        video_release_confirmations: [
          create(:video_release_confirmation, releasable: talent_releases.first),
        ]
      )
      create(:audio_confirmation, video: video)

      get :show, params: { video_id: video, use_route: video_video_analyses_path(video) }

      expect(assigns(:video_release_confirmations)).to eq [VideoReleaseConfirmation.first]
      expect(assigns(:talent_releases)).to eq talent_releases
      expect(assigns(:appearance_releases)).to eq appearance_releases
      expect(assigns(:location_releases)).to eq location_releases
      expect(assigns(:acquired_media_releases)).to eq acquired_media_releases
      expect(assigns(:music_releases)).to eq music_releases
      expect(assigns(:material_releases)).to eq material_releases
      expect(assigns(:video_analysis_presenter)).to be_an_instance_of(VideoAnalysisPresenter)
      expect(assigns(:graphics_elements_data)).to have_attributes(graphics_elements: [])
      expect(assigns(:unreleased_appearances_data)).to have_attributes(unreleased_appearances: [])
      expect(assigns(:audio_confirmations)).to eq [AudioConfirmation.last]
    end

    it "sorts confirmations by appears_at" do
      talent_releases = [create(:talent_release), create(:talent_release), create(:talent_release)]

      project = create(:project,
        name: "All the releases",
        account: admin.accounts.first,
        talent_releases: talent_releases,
      )
      video = create(:video,
        project: project,
        video_release_confirmations: [
          create(:video_release_confirmation, releasable: talent_releases.first, time_elapsed: "10"),
          create(:video_release_confirmation, releasable: talent_releases.second, time_elapsed: "50"),
          create(:video_release_confirmation, releasable: talent_releases.last, time_elapsed: "30"),
        ]
      )

      get :show, params: { video_id: video, use_route: video_video_analyses_path(video) }

      body = response.body
      first = body =~ /00:00:10:00/
      second = body =~ /00:00:30:00/
      last = body =~ /00:00:50:00/

      expect(first < second).to eq true
      expect(second < last).to eq true
    end
  end
end
