class Directory < ApplicationRecord
  belongs_to :project
  belongs_to :user

  has_many_attached :files

  validates :name, presence: true, uniqueness: { scope: :project_id }

  enum permissions: { "Everyone": 0, "Account Managers & Project Managers": 1, "Account Managers Only": 2 }
  enum category: { "Other": 0, "Finance": 1, "Scripts": 2, "Call Sheets": 3, "Photos": 4, "Videos": 5 }

  scope :order_by_name, -> { order(name: :asc) }
  scope :for_associates, -> { where(permissions: "Everyone") }
  scope :for_project_managers, -> { where(permissions: ["Everyone", "Account Managers & Project Managers"]) }

  def search_files(query)
    files_attachments.joins(:blob).where("active_storage_blobs.filename ILIKE ?", "%#{query}%")
  end
end
