require "rails_helper"

RSpec.describe MusicRelease do
  it_behaves_like "a contractable"
  it_behaves_like "an exploitable"
  it_behaves_like "a notable"
  it_behaves_like "a releasable"
  it_behaves_like "a taggable"

  describe "associations" do
    it { is_expected.to have_many(:file_infos).dependent(:destroy) }
    it { is_expected.to have_many(:composers).dependent(:destroy) }
    it { is_expected.to have_many(:publishers).dependent(:destroy) }
  end

  describe "nested attributes" do
    it { is_expected.to accept_nested_attributes_for(:file_infos) }
    it { is_expected.to accept_nested_attributes_for(:composers) }
    it { is_expected.to accept_nested_attributes_for(:publishers) }
  end

  describe "validations" do
    it { is_expected.to validate_presence_of(:name) }

    describe "#person_email" do
      it { is_expected.to allow_value("test@test.com", nil).for(:person_email) }
      it { is_expected.not_to allow_values("foo", "test@foo", "N/A").for(:person_email) }
    end

    describe "composers" do
      it "validates length 1" do
        music_release = build(:music_release)
        music_release.composers = []

        expect(music_release.valid?).to eq false
        expect(music_release.errors[:composers]).to eq ["at least 1 required"]
      end

      it "validates all percentages add up to 100" do
        music_release = build(:music_release, composers: [build(:composer, percentage: 55), build(:composer, percentage: 55)])

        expect(music_release.valid?).to eq false
        expect(music_release.errors[:base]).to eq ["Composer percentages must add up to 100%"]
      end
    end

    describe "publishers" do
      it "validates length 1" do
        music_release = build(:music_release)
        music_release.publishers = []

        expect(music_release.valid?).to eq false
        expect(music_release.errors[:publishers]).to eq ["at least 1 required"]
      end

      it "validates all percentages add up to 100" do
        music_release = build(:music_release, publishers: [build(:publisher, percentage: 55), build(:publisher, percentage: 55)])

        expect(music_release.valid?).to eq false
        expect(music_release.errors[:base]).to eq ["Publisher percentages must add up to 100%"]
      end
    end
  end

  describe "attachments" do
    it { is_expected.to respond_to(:contract) }
  end

  describe "#uses_edl?" do
    it { is_expected.to be_uses_edl }
  end

  describe "#composer_info" do
    it "returns a single string with all the info included" do
      release = build(:music_release, composers: [
        build(:composer, name: "composer1", affiliation: "affiliation1", percentage: 50, cae_number: "cae1"),
        build(:composer, name: "composer2", affiliation: "affiliation2", percentage: 50, cae_number: "cae2"),
      ])

      expect(release.composer_info).to eq("composer1, affiliation1, 50.0, $cae:cae1|composer2, affiliation2, 50.0, $cae:cae2")
    end

    context "when a cae number is present" do
      it "includes it in the info" do
        release = build(:music_release, composers: [
          build(:composer, name: "composer1", affiliation: "affiliation1", percentage: 50, cae_number: nil),
          build(:composer, name: "composer2", affiliation: "affiliation2", percentage: 50, cae_number: "cae2"),
        ])

      expect(release.composer_info).to eq("composer1, affiliation1, 50.0|composer2, affiliation2, 50.0, $cae:cae2")
      end
    end
  end
end
