require "rails_helper"

module ExcelReports
  module VideoReports
    RSpec.describe ReleasableDataAdapter do
      let(:user) { create(:user) }
      let(:project) { create(:project, account:user.primary_account, description: "This is the video project description.") }
      let(:video) { create(:video, project: project, number: "45", name: "Amazing Race") }
      let(:acquired_media_release) { create(:acquired_media_release, project: project, name: "releasable name", person_address_street1: "contact_address") }
      let(:confirmation) do
        create(:video_release_confirmation,
          video: video,
          releasable: acquired_media_release,
          clip_name: "clippy",
          timecode_in: "timecode_in",
          timecode_out: "timecode_out",
          duration: "duration",
          description: "description",
          source_file_name: "cat.mov"
        )
      end

      subject { described_class.new(confirmation) }

      describe "#description" do
        context "when confirmation contains description" do
          it "returns description from confirmation" do
            expect(subject.description).to eq "description"
          end
        end

        context "when confirmation does NOT contain description" do
          it "returns project description" do
            confirmation.description = nil

            expect(subject.description).to eq "This is the video project description."
          end
        end
      end

      describe "#episode_number" do
        it "returns video number" do
          expect(subject.episode_number).to eq "45"
        end
      end

      describe "#episode_title" do
        it "returns video name" do
          expect(subject.episode_title).to eq "Amazing Race"
        end
      end

      describe "#name" do
        it "returns releasable name" do
          expect(subject.name).to eq "releasable name"
        end
      end

      describe "#source_file_name" do
        it "returns source_file_name from confirmation" do
          expect(subject.source_file_name).to eq "cat.mov"
        end
      end

      describe "#clip_name" do
        it "returns clip_name from confirmation" do
          expect(subject.clip_name).to eq "clippy"
        end
      end

      describe "#source_file_and_clip_names" do
        context "when source_file_name and clip_name present" do
          it "returns source_file_name and clip_name from confirmation joined by -" do
            expect(subject.source_file_and_clip_names).to eq "cat.mov - clippy"
          end
        end

        context "when only source_file_name present" do
          it "returns source_file_name from confirmation" do
            confirmation.source_file_name = "cat.mov"
            confirmation.clip_name = nil
            expect(subject.source_file_and_clip_names).to eq "cat.mov"
          end
        end

        context "when only clip_name present" do
          it "returns clip_name from confirmation" do
            confirmation.source_file_name = nil
            confirmation.clip_name = "clippy"
            expect(subject.source_file_and_clip_names).to eq "clippy"
          end
        end

        context "when source_file_name and clip_name present" do
          it "returns empty string" do
            confirmation.source_file_name = nil
            confirmation.clip_name = ""
            expect(subject.source_file_and_clip_names).to eq ""
          end
        end
      end

      describe "#timecode_in" do
        it "returns timecode_in from confirmation" do
          expect(subject.timecode_in).to eq "timecode_in"
        end
      end

      describe "#timecode_out" do
        it "returns timecode_out from confirmation" do
          expect(subject.timecode_out).to eq "timecode_out"
        end
      end

      describe "#duration" do
        it "returns duration from confirmation" do
          expect(subject.duration).to eq "duration"
        end
      end

      describe "#confirmation" do
        it "returns given confirmation" do
          expect(subject.confirmation).to eq confirmation
        end
      end

      describe "#contact_address" do
        it "returns address from contact info" do
          expect(subject.contact_address).to eq("contact_address")
        end
      end
    end
  end
end
