require "rails_helper"

RSpec.describe "Brayniac AI API Integration", integration: true do
  before :all do
    Rails.application.config.active_storage.service = :amazon
  end

  after :each do
    ActiveStorage::Attachment.all.each { |attachment| attachment.purge }
  end

  after :all do
    Rails.application.config.active_storage.service = :test
  end

  describe "collection" do
    it "responds with collection id" do
      project = create(:project)
      create(:talent_release, project: project)
      create(:appearance_release, project: project)

      collection = HeadshotCollection.for_project(project)
      result = BrayniacAI::Collection.create(collection)

      expect(result.collection_id).not_to be_nil
    end
  end

  describe "document_analysis" do
    it "responds with headshot_filename, headshot_url" do
      file = Rack::Test::UploadedFile.new(file_fixture("AppearanceRelease.pdf"), "application/pdf")

      import = create(:import, file: file)
      result = BrayniacAI::DocumentAnalysis.create(bucket_name: ENV["AWS_BUCKET"], object_name: import.file.key)

      expect(result.headshot_url).not_to be_nil
      expect(result.headshot_filename).not_to be_nil
    end
  end

  describe "edl_parse" do
    context "with Adobe Premiere EDL" do
      it "responds with edl events" do
        video = create(:video)
        video.file.key = ENV["TEST_VIDEO_FILE"]
        video.edl_file.key = ENV["TEST_ADOBE_EDL_FILE"]
        video.save!

        files_for_request = FilesForRequest.new(video)

        results = BrayniacAI::EdlParse.create(
          video_bucket_name: files_for_request.aws_bucket_name,
          video_object_name: files_for_request.file_object_name,
          edl_bucket_name: files_for_request.aws_bucket_name,
          edl_object_name: files_for_request.edl_file_object_name,
          timecode_start: "00:00:00:00",
          timecode_end: nil,
          collection: {},
          edl_timecode_start: files_for_request.start_timecode_offset,
        )

        expect(results.results.first.to_json).to eq BrayniacAI::EdlParse::Result.new(
          channel: "V",
          clip_name: "Qualcomm.mp4",
          description: "",
          duration: "00:19:04",
          matches: [],
          source_file_name: "Qualcomm.mp4",
          start_time: 0.0,
          timecode_in: "00:00:00:00",
          timecode_out: "00:00:19:04",
        ).to_json
      end
    end

    context "with Avid EDL" do
      it "responds with edl events" do
        video = create(:video)
        video.file.key = ENV["TEST_VIDEO_FILE"]
        video.edl_file.key = ENV["TEST_AVID_EDL_FILE"]
        video.save!

        files_for_request = FilesForRequest.new(video)

        results = BrayniacAI::EdlParse.create(
          video_bucket_name: files_for_request.aws_bucket_name,
          video_object_name: files_for_request.file_object_name,
          edl_bucket_name: files_for_request.aws_bucket_name,
          edl_object_name: files_for_request.edl_file_object_name,
          timecode_start: "00:00:00:00",
          timecode_end: nil,
          collection: {},
          edl_timecode_start: files_for_request.start_timecode_offset,
        )

        expect(results.results.first.to_json).to eq BrayniacAI::EdlParse::Result.new(
          {
            channel: "V",
            clip_name: "MAY 16TH",
            description: "",
            duration: "00:05:00",
            matches: [],
            source_file_name: "MAY 16TH",
            start_time: 0.0,
            timecode_in: "00:59:55:00",
            timecode_out: "01:00:00:00",
          }
        ).to_json
      end
    end
  end

  describe "tag" do
    let!(:location_release) { create(:location_release_with_photo) }

    it "responds with tags about the photo" do
      results = BrayniacAI::Tag.create(bucket_name: ENV["AWS_BUCKET"], object_name: location_release.photos.first.key)

      expect(results.labels).to contain_exactly("Rug", "Flooring", "Floor", "Art", "Ornament", "Tapestry")
    end
  end

  describe "validation" do
    it "responds with true when face detected" do
      talent_release = create(:talent_release, photos: [Rack::Test::UploadedFile.new(file_fixture("hemsworth.jpeg"), "image/jpeg")])

      results = BrayniacAI::Validation.create(bucket_name: ENV["AWS_BUCKET"], object_name: talent_release.photos.first.key)

      expect(results.valid).to eq true
    end

    it "responds with true when face NOT detected" do
      talent_release = create(:talent_release, photos: [Rack::Test::UploadedFile.new(file_fixture("person_photo.png"), "image/png")])

      results = BrayniacAI::Validation.create(bucket_name: ENV["AWS_BUCKET"], object_name: talent_release.photos.first.key)

      expect(results.valid).to eq false
    end
  end
end
