require "rails_helper"

RSpec.describe VideoAnalysis do
  let(:project) { create(:project, headshot_collection_uid: "collection_id") }
  let(:video) { create(:video, project: project) }

  before :each do
    video.file.update(key: "generated-key")
  end

  describe "#to_hash" do
    context "when reanalysis is true" do
      it "returns hash of bucket_name, collection_id, video_object_name, reanalysis" do
        allow(ENV).to receive(:[])
        allow(ENV).to receive(:[]).with("AWS_BUCKET").and_return("video-dumping-ground")

        expect(described_class.new(video, true).to_hash).to eq({
          bucket_name: "video-dumping-ground",
          collection_id: "collection_id",
          video_object_name: "generated-key",
          reanalysis: true,
        })
      end
    end

    context "when reanalysis is not true" do
      it "returns hash of bucket_name, collection_id, video_object_name" do
        allow(ENV).to receive(:[])
        allow(ENV).to receive(:[]).with("AWS_BUCKET").and_return("video-dumping-ground")

        expect(described_class.new(video, false).to_hash).to eq({
          bucket_name: "video-dumping-ground",
          collection_id: "collection_id",
          video_object_name: "generated-key",
          reanalysis: false,
        })
      end
    end
  end

  describe "#as_json" do
    context "when reanalysis is true" do
      it "returns json with bucket_name, collection_id, video_object_name, reanalysis" do
        allow(ENV).to receive(:[])
        allow(ENV).to receive(:[]).with("AWS_BUCKET").and_return("video-dumping-ground")

        expect(described_class.new(video, true).as_json).to eq({
          bucket_name: "video-dumping-ground",
          collection_id: "collection_id",
          video_object_name: "generated-key",
          reanalysis: true,
        })
      end
    end

    context "when reanalysis is not true" do
      it "returns json with bucket_name, collection_id, video_object_name" do
        allow(ENV).to receive(:[])
        allow(ENV).to receive(:[]).with("AWS_BUCKET").and_return("video-dumping-ground")

        expect(described_class.new(video, false).as_json).to eq({
          bucket_name: "video-dumping-ground",
          collection_id: "collection_id",
          video_object_name: "generated-key",
          reanalysis: false,
        })
      end
    end
  end

  describe "#overlay_video_url" do
    let(:video_analysis) { described_class.new(video, false) }

    context "when use_overlay_video false" do
      before :each do
        video_analysis.use_overlay_video = false
      end

      it "returns nil" do
        expect(video_analysis.overlay_video_url).to be_nil
      end
    end

    context "when use_overlay_video true" do
      before :each do
        video_analysis.use_overlay_video = true
      end

      context "when overlay_video_url is None" do
        before :each do
          allow(BrayniacAI::FacialRecognition).to receive(:find).and_return(BrayniacAI::FacialRecognition.new({overlay_video_url: "None"}))
        end

        it "returns nil" do
          expect(video_analysis.overlay_video_url).to be_nil
        end
      end

      context "when overlay_video_url is not None" do
        before :each do
          allow(BrayniacAI::FacialRecognition).to receive(:find).and_return(BrayniacAI::FacialRecognition.new({overlay_video_url: "www.google.com"}))
        end

        it "returns overlay_video_url" do
          expect(video_analysis.overlay_video_url).to eq "www.google.com"
        end
      end
    end
  end

  describe "#first_appearances" do
    before :each do
      allow(BrayniacAI::FacialRecognition).to receive(:find).and_return(BrayniacAI::FacialRecognition.new({first_appearances: ["appearance 1"]}))
    end

    it "returns first_appearances from response" do
      expect(described_class.new(video, false).first_appearances).to eq ["appearance 1"]
    end
  end
end
