require "rails_helper"

RSpec.describe VideoAnalyses::AudioConfirmationsController, type: :controller do
  let(:user) { create(:user) }
  let(:project) { create(:project, account: user.primary_account) }
  let(:video) { create(:video, project: project) }

  before :each do
    sign_in user
  end

  render_views

  describe "#new" do
    let(:edl_gateway) { double(:edl_gateway) }

    before :each do
      allow(EdlEventGateway).to receive(:new).and_return(edl_gateway)
      allow(edl_gateway).to receive(:edl_events).and_return(build_list(:edl_event, 1))
    end

    it "responds successfully and sets audio_confirmation, edl_events_data, matched_filename" do
      get :new, params: {
        video_id: video.id,
        audio_confirmation: {
          time_elapsed: "00:01:00",
          channel: "V",
          timecode_in: "timecode_in",
          timecode_out: "timecode_out",
          duration: "duration",
          source_file_name: "source_file_name",
          clip_name: "clip_name",
          description: "description",
          music_type: "music_type",
          music_category: "music_category",
          composer_info: "composer_info",
          publisher_info: "publisher_info",
          catalog: "catalog",
          title: "title",
          confirmation_type: "library_music",
          edl_type: "audio",
        },
        matched_file_name: "matched_file_name",
      }, xhr: true

      expect(EdlEventGateway).to have_received(:new).with(
        instance_of(AudioFilesForRequest),
        "00:00:00:00",
        "00:00:00:00",
        channel_filter: "A",
      )
      expect(response).to be_successful
      expect(assigns(:audio_confirmation)).to have_attributes(
        id: nil,
        time_elapsed: "00:01:00",
        clip_name: "clip_name",
        description: "description",
        duration: "duration",
        source_file_name: "source_file_name",
        channel: "V",
        timecode_in: "timecode_in",
        timecode_out: "timecode_out",
        music_type: "music_type",
        music_category: "music_category",
        composer_info: "composer_info",
        publisher_info: "publisher_info",
        catalog: "catalog",
        title: "title",
        confirmation_type: "library_music",
      )
      expect(assigns(:edl_events_data)).to eq({
        edl_events: build_list(:edl_event, 1),
        edl_attributes: {
          channel: "V",
          timecode_in: "timecode_in",
          timecode_out: "timecode_out",
          duration: "duration",
          source_file_name: "source_file_name",
          clip_name: "clip_name",
          description: "description",
        },
        info_message: "An EDL event was found. Data is shown below",
      })
      expect(assigns(:matched_file_name)).to eq "matched_file_name"
    end

    context "when edl_type param is all_tracks" do
      it "uses the all tracks edl" do
        get :new, params: {
          video_id: video.id,
          audio_confirmation: {
            time_elapsed: "00:01:00",
            channel: "V",
            timecode_in: "timecode_in",
            timecode_out: "timecode_out",
            duration: "duration",
            source_file_name: "source_file_name",
            clip_name: "clip_name",
            description: "description",
            music_type: "music_type",
            music_category: "music_category",
            composer_info: "composer_info",
            publisher_info: "publisher_info",
            catalog: "catalog",
            title: "title",
            confirmation_type: "library_music",
            edl_type: "all_tracks",
          },
          matched_file_name: "matched_file_name",
        }, xhr: true

        expect(EdlEventGateway).to have_received(:new).with(
          instance_of(FilesForRequest),
          "00:00:00:00",
          "00:00:00:00",
          channel_filter: "A",
        )
      end
    end
  end

  describe "#create" do
    it "responds successfully and creates audio_confirmation" do
      expect {
        post :create, params: {
          video_id: video.id,
          audio_confirmation: {
            time_elapsed: "00:01:00",
            channel: "V",
            timecode_in: "timecode_in",
            timecode_out: "timecode_out",
            duration: "duration",
            source_file_name: "source_file_name",
            clip_name: "clip_name",
            description: "description",
            music_type: "music_type",
            music_category: "music_category",
            composer_info: "composer_info",
            publisher_info: "publisher_info",
            catalog: "catalog",
            title: "title",
            confirmation_type: "library_music",
          },
        }, xhr: true
      }.to change(AudioConfirmation, :count).by(1)

      expect(response).to be_successful
    end

    it "sets audio_confirmation_data, audio_confirmations_data" do
      expect {
        post :create, params: {
          video_id: video.id,
          audio_confirmation: {
            time_elapsed: "00:01:00",
            channel: "V",
            timecode_in: "timecode_in",
            timecode_out: "timecode_out",
            duration: "duration",
            source_file_name: "source_file_name",
            clip_name: "clip_name",
            description: "description",
            music_type: "music_type",
            music_category: "music_category",
            composer_info: "composer_info",
            publisher_info: "publisher_info",
            catalog: "catalog",
            title: "title",
            confirmation_type: "library_music",
          },
        }, xhr: true
      }.to change(AudioConfirmation, :count).by(1)

      expect(response).to be_successful
      expect(assigns(:audio_confirmation_data)).to have_attributes(
        source_file_name: "source_file_name",
        presented_source_file_name: "(L) source_file_name",
        timecode_in: "timecode_in",
        should_toggle_checkmark: true,
        is_valid: true,
      )
      expect(assigns(:audio_confirmations_data)).to have_attributes(
        audio_confirmations: [AudioConfirmation.last]
      )
    end
  end

  describe "#destroy" do
    let!(:audio_confirmation) do
      create(:audio_confirmation,
        source_file_name: "source_file_name",
        timecode_in: "timecode_in",
        confirmation_type: "original_music"
      )
    end

    it "sets audio_confirmation_data, audio_confirmations_data" do
      delete :destroy, params: { id: audio_confirmation.id }, xhr: true

      expect(assigns(:audio_confirmation_data)).to have_attributes(
        source_file_name: "source_file_name",
        presented_source_file_name: "(O) source_file_name",
        timecode_in: "timecode_in",
        should_toggle_checkmark: true,
        is_valid: true,
      )
      expect(assigns(:audio_confirmations_data).audio_confirmations).to eq([])
    end

    it "destroys given audio_confirmation" do
      expect {
        delete :destroy, params: { id: audio_confirmation.id }, xhr: true
      }.to change(AudioConfirmation, :count).from(1).to(0)
    end
  end
end
