# A store a client can install from: a WarpEngine catalog, and — optionally — a
# repository holding that store's own configuration.
#
# This is deliberately not part of WarpEngine. The engine serves one catalog and
# has no business knowing which stores exist for it; the registry is a property of
# this site, which is what the graphical client asks.
#
# The repository is optional because a store does not need one. A client takes
# identity from this record — the name, the catalog and a slug derived from it —
# and everything else from the store engine's built-in defaults. A repository is
# still honoured when given: it remains the authority on how that store behaves,
# which platforms it offers and where things land.
class Store < ApplicationRecord
  URL = %r{\Ahttps?://\S+\z}

  validates :name, presence: true
  validates :catalog_url, presence: true, format: { with: URL, message: "must be an http(s) URL" }
  validates :store_repository_url, format: { with: URL, message: "must be an http(s) URL" },
                                   allow_blank: true

  default_scope { where(deleted_at: nil) }

  scope :ordered, -> { order(:name) }

  def self.ransackable_attributes(auth_object = nil)
    %w[id name catalog_url store_repository_url created_at updated_at]
  end
end
