require "rails_helper"

RSpec.describe Video do
  describe "associations" do
    it { is_expected.to belong_to(:project).touch(true) }
    it { is_expected.to have_many(:appearance_releases) }
    it { is_expected.to have_many(:location_releases) }
    it { is_expected.to have_many(:material_releases) }
    it { is_expected.to have_many(:talent_releases) }
    it { is_expected.to have_many(:bookmarks).dependent(:destroy) }
    it { is_expected.to have_many(:video_release_confirmations).dependent(:destroy) }
    it { is_expected.to have_many(:unreleased_appearances).dependent(:destroy) }
    it { is_expected.to have_many(:confirmed_talent_releases).source(:releasable).through(:video_release_confirmations) }
    it { is_expected.to have_many(:confirmed_appearance_releases).source(:releasable).through(:video_release_confirmations) }
    it { is_expected.to have_many(:confirmed_location_releases).source(:releasable).through(:video_release_confirmations) }
    it { is_expected.to have_many(:confirmed_material_releases).source(:releasable).through(:video_release_confirmations) }
    it { is_expected.to have_many(:confirmed_talent_releases).source(:releasable).through(:video_release_confirmations) }
    it { is_expected.to have_many(:confirmed_acquired_media_releases).source(:releasable).through(:video_release_confirmations) }
    it { is_expected.to have_many(:graphics_elements).dependent(:destroy) }
    it { is_expected.to have_many(:audio_confirmations).dependent(:destroy) }
  end

  describe "attachments" do
    it { is_expected.to respond_to(:file) }
    it { is_expected.to respond_to(:edl_file) }
    it { is_expected.to respond_to(:graphics_only_edl_file) }
    it { is_expected.to respond_to(:audio_only_edl_file) }
  end

  describe "validations" do
    context '#file' do
      it { is_expected.to validate_attachment_of(:file) }
      it { is_expected.to allow_content_type("video/mp4", "video/quicktime").for(:file) }
      it { is_expected.not_to allow_content_types("image/png").for(:file) }
    end

    context '#edl_file' do
      it { is_expected.to validate_attachment_of(:edl_file) }
      it { is_expected.to allow_content_type("application/octet-stream").for(:edl_file) }
      it { is_expected.not_to allow_content_types("text/plain", "image/png", "image/jpeg").for(:edl_file) }
    end

    context '#graphics_only_edl_file' do
      it { is_expected.to allow_content_type("application/octet-stream").for(:graphics_only_edl_file) }
      it { is_expected.not_to allow_content_types("text/plain", "image/png", "image/jpeg").for(:graphics_only_edl_file) }
    end

    context '#audio_only_edl_file' do
      context "when video_editing_system is adobe_premiere" do
        subject { build(:video, video_editing_system: "adobe_premiere") }

        it { is_expected.to validate_attachment_of(:audio_only_edl_file) }
        it { is_expected.to allow_content_type("application/octet-stream").for(:audio_only_edl_file) }
        it { is_expected.not_to allow_content_types("text/plain", "image/png", "image/jpeg").for(:audio_only_edl_file) }
      end

      context "when video_editing_system is not adobe_premiere" do
        subject { build(:video, video_editing_system: "avid") }

        it { is_expected.not_to validate_attachment_of(:audio_only_edl_file) }
      end
    end
  end

  describe "enums" do
    it { is_expected.to define_enum_for(:analysis_status).with_values(not_started: 0, pending: 1, success: 2, failure: 3).with_prefix(:analysis) }
    it { is_expected.to define_enum_for(:audio_analysis_status).with_values(not_started: 0, pending: 1, success: 2, failure: 3).with_prefix(:audio_analysis) }
    it { is_expected.to define_enum_for(:video_editing_system).with_values(avid: 0, adobe_premiere: 1) }
  end
end
