require "rails_helper"

RSpec.describe VideoReleaseConfirmationsController, type: :controller do
  let(:admin) { create(:user, :admin) }
  let(:project) { create(:project, account: admin.accounts.first) }
  let(:video) { create(:video, project: project) }

  before :each do
    sign_in admin
  end

  describe "acquired_media_release" do
    let(:acquired_media_release) { create(:acquired_media_release) }

    describe "#new" do
      let(:edl_event_gateway) { instance_double(EdlEventGateway) }

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

      it "assigns video_release_confirmation, video, edl_events_data" do
        file_info = create(:file_info)

        post :new,
          params: {
            video_id: video,
            acquired_media_release_id: acquired_media_release,
            video_release_confirmation: {
              time_elapsed: nil,
              timecode_in: nil,
              timecode_out: nil,
              duration: nil,
              source_file_name: nil,
              clip_name: nil,
              description: nil,
              file_info_id: file_info,
            },
            use_route: new_video_acquired_media_release_video_release_confirmation_path(video, acquired_media_release)
          },
          xhr: true

        expect(assigns(:video_release_confirmation)).to have_attributes({
          video_id: video.id,
          releasable_id: acquired_media_release.id,
          time_elapsed: "",
          releasable_type: "AcquiredMediaRelease",
          channel: "V",
          timecode_in: "timecode_in",
          timecode_out: "timecode_out",
          duration: "duration",
          source_file_name: "source_file_name",
          clip_name: "clip_name",
          description: "description",
          file_info_id: file_info.id,
        })
        expect(assigns(:video)).to have_attributes({
          id: video.id,
          project_id: project.id,
          analysis_status: "not_started",
          analysis_started_at: nil,
          name: nil,
          number: nil,
          report_published_at: nil
        })
        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",
        })
      end
    end

    describe "#create" do
      it "assigns video_release_confirmations, video_release_confirmation" do
        file_info = create(:file_info)

        post :create,
          params: {
            video_id: video,
            acquired_media_release_id: acquired_media_release,
            video_release_confirmation: {
              channel: "V",
              time_elapsed: "5",
              timecode_in: "01:00:00:00",
              timecode_out: "01:00:05:00",
              duration: "5.0",
              source_file_name: "DISCLAIMER.AVI",
              clip_name: "DISCLAIMER.AVI.NEW.02",
              description: "Boat",
              file_info_id: file_info,
            },
            use_route: video_acquired_media_release_video_release_confirmations_path(video, acquired_media_release)
          },
          xhr: true

        expect(assigns(:video_release_confirmations)).to eq(video.video_release_confirmations)
        expect(assigns(:video_release_confirmation)).to have_attributes({
          time_elapsed: "5",
          timecode_in: "01:00:00:00",
          timecode_out: "01:00:05:00",
          duration: "5.0",
          source_file_name: "DISCLAIMER.AVI",
          clip_name: "DISCLAIMER.AVI.NEW.02",
          description: "Boat",
          file_info_id: file_info.id,
        })
      end

      it "creates a video confirmation" do
        expect {
          post :create,
            params: {
              video_id: video,
              acquired_media_release_id: acquired_media_release,
              video_release_confirmation: {
                channel: "V",
                time_elapsed: "5",
                timecode_in: "01:00:00:00",
                timecode_out: "01:00:05:00",
                duration: "5.0",
                source_file_name: "DISCLAIMER.AVI",
                clip_name: "DISCLAIMER.AVI.NEW.02",
                description: "Boat",
              },
              use_route: video_acquired_media_release_video_release_confirmations_path(video, acquired_media_release)
            },
            xhr: true
        }.to change { VideoReleaseConfirmation.count }.from(0).to(1)
      end
    end

    describe "#destroy" do
      let!(:video_release_confirmation) { create(:video_release_confirmation, video: video, releasable: acquired_media_release) }

      it "assigns releasable, video, video_release_confirmations" do
        delete :destroy,
          params: {
            id: video_release_confirmation,
            video_id: video,
            use_route: video_acquired_media_release_video_release_confirmations_path(video, video_release_confirmation)
          }, xhr: true

        expect(assigns(:releasable)).to eq(video_release_confirmation.releasable)
        expect(assigns(:video)).to eq(video)
        expect(assigns(:video_release_confirmations).to_a).to eq([])
      end

      it "destroys video confirmation" do
        expect {
          delete :destroy,
            params: {
              id: video_release_confirmation,
              video_id: video,
              use_route: video_acquired_media_release_video_release_confirmations_path(video, video_release_confirmation)
            },
            xhr: true
        }.to change { VideoReleaseConfirmation.count }.from(1).to(0)
      end
    end
  end

  describe "location_releases" do
    let(:location_release) { create(:location_release) }
    let(:edl_event_gateway) { instance_double(EdlEventGateway) }

    describe "#new" do
      before :each do
        allow(EdlEventGateway).to receive(:new).and_return(edl_event_gateway)
        allow(edl_event_gateway).to receive(:edl_events).and_return(build_list(:edl_event, 1))
      end

      it "assigns video_release_confirmation, video, edl_events_data" do
        post :new,
          params: {
            video_id: video,
            location_release_id: location_release,
            video_release_confirmation: {
              time_elapsed: nil,
              timecode_in: nil,
              timecode_out: nil,
              duration: nil,
              source_file_name: nil,
              clip_name: nil,
              description: nil,
            },
            use_route: new_video_location_release_video_release_confirmation_path(video, location_release)
          },
          xhr: true

        expect(assigns(:video_release_confirmation)).to have_attributes({
          video_id: video.id,
          releasable_id: location_release.id,
          time_elapsed: "",
          releasable_type: "LocationRelease",
          channel: "V",
          timecode_in: "timecode_in",
          timecode_out: "timecode_out",
          duration: "duration",
          source_file_name: "source_file_name",
          clip_name: "clip_name",
          description: "description",
        })
        expect(assigns(:video)).to have_attributes({
          id: video.id,
          project_id: project.id,
          analysis_status: "not_started",
          analysis_started_at: nil,
          name: nil,
          number: nil,
          report_published_at: nil
        })
        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",
        })
      end
    end

    describe "#create" do
      it "assigns video_release_confirmations, video_release_confirmation" do
        post :create,
          params: {
            video_id: video,
            location_release_id: location_release,
            video_release_confirmation: {
              channel: "V",
              time_elapsed: "5",
              timecode_in: "01:00:00:00",
              timecode_out: "01:00:05:00",
              duration: "5.0",
              source_file_name: "DISCLAIMER.AVI",
              clip_name: "DISCLAIMER.AVI.NEW.02",
              description: "Boat",
            },
            use_route: video_location_release_video_release_confirmations_path(video, location_release)
          },
          xhr: true

        expect(assigns(:video_release_confirmations)).to eq(video.video_release_confirmations)
        expect(assigns(:video_release_confirmation)).to have_attributes({
          time_elapsed: "5",
          timecode_in: "01:00:00:00",
          timecode_out: "01:00:05:00",
          duration: "5.0",
          source_file_name: "DISCLAIMER.AVI",
          clip_name: "DISCLAIMER.AVI.NEW.02",
          description: "Boat",
        })
      end

      it "creates a video confirmation" do
        expect {
          post :create,
            params: {
              video_id: video,
              location_release_id: location_release,
              video_release_confirmation: {
                time_elapsed: "5",
                timecode_in: "01:00:00:00",
                timecode_out: "01:00:05:00",
                duration: "5.0",
                source_file_name: "DISCLAIMER.AVI",
                clip_name: "DISCLAIMER.AVI.NEW.02",
                description: "Boat",
              },
              use_route: video_location_release_video_release_confirmations_path(video, location_release)
            },
            xhr: true
        }.to change { VideoReleaseConfirmation.count }.from(0).to(1)
      end
    end

    describe "#destroy" do
      let!(:video_release_confirmation) { create(:video_release_confirmation, video: video, releasable: location_release) }

      it "assigns releasable, video, video_release_confirmations" do
        delete :destroy,
          params: {
            id: video_release_confirmation,
            video_id: video,
            use_route: video_location_release_video_release_confirmations_path(video, video_release_confirmation)
          }, xhr: true

        expect(assigns(:releasable)).to eq(video_release_confirmation.releasable)
        expect(assigns(:video)).to eq(video)
        expect(assigns(:video_release_confirmations).to_a).to eq([])
      end

      it "destroys video confirmation" do
        expect {
          delete :destroy,
            params: {
              id: video_release_confirmation,
              video_id: video,
              use_route: video_location_release_video_release_confirmations_path(video, video_release_confirmation)
            },
            xhr: true
        }.to change { VideoReleaseConfirmation.count }.from(1).to(0)
      end
    end
  end
end
