require "rails_helper"

RSpec.describe AcquiredMediaRelease 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"

  context "for native releases" do
    it { is_expected.to validate_attachment_of(:signature).on(:native) }
  end

  context "for non-native releases" do
    it { is_expected.to validate_attachment_of(:contract).on(:non_native) }
  end

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

  describe "nested attributes" do
    it { is_expected.to accept_nested_attributes_for(:file_infos) }
  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
  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 "#contract_file_name" do
    it "includes project name, release type, signed at date, release number and release name" do
      release = create(:acquired_media_release_with_contract_template, signed_at: Date.new(2020, 2, 10), name: "sample release")

      expect(release.contract_file_name).to eq("my-video-project_acquired_media_2020.02.10_1_sample-release")
    end

    context "when signed at is nil" do
      it "uses the created at date" do
        release = create(:acquired_media_release_with_contract_template, 
                         signed_at: nil, 
                         created_at: DateTime.new(2020, 2, 10, 12, 0, 0), 
                         name: "sample release")

        expect(release.contract_file_name).to eq("my-video-project_acquired_media_2020.02.10_1_sample-release")
      end
    end
  end
end
