require "rails_helper"

RSpec.describe AudioAnalysis, type: :model do
  let(:video) { build(:video, :with_audio_only_edl_file, audio_analysis_uid: "123abc") }

  subject { AudioAnalysis.new(video) }

  describe "#to_hash" do
    it "includes edl" do
      allow(ENV).to receive(:[])
      allow(ENV).to receive(:[]).with("AWS_BUCKET").and_return("mybucket")

      expect(subject.to_hash).to include({
        edl_bucket_name: "mybucket",
        edl_object_name: video.audio_only_edl_file.key,
      })
    end

    it "includes acquired media audio file infos" do
      create(:acquired_media_release, project: video.project, file_infos: [
        build(:file_info, id: 1, filename: "acquired_media_1.mp3", content_type: "audio/mp3"),
        build(:file_info, id: 2, filename: "acquired_media_2.aif", content_type: "audio/aif"),
        build(:file_info, id: 3, filename: "acquired_media_3.wav", content_type: "audio/wav"),
        build(:file_info, id: 4, filename: "acquired_media_4.mov", content_type: "video/mov"),
        build(:file_info, id: 5, filename: "acquired_media_4.mp4", content_type: "video/mp4"),
      ])

      expect(subject.to_hash).to have_key(:acquired_audio)
      expect(subject.to_hash[:acquired_audio]).to include(id: 1, filename: "acquired_media_1.mp3")
      expect(subject.to_hash[:acquired_audio]).to include(id: 2, filename: "acquired_media_2.aif")
      expect(subject.to_hash[:acquired_audio]).to include(id: 3, filename: "acquired_media_3.wav")
      expect(subject.to_hash[:acquired_audio]).not_to include(id: 4, filename: "acquired_media_4.mov")
      expect(subject.to_hash[:acquired_audio]).not_to include(id: 5, filename: "acquired_media_5.mp4")
    end

    it "includes music release file infos" do
      create(:music_release, 
             project: video.project, 
             file_infos: [
               build(:file_info, id: 1, filename: "original_music_1.mp3"),
               build(:file_info, id: 2, filename: "original_music_2.mp3"),
             ],
             composers: build_list(:composer, 1),
             publishers: build_list(:publisher, 1),
            )

      expect(subject.to_hash).to have_key(:original_music)
      expect(subject.to_hash[:original_music]).to include(id: 1, filename: "original_music_1.mp3", 
                                                          composers: "Beethoven, Affiliation, 100.0", 
                                                          publishers: "Jingle Punks, Affiliation, 100.0")
      expect(subject.to_hash[:original_music]).to include(id: 2, filename: "original_music_2.mp3", 
                                                          composers: "Beethoven, Affiliation, 100.0", 
                                                          publishers: "Jingle Punks, Affiliation, 100.0")
    end
  end

  describe "#results" do
    it "returns response from API request using audio analysis uid" do
      response = double("response", results: [])
      edl_event_gateway = double("edl_event_gateway", fps: 29.97, edl_offset_seconds: 0)
      allow(EdlEventGateway).to receive(:new).and_return(edl_event_gateway)
      allow(BrayniacAI::AudioRecognition).to receive(:find).and_return(response)

      expect(subject.results).to eq(response.results)
      expect(BrayniacAI::AudioRecognition).to have_received(:find).with(video.audio_analysis_uid, params: { fps: 29.97, edl_offset_seconds: 0 })
    end
  end
end
