class Attachment < ApplicationRecord

  extend Mobility
  translates :body, :url, locale_accessors: I18n.available_locales

  ALIGNMENT_TILE = %w"N NE E SE S SW W NW"
  ALIGNMENT_SITE = %w"E W"

  TEMPLATE_SITE_ASSET = %w"Hero L"

  belongs_to :attachable_for, polymorphic: true, optional: true
  belongs_to :asset, optional: true

  store_accessor :props, :fg_color, :bg_color, :alignment, :template

  acts_as_list scope: [:attachable_for_id, :attachable_for_type]

  scope :ordered, -> { order(position: :asc) }
  scope :favorites, -> { where(is_favorite: true) }

  scope :with_text, -> { where(asset: nil) }

  scope :favorites_first, -> { reorder("(is_favorite = TRUE) DESC, position ASC") }
  scope :portraits_first, -> { reorder(Arel.sql("(props ->> 'is_portrait' = '1') DESC, position ASC")) }

  scope :not_portraits, -> { where("(props ->> 'is_portrait' IS NULL OR props ->> 'is_portrait' != '1')") }


  def is_large?
    self.is_large.to_i == 1
  end


  def is_portrait?
    self.is_portrait.to_i == 1
  end


  def template_subclass
    return 'na' if self.template.blank?
    self.template.downcase
  end


  def alignment_subclass
    return 'na' if self.alignment.blank?
    self.alignment.downcase
  end


end
