require 'rails_helper'

RSpec.describe Directory, type: :model do
  describe "associations" do
    it { is_expected.to belong_to(:project) }
    it { is_expected.to belong_to(:user) }
  end
  
  describe "validations" do
    it { is_expected.to validate_presence_of(:name) }

    describe "#name uniqueness validation" do
      subject { build(:directory) }
      it { is_expected.to validate_uniqueness_of(:name).scoped_to(:project_id) }
    end
  end

  describe "enums" do
    it { is_expected.to define_enum_for(:permissions).with_values("Everyone": 0, "Account Managers & Project Managers": 1, "Account Managers Only": 2) }
    it { is_expected.to define_enum_for(:category).with_values("Other": 0, "Finance": 1, "Scripts": 2, "Call Sheets": 3, "Photos": 4, "Videos": 5) }
  end

  describe "scopes" do
    it "should return directories in alphabetical order" do
      directory = create(:directory, name: "Payrolls", category: "Finance")
      directory_2 = create(:directory, name: "Actors", category: "Videos")

      directories = Directory.order_by_name

      # Directory one was created earlier than Directory 2
      expect(directory.created_at < directory_2.created_at).to be true

      # Directories ordered in alphabetical order
      expect(directories.first.name).to eq "Actors"
      expect(directories.last.name).to eq "Payrolls"
    end

    it "should return directories with permission 'Everyone'" do
      directory = create(:directory, name: "Payrolls", category: "Finance")
      directory_2 = create(:directory, :for_manager, name: "Actors", category: "Videos")

      directories = Directory.for_associates

      expect(directories).to include(directory)
      expect(directories).not_to include(directory_2)
    end

    it "should return directories with permission 'Account Managers & Project Managers'" do
      directory = create(:directory, name: "Payrolls", category: "Finance")
      directory_2 = create(:directory, :for_manager, name: "Actors", category: "Videos")

      directories = Directory.for_project_managers

      expect(directories).to include(directory)
      expect(directories).to include(directory_2)
    end
  end

  describe "search_files" do
    it "should return directories in alphabetical order" do
      directory = create(:directory, :with_files)
      results = directory.search_files('loc')

      expect(results.first.filename).to eq "location_photo.png"
    end
  end
end
