require "rails_helper"

module ExcelReports
  module AudioReports
    RSpec.describe AudioConfirmationData do
      let(:audio_confirmation) do
        build(:audio_confirmation,
          music_type: "Vocal",
          music_category: "Feature",
          title: "title",
          source_file_name: "source_file_name",
        )
      end

      subject { described_class.new(audio_confirmation) }

      describe "#music_type" do
        it "returns first letter of music_type" do
          expect(subject.music_type).to eq "V"
        end

        context "when music_type nil" do
          before :each do
            audio_confirmation.music_type = nil
          end

          it "returns empty string" do
            expect(subject.music_type).to eq ""
          end
        end
      end

      describe "#music_category" do
        it "returns first letter of music_category" do
          expect(subject.music_category).to eq "F"
        end

        context "when music_category nil" do
          before :each do
            audio_confirmation.music_category = nil
          end

          it "returns empty string" do
            expect(subject.music_category).to eq ""
          end
        end
      end

      describe "#title_and_source_file_name" do
        it "returns title_and_source_file_name and source filename separated by dash" do
          expect(subject.title_and_source_file_name).to eq("title - source_file_name")
        end

        context "when source filename is blank" do
          it "returns title_and_source_file_name alone" do
            audio_confirmation.source_file_name = nil

            expect(subject.title_and_source_file_name).to eq("title")
          end
        end
      end

      describe "#origin" do
        context "for library music" do
          let(:audio_confirmation) { build(:audio_confirmation, :library) }

          it "returns 'Production Library (Non-affiliated)'" do
            expect(subject.origin).to eq("Production Library (Non-affiliated)") 
          end
        end

        context "for original music" do
          let(:audio_confirmation) { build(:audio_confirmation, :original) }

          it "returns 'Commissioned'" do
            expect(subject.origin).to eq("Commissioned") 
          end
        end
      end

      describe "#composers_with_split" do
        let(:audio_confirmation) { build(:audio_confirmation, composer_info: "John Doe, BMI, 50%") }

        it "includes name, affiliation, and percentage split" do
          expect(subject.composers_with_split).to eq "John Doe (BMI) 50.0%"
        end

        context "when there are co-writers " do
          let(:audio_confirmation) { build(:audio_confirmation, composer_info: "John Doe, Jane Doe, BMI, 50%") }
          
          it "includes both names" do
            expect(subject.composers_with_split).to eq "John Doe, Jane Doe (BMI) 50.0%"
          end
        end

        context "when there are multiple composers" do
          let(:audio_confirmation) { build(:audio_confirmation, composer_info: "John Doe, BMI, 50% | Jane Doe, Universal, 50%") }

          it "includes both composers separated by a newline" do
            expect(subject.composers_with_split).to eq "John Doe (BMI) 50.0%\n Jane Doe (Universal) 50.0%"
          end
        end

        context "when there is a cae number" do
          let(:audio_confirmation) { build(:audio_confirmation, composer_info: "John Doe, Jane Doe, BMI, 50%, $cae:abc123 | James Doe, Universal, 50%, $cae:def456") }

          it "does not include the cae number" do
            expect(subject.composers_with_split).to eq "John Doe, Jane Doe (BMI) 50.0%\n James Doe (Universal) 50.0%"
          end
        end
      end

      describe "#composers_cae_numbers" do
        let(:audio_confirmation) { build(:audio_confirmation, composer_info: "John Doe, BMI, 50%, $cae:abc123") }
        
        it "includes cae number" do
          expect(subject.composers_cae_numbers).to eq("abc123")
        end

        context "when there are multiple composers" do
          let(:audio_confirmation) { build(:audio_confirmation, composer_info: "John Doe, BMI, 50%, $cae:abc123 | Jane Doe, Universal, 50%, $cae:def456") }

          it "includes all cae numbers separated by a newline" do
            expect(subject.composers_cae_numbers).to eq("abc123\ndef456")
          end
        end

        context "when cae numbers are not present" do
          let(:audio_confirmation) { build(:audio_confirmation, composer_info: "John Doe, BMI, 50%") }

          it "returns an empty string" do
            expect(subject.composers_cae_numbers).to be_empty
          end
        end
      end
    end
  end
end
