require "rails_helper"

RSpec.describe VideoAnalysisPresenter do
  let(:project) { build(:project) }
  let(:video_analysis) { instance_double(VideoAnalysis) }
  let(:matches_presenter) { instance_double(MatchesPresenter) }

  describe "#stale?" do
    it "returns true if appearance releases have been uploaded since the last analysis date" do
      appearance_release = create(:appearance_release, project: project, created_at: Time.zone.now)
      video = create(:video, project: project, analysis_started_at: 1.day.ago)

      video_analysis_presenter = described_class.new(video, video_analysis, matches_presenter)

      expect(video_analysis_presenter).to be_stale
    end

    it "returns false if appearance releases have not been uploaded since the last analysis date" do
      appearance_release = create(:appearance_release, project: project, created_at: 1.day.ago)
      video = create(:video, project: project, analysis_started_at: Time.zone.now)

      video_analysis_presenter = described_class.new(video, video_analysis, matches_presenter)

      expect(video_analysis_presenter).not_to be_stale
    end

    it "includes talent releases" do
      talent_release = create(:talent_release, project: project, created_at: 1.hour.ago)
      video = create(:video, project: project, analysis_started_at: 2.hours.ago)

      video_analysis_presenter = described_class.new(video, video_analysis, matches_presenter)

      expect(video_analysis_presenter).to be_stale
    end

    it "includes acquired media releases" do
      acquired_media_release = create(:acquired_media_release, project: project, created_at: 1.hour.ago)
      video = create(:video, project: project, analysis_started_at: 2.hours.ago)

      video_analysis_presenter = described_class.new(video, video_analysis, matches_presenter)

      expect(video_analysis_presenter).to be_stale
    end
  end

  describe "#bookmarks" do
    it "returns bookmarks from video" do
      video = create(:video, project: project)
      bookmark = create(:bookmark, video: video, notes: "Test notes")

      expect(described_class.new(video, video_analysis, matches_presenter).bookmarks).to eq [bookmark]
    end
  end

  describe "#video_url" do
    context "when video analysis is not successful" do
      let(:video) { create(:video, project: project, analysis_status: :pending) }

      it "returns video url from video" do
        expect(described_class.new(video, video_analysis, matches_presenter).video_url).to match /http:\/\/#{AppHost.new.domain_with_port}\/rails\/active_storage\/blobs\/.*--.*\/video_file.mp4/
      end
    end

    context "when video analysis is successful" do
      let(:video) { create(:video, project: project, analysis_status: :success) }

      it "returns video url from overlay" do
        allow(video_analysis).to receive(:overlay_video_url).and_return("www.google.com")

        expect(described_class.new(video, video_analysis, matches_presenter).video_url).to eq "www.google.com"
      end
    end
  end

  describe "#unreleased_appearances" do
    let(:video) { create(:video, project: project) }

    it "returns unreleased_appearances from video" do
      unreleased_appearance = create(:unreleased_appearance, video: video)

      expect(described_class.new(video, video_analysis, matches_presenter).unreleased_appearances).to eq [unreleased_appearance]
    end
  end

  describe "#chronological_appearances" do
    let(:video) { create(:video, project: project) }

    it "returns matches" do
      allow(matches_presenter).to receive(:build_chronological_matches).and_return(["matches"])
      expect(described_class.new(video, video_analysis, matches_presenter).chronological_appearances).to eq ["matches"]
    end
  end

  describe "#chronological_graphics_matches" do
    let(:video) { create(:video, project: project) }

    it "returns matches" do
      allow(matches_presenter).to receive(:build_graphics_matches).and_return(["matches"])
      expect(described_class.new(video, video_analysis, matches_presenter).chronological_graphics_matches).to eq ["matches"]
    end
  end

  describe "#all_tracks_edl_events" do
    let(:video) { create(:video, project: project) }

    it "returns all_tracks_edl_events" do
      allow(matches_presenter).to receive(:all_tracks_edl_events).and_return(["edl event"])
      expect(described_class.new(video, video_analysis, matches_presenter).all_tracks_edl_events).to eq ["edl event"]
    end
  end

  describe "#chronological_audio_matches" do
    let(:video) { create(:video, project: project) }

    it "returns matches" do
      allow(matches_presenter).to receive(:build_chronological_audio_matches).and_return(["matches"])
      expect(described_class.new(video, video_analysis, matches_presenter).chronological_audio_matches).to eq ["matches"]
    end
  end
end
